Skip to content

Commit 9067cc4

Browse files
committed
reafactor: improve IDistributedCache impl
1 parent e216227 commit 9067cc4

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/Enyim.Caching/DistributedCache.cs

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using Microsoft.Extensions.Caching.Distributed;
2-
using Microsoft.Extensions.Logging;
3-
using System.Threading.Tasks;
4-
using System.Threading;
5-
using Enyim.Caching.Memcached;
6-
using Microsoft.Extensions.Caching.Memory;
1+
using Enyim.Caching.Memcached;
2+
using Microsoft.Extensions.Caching.Distributed;
73
using System;
8-
using Microsoft.Extensions.Options;
4+
using System.Threading;
5+
using System.Threading.Tasks;
96

107
namespace Enyim.Caching
118
{
@@ -40,10 +37,17 @@ async Task<byte[]> IDistributedCache.GetAsync(string key, CancellationToken toke
4037
void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOptions options)
4138
{
4239
ulong tmp = 0;
40+
41+
if (!HasSlidingExpiration(options))
42+
{
43+
PerformStore(StoreMode.Set, key, value, 0, ref tmp, out var status0);
44+
return;
45+
}
46+
4347
var expiration = GetExpiration(options);
4448
PerformStore(StoreMode.Set, key, value, expiration, ref tmp, out var status);
4549

46-
if (options.SlidingExpiration.HasValue)
50+
if (options != null && options.SlidingExpiration.HasValue)
4751
{
4852
var sldExp = options.SlidingExpiration.Value;
4953
Add(GetSlidingExpirationKey(key), sldExp.ToString(), sldExp);
@@ -52,6 +56,12 @@ void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOption
5256

5357
async Task IDistributedCache.SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
5458
{
59+
if (!HasSlidingExpiration(options))
60+
{
61+
await PerformStoreAsync(StoreMode.Set, key, value, 0);
62+
return;
63+
}
64+
5565
var expiration = GetExpiration(options);
5666
await PerformStoreAsync(StoreMode.Set, key, value, expiration);
5767

@@ -62,6 +72,23 @@ void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOption
6272
}
6373
}
6474

75+
private static bool HasSlidingExpiration(DistributedCacheEntryOptions options)
76+
{
77+
if (options == null)
78+
{
79+
return false;
80+
}
81+
82+
if ((options.SlidingExpiration.HasValue == false || options.SlidingExpiration.Value == TimeSpan.Zero) &&
83+
options.AbsoluteExpiration.HasValue == false &&
84+
options.AbsoluteExpirationRelativeToNow.HasValue == false)
85+
{
86+
return false;
87+
}
88+
89+
return true;
90+
}
91+
6592
public void Refresh(string key)
6693
{
6794
var sldExpKey = GetSlidingExpirationKey(key);

src/Enyim.Caching/MemcachedClient.cs

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ private void StartPool()
8484

8585
public event Action<IMemcachedNode> NodeFailed;
8686

87+
public bool Add(string key, object value)
88+
{
89+
return Store(StoreMode.Add, key, value);
90+
}
91+
8792
public bool Add(string key, object value, int cacheSeconds)
8893
{
8994
return Store(StoreMode.Add, key, value, TimeSpan.FromSeconds(cacheSeconds));
@@ -99,6 +104,11 @@ public bool Add(string key, object value, TimeSpan timeSpan)
99104
return Store(StoreMode.Add, key, value, timeSpan);
100105
}
101106

107+
public async Task<bool> AddAsync(string key, object value)
108+
{
109+
return await StoreAsync(StoreMode.Add, key, value);
110+
}
111+
102112
public async Task<bool> AddAsync(string key, object value, int cacheSeconds)
103113
{
104114
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)
551561
return PerformStore(mode, key, value, MemcachedClient.GetExpiration(validFor, null), ref tmp, out status).Success;
552562
}
553563

564+
public async Task<bool> StoreAsync(StoreMode mode, string key, object value)
565+
{
566+
return (await PerformStoreAsync(mode, key, value, 0)).Success;
567+
}
568+
554569
public async Task<bool> StoreAsync(StoreMode mode, string key, object value, DateTime expiresAt)
555570
{
556571
return (await PerformStoreAsync(mode, key, value, MemcachedClient.GetExpiration(null, expiresAt))).Success;

0 commit comments

Comments
 (0)