Skip to content

Commit

Permalink
Merge pull request #244 from cnblogs/improve-idistributedcache-impl
Browse files Browse the repository at this point in the history
reafactor: improve IDistributedCache impl
  • Loading branch information
cnblogs-dudu authored Nov 23, 2024
2 parents e216227 + 9067cc4 commit 4f90f0f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/Enyim.Caching/DistributedCache.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -40,10 +37,17 @@ async Task<byte[]> 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);
Expand All @@ -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);

Expand All @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ private void StartPool()

public event Action<IMemcachedNode> 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));
Expand All @@ -99,6 +104,11 @@ public bool Add(string key, object value, TimeSpan timeSpan)
return Store(StoreMode.Add, key, value, timeSpan);
}

public async Task<bool> AddAsync(string key, object value)
{
return await StoreAsync(StoreMode.Add, key, value);
}

public async Task<bool> AddAsync(string key, object value, int cacheSeconds)
{
return await StoreAsync(StoreMode.Add, key, value, TimeSpan.FromSeconds(cacheSeconds));
Expand Down Expand Up @@ -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<bool> StoreAsync(StoreMode mode, string key, object value)
{
return (await PerformStoreAsync(mode, key, value, 0)).Success;
}

public async Task<bool> StoreAsync(StoreMode mode, string key, object value, DateTime expiresAt)
{
return (await PerformStoreAsync(mode, key, value, MemcachedClient.GetExpiration(null, expiresAt))).Success;
Expand Down

0 comments on commit 4f90f0f

Please sign in to comment.