|
3 | 3 | using Configuration.Caching; |
4 | 4 | using JetBrains.Annotations; |
5 | 5 | using Microsoft.Extensions.Caching.Memory; |
| 6 | + using Microsoft.Extensions.Internal; |
6 | 7 | using System; |
7 | 8 | using Utility; |
8 | 9 |
|
9 | | - /// <summary> |
10 | | - /// An implementation of <see cref="ICacheProvider{TCache}" />. |
11 | | - /// </summary> |
12 | | - public class InMemoryCacheProvider : CacheProviderBase<InMemoryCache> |
| 10 | + internal class InMemoryCacheProvider : ICacheProvider |
13 | 11 | { |
| 12 | + #region Properties |
| 13 | + |
| 14 | + public IMemoryCache Cache { get; } |
| 15 | + public TimeSpan? Expiry { get; set; } |
| 16 | + |
| 17 | + #endregion |
| 18 | + |
14 | 19 | #region Constructors |
15 | 20 |
|
16 | | - /// <summary> |
17 | | - /// Initializes a new instance of the <see cref="InMemoryCacheProvider" /> class. |
18 | | - /// </summary> |
19 | | - public InMemoryCacheProvider() : this((TimeSpan?)null) { } |
20 | | - |
21 | | - /// <summary> |
22 | | - /// Initializes a new instance of the <see cref="InMemoryCacheProvider" /> class. |
23 | | - /// </summary> |
24 | | - /// <param name="expiry">The caching expiration time.</param> |
25 | | - public InMemoryCacheProvider([CanBeNull] TimeSpan? expiry) : this(new MemoryCache(new MemoryCacheOptions()), expiry) { } |
26 | | - |
27 | | - /// <summary> |
28 | | - /// Initializes a new instance of the <see cref="InMemoryCacheProvider" /> class. |
29 | | - /// </summary> |
30 | | - /// <param name="optionsAction">The configuration options action.</param> |
31 | | - public InMemoryCacheProvider([NotNull] Action<MemoryCacheOptions> optionsAction) : this(optionsAction, (TimeSpan?)null) { } |
32 | | - |
33 | | - /// <summary> |
34 | | - /// Initializes a new instance of the <see cref="InMemoryCacheProvider" /> class. |
35 | | - /// </summary> |
36 | | - /// <param name="optionsAction">The configuration options action.</param> |
37 | | - /// <param name="expiry">The caching expiration time.</param> |
38 | | - public InMemoryCacheProvider([NotNull] Action<MemoryCacheOptions> optionsAction, [CanBeNull] TimeSpan? expiry) : this(new MemoryCache(GetConfigurationOptions(optionsAction)), expiry) { } |
39 | | - |
40 | | - /// <summary> |
41 | | - /// Initializes a new instance of the <see cref="InMemoryCacheProvider" /> class. |
42 | | - /// </summary> |
43 | | - /// <param name="cache">The underlying caching storage.</param> |
44 | | - public InMemoryCacheProvider([NotNull] IMemoryCache cache) : this(cache, (TimeSpan?)null) { } |
45 | | - |
46 | | - /// <summary> |
47 | | - /// Initializes a new instance of the <see cref="InMemoryCacheProvider" /> class. |
48 | | - /// </summary> |
49 | | - /// <param name="cache">The underlying caching storage.</param> |
50 | | - /// <param name="expiry">The caching expiration time.</param> |
51 | | - public InMemoryCacheProvider([NotNull] IMemoryCache cache, [CanBeNull] TimeSpan? expiry) |
| 21 | + public InMemoryCacheProvider() : this(null, null, null) { } |
| 22 | + |
| 23 | + public InMemoryCacheProvider(ISystemClock clock, TimeSpan? expirationScanFrequency, TimeSpan? expiry) |
| 24 | + : this (GetMemoryCacheOptions(clock, expirationScanFrequency), expiry) { } |
| 25 | + |
| 26 | + private InMemoryCacheProvider(MemoryCacheOptions options, TimeSpan? expiry) |
52 | 27 | { |
53 | | - Cache = new InMemoryCache(Guard.NotNull(cache, nameof(cache))); |
| 28 | + Cache = new MemoryCache(Guard.NotNull(options, nameof(options))); |
54 | 29 | Expiry = expiry; |
55 | 30 | } |
56 | 31 |
|
57 | 32 | #endregion |
58 | 33 |
|
59 | 34 | #region Private Methods |
60 | 35 |
|
61 | | - private static MemoryCacheOptions GetConfigurationOptions(Action<MemoryCacheOptions> optionsAction) |
| 36 | + private static MemoryCacheOptions GetMemoryCacheOptions(ISystemClock clock, TimeSpan? expirationScanFrequency) |
62 | 37 | { |
63 | | - Guard.NotNull(optionsAction, nameof(optionsAction)); |
64 | | - |
65 | 38 | var options = new MemoryCacheOptions(); |
66 | 39 |
|
67 | | - optionsAction(options); |
| 40 | + options.Clock = clock; |
| 41 | + |
| 42 | + if (expirationScanFrequency.HasValue) |
| 43 | + { |
| 44 | + options.ExpirationScanFrequency = expirationScanFrequency.Value; |
| 45 | + } |
68 | 46 |
|
69 | 47 | return options; |
70 | 48 | } |
71 | 49 |
|
| 50 | + private void Set<T>(string key, T value, CacheItemPriority priority, TimeSpan? expiry, Action<string> cacheRemovedCallback = null) |
| 51 | + { |
| 52 | + Guard.NotEmpty(key, nameof(key)); |
| 53 | + |
| 54 | + var policy = new MemoryCacheEntryOptions(); |
| 55 | + |
| 56 | + if (cacheRemovedCallback != null) |
| 57 | + { |
| 58 | + policy.PostEvictionCallbacks.Add(new PostEvictionCallbackRegistration |
| 59 | + { |
| 60 | + EvictionCallback = (o, val, reason, state) => |
| 61 | + { |
| 62 | + cacheRemovedCallback(reason.ToString()); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + if (expiry.HasValue && expiry.Value != TimeSpan.Zero) |
| 68 | + policy.AbsoluteExpiration = DateTimeOffset.Now.Add(expiry.Value); |
| 69 | + |
| 70 | + if (expiry.HasValue && expiry.Value == TimeSpan.Zero && priority != CacheItemPriority.NeverRemove) |
| 71 | + policy.Priority = CacheItemPriority.NeverRemove; |
| 72 | + else |
| 73 | + policy.Priority = priority; |
| 74 | + |
| 75 | + Cache.Set<T>(key, value, policy); |
| 76 | + } |
| 77 | + |
| 78 | + #endregion |
| 79 | + |
| 80 | + #region Implementation of ICacheProvider |
| 81 | + |
| 82 | + public void Set<T>([NotNull] string key, T value, TimeSpan? expiry = null, Action<string> cacheRemovedCallback = null) |
| 83 | + { |
| 84 | + Set<T>(key, value, CacheItemPriority.Normal, expiry ?? Expiry, cacheRemovedCallback); |
| 85 | + } |
| 86 | + |
| 87 | + public void Remove([NotNull] string key) |
| 88 | + { |
| 89 | + Cache.Remove(Guard.NotEmpty(key, nameof(key))); |
| 90 | + } |
| 91 | + |
| 92 | + public bool TryGetValue<T>([NotNull] string key, out T value) |
| 93 | + { |
| 94 | + return Cache.TryGetValue<T>(Guard.NotEmpty(key, nameof(key)), out value); |
| 95 | + } |
| 96 | + |
| 97 | + public int Increment([NotNull] string key, int defaultValue, int incrementValue) |
| 98 | + { |
| 99 | + Guard.NotEmpty(key, nameof(key)); |
| 100 | + |
| 101 | + if (!TryGetValue<int>(key, out var current)) |
| 102 | + current = defaultValue; |
| 103 | + |
| 104 | + var value = current + incrementValue; |
| 105 | + |
| 106 | + Set<int>(key, value, CacheItemPriority.NeverRemove, expiry: null); |
| 107 | + |
| 108 | + return value; |
| 109 | + } |
| 110 | + |
72 | 111 | #endregion |
73 | 112 | } |
74 | 113 | } |
0 commit comments