forked from enyim/EnyimMemcached
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathDistributedCache.cs
160 lines (136 loc) · 5.07 KB
/
DistributedCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Enyim.Caching.Memcached;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Enyim.Caching
{
public partial class MemcachedClient
{
#region Implement IDistributedCache
byte[] IDistributedCache.Get(string key)
{
var value = Get<byte[]>(key);
if (value != null)
{
Refresh(key);
}
return value;
}
async Task<byte[]> IDistributedCache.GetAsync(string key, CancellationToken token = default)
{
var value = await GetValueAsync<byte[]>(key);
if (value != null)
{
await RefreshAsync(key);
}
return value;
}
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 != null && options.SlidingExpiration.HasValue)
{
var sldExp = options.SlidingExpiration.Value;
Add(GetSlidingExpirationKey(key), sldExp.ToString(), sldExp);
}
}
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);
if (options.SlidingExpiration.HasValue)
{
var sldExp = options.SlidingExpiration.Value;
await AddAsync(GetSlidingExpirationKey(key), sldExp.ToString(), sldExp);
}
}
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);
var sldExpStr = Get<string>(sldExpKey);
if (!string.IsNullOrEmpty(sldExpStr)
&& TimeSpan.TryParse(sldExpStr, out var sldExp))
{
var value = Get(key);
if (value != null)
{
Replace(key, value, sldExp);
Replace(sldExpKey, sldExpStr, sldExp);
}
}
}
public async Task RefreshAsync(string key, CancellationToken token = default)
{
var sldExpKey = GetSlidingExpirationKey(key);
var sldExpStr = await GetValueAsync<string>(sldExpKey);
if (!string.IsNullOrEmpty(sldExpStr)
&& TimeSpan.TryParse(sldExpStr, out var sldExp))
{
var value = (await GetAsync(key)).Value;
if (value != null)
{
await ReplaceAsync(key, value, sldExp);
await ReplaceAsync(sldExpKey, sldExpStr, sldExp);
}
}
}
void IDistributedCache.Remove(string key)
{
Remove(key);
Remove(GetSlidingExpirationKey(key));
}
async Task IDistributedCache.RemoveAsync(string key, CancellationToken token = default)
{
await RemoveAsync(key);
await RemoveAsync(GetSlidingExpirationKey(key));
}
private uint GetExpiration(DistributedCacheEntryOptions options)
{
if (options.SlidingExpiration.HasValue)
{
return GetExpiration(options.SlidingExpiration);
}
else if (options.AbsoluteExpirationRelativeToNow.HasValue)
{
return GetExpiration(null, relativeToNow: options.AbsoluteExpirationRelativeToNow.Value);
}
else if (options.AbsoluteExpiration.HasValue)
{
return GetExpiration(null, absoluteExpiration: options.AbsoluteExpiration.Value);
}
else
{
throw new ArgumentException("Invalid enum value for options", nameof(options));
}
}
private string GetSlidingExpirationKey(string key) => $"{key}-sliding-expiration";
#endregion
}
}