Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve IDistributedCache impl #244

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading