From 9067cc410c47bafe4c62b8458dd696baa7c829bb Mon Sep 17 00:00:00 2001 From: dudu Date: Sat, 23 Nov 2024 11:21:47 +0800 Subject: [PATCH] reafactor: improve IDistributedCache impl --- src/Enyim.Caching/DistributedCache.cs | 43 ++++++++++++++++++++++----- src/Enyim.Caching/MemcachedClient.cs | 15 ++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/Enyim.Caching/DistributedCache.cs b/src/Enyim.Caching/DistributedCache.cs index 26ba7869..c9ba9b9c 100644 --- a/src/Enyim.Caching/DistributedCache.cs +++ b/src/Enyim.Caching/DistributedCache.cs @@ -1,11 +1,8 @@ -using Microsoft.Extensions.Caching.Distributed; -using Microsoft.Extensions.Logging; -using System.Threading.Tasks; -using System.Threading; -using Enyim.Caching.Memcached; -using Microsoft.Extensions.Caching.Memory; +using Enyim.Caching.Memcached; +using Microsoft.Extensions.Caching.Distributed; using System; -using Microsoft.Extensions.Options; +using System.Threading; +using System.Threading.Tasks; namespace Enyim.Caching { @@ -40,10 +37,17 @@ async Task IDistributedCache.GetAsync(string key, CancellationToken toke void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOptions options) { ulong tmp = 0; + + if (!HasSlidingExpiration(options)) + { + PerformStore(StoreMode.Set, key, value, 0, ref tmp, out var status0); + return; + } + var expiration = GetExpiration(options); PerformStore(StoreMode.Set, key, value, expiration, ref tmp, out var status); - if (options.SlidingExpiration.HasValue) + if (options != null && options.SlidingExpiration.HasValue) { var sldExp = options.SlidingExpiration.Value; Add(GetSlidingExpirationKey(key), sldExp.ToString(), sldExp); @@ -52,6 +56,12 @@ void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOption async Task IDistributedCache.SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken)) { + if (!HasSlidingExpiration(options)) + { + await PerformStoreAsync(StoreMode.Set, key, value, 0); + return; + } + var expiration = GetExpiration(options); await PerformStoreAsync(StoreMode.Set, key, value, expiration); @@ -62,6 +72,23 @@ void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOption } } + private static bool HasSlidingExpiration(DistributedCacheEntryOptions options) + { + if (options == null) + { + return false; + } + + if ((options.SlidingExpiration.HasValue == false || options.SlidingExpiration.Value == TimeSpan.Zero) && + options.AbsoluteExpiration.HasValue == false && + options.AbsoluteExpirationRelativeToNow.HasValue == false) + { + return false; + } + + return true; + } + public void Refresh(string key) { var sldExpKey = GetSlidingExpirationKey(key); diff --git a/src/Enyim.Caching/MemcachedClient.cs b/src/Enyim.Caching/MemcachedClient.cs index 3354bf9e..8ba225e1 100755 --- a/src/Enyim.Caching/MemcachedClient.cs +++ b/src/Enyim.Caching/MemcachedClient.cs @@ -84,6 +84,11 @@ private void StartPool() public event Action NodeFailed; + public bool Add(string key, object value) + { + return Store(StoreMode.Add, key, value); + } + public bool Add(string key, object value, int cacheSeconds) { return Store(StoreMode.Add, key, value, TimeSpan.FromSeconds(cacheSeconds)); @@ -99,6 +104,11 @@ public bool Add(string key, object value, TimeSpan timeSpan) return Store(StoreMode.Add, key, value, timeSpan); } + public async Task AddAsync(string key, object value) + { + return await StoreAsync(StoreMode.Add, key, value); + } + public async Task AddAsync(string key, object value, int cacheSeconds) { return await StoreAsync(StoreMode.Add, key, value, TimeSpan.FromSeconds(cacheSeconds)); @@ -551,6 +561,11 @@ public bool Store(StoreMode mode, string key, object value, TimeSpan validFor) return PerformStore(mode, key, value, MemcachedClient.GetExpiration(validFor, null), ref tmp, out status).Success; } + public async Task StoreAsync(StoreMode mode, string key, object value) + { + return (await PerformStoreAsync(mode, key, value, 0)).Success; + } + public async Task StoreAsync(StoreMode mode, string key, object value, DateTime expiresAt) { return (await PerformStoreAsync(mode, key, value, MemcachedClient.GetExpiration(null, expiresAt))).Success;