|
| 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; |
| 7 | +using System; |
| 8 | +using Microsoft.Extensions.Options; |
| 9 | + |
| 10 | +namespace Enyim.Caching |
| 11 | +{ |
| 12 | + public partial class MemcachedClient |
| 13 | + { |
| 14 | + #region Implement IDistributedCache |
| 15 | + |
| 16 | + byte[] IDistributedCache.Get(string key) |
| 17 | + { |
| 18 | + var value = Get<byte[]>(key); |
| 19 | + |
| 20 | + if (value != null) |
| 21 | + { |
| 22 | + Refresh(key); |
| 23 | + } |
| 24 | + |
| 25 | + return value; |
| 26 | + } |
| 27 | + |
| 28 | + async Task<byte[]> IDistributedCache.GetAsync(string key, CancellationToken token = default) |
| 29 | + { |
| 30 | + var value = await GetValueAsync<byte[]>(key); |
| 31 | + |
| 32 | + if (value != null) |
| 33 | + { |
| 34 | + await RefreshAsync(key); |
| 35 | + } |
| 36 | + |
| 37 | + return value; |
| 38 | + } |
| 39 | + |
| 40 | + void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOptions options) |
| 41 | + { |
| 42 | + ulong tmp = 0; |
| 43 | + var expiration = GetExpiration(options); |
| 44 | + PerformStore(StoreMode.Set, key, value, expiration, ref tmp, out var status); |
| 45 | + |
| 46 | + if (options.SlidingExpiration.HasValue) |
| 47 | + { |
| 48 | + var sldExp = options.SlidingExpiration.Value; |
| 49 | + Add(GetSlidingExpirationKey(key), sldExp.ToString(), sldExp); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + async Task IDistributedCache.SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken)) |
| 54 | + { |
| 55 | + var expiration = GetExpiration(options); |
| 56 | + await PerformStoreAsync(StoreMode.Set, key, value, expiration); |
| 57 | + |
| 58 | + if (options.SlidingExpiration.HasValue) |
| 59 | + { |
| 60 | + var sldExp = options.SlidingExpiration.Value; |
| 61 | + await AddAsync(GetSlidingExpirationKey(key), sldExp.ToString(), sldExp); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void Refresh(string key) |
| 66 | + { |
| 67 | + var sldExpKey = GetSlidingExpirationKey(key); |
| 68 | + var sldExpStr = Get<string>(sldExpKey); |
| 69 | + if (!string.IsNullOrEmpty(sldExpStr) |
| 70 | + && TimeSpan.TryParse(sldExpStr, out var sldExp)) |
| 71 | + { |
| 72 | + var value = Get(key); |
| 73 | + if (value != null) |
| 74 | + { |
| 75 | + Replace(key, value, sldExp); |
| 76 | + Replace(sldExpKey, sldExpStr, sldExp); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public async Task RefreshAsync(string key, CancellationToken token = default) |
| 82 | + { |
| 83 | + var sldExpKey = GetSlidingExpirationKey(key); |
| 84 | + var sldExpStr = await GetValueAsync<string>(sldExpKey); |
| 85 | + if (!string.IsNullOrEmpty(sldExpStr) |
| 86 | + && TimeSpan.TryParse(sldExpStr, out var sldExp)) |
| 87 | + { |
| 88 | + var value = (await GetAsync(key)).Value; |
| 89 | + if (value != null) |
| 90 | + { |
| 91 | + await ReplaceAsync(key, value, sldExp); |
| 92 | + await ReplaceAsync(sldExpKey, sldExpStr, sldExp); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + void IDistributedCache.Remove(string key) |
| 98 | + { |
| 99 | + Remove(key); |
| 100 | + Remove(GetSlidingExpirationKey(key)); |
| 101 | + } |
| 102 | + |
| 103 | + async Task IDistributedCache.RemoveAsync(string key, CancellationToken token = default) |
| 104 | + { |
| 105 | + await RemoveAsync(key); |
| 106 | + await RemoveAsync(GetSlidingExpirationKey(key)); |
| 107 | + } |
| 108 | + |
| 109 | + private uint GetExpiration(DistributedCacheEntryOptions options) |
| 110 | + { |
| 111 | + if (options.SlidingExpiration.HasValue) |
| 112 | + { |
| 113 | + return GetExpiration(options.SlidingExpiration); |
| 114 | + } |
| 115 | + else if (options.AbsoluteExpirationRelativeToNow.HasValue) |
| 116 | + { |
| 117 | + return GetExpiration(null, relativeToNow: options.AbsoluteExpirationRelativeToNow.Value); |
| 118 | + } |
| 119 | + else if (options.AbsoluteExpiration.HasValue) |
| 120 | + { |
| 121 | + return GetExpiration(null, absoluteExpiration: options.AbsoluteExpiration.Value); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + throw new ArgumentException("Invalid enum value for options", nameof(options)); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private string GetSlidingExpirationKey(string key) => $"{key}-sliding-expiration"; |
| 130 | + |
| 131 | + #endregion |
| 132 | + } |
| 133 | +} |
0 commit comments