| | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | 2 | | using MRA.Infrastructure.Settings; |
| | 3 | | using System.Reflection; |
| | 4 | |
|
| | 5 | | namespace MRA.Infrastructure.Cache; |
| | 6 | |
|
| | 7 | | public class MicrosoftCacheProvider : ICacheProvider |
| | 8 | | { |
| | 9 | | internal readonly IMemoryCache _cache; |
| | 10 | | internal readonly AppSettings _appSettings; |
| | 11 | |
|
| 1 | 12 | | public MicrosoftCacheProvider(AppSettings appSettings, IMemoryCache cache) |
| | 13 | | { |
| 1 | 14 | | _cache = cache; |
| 1 | 15 | | _appSettings = appSettings; |
| 1 | 16 | | } |
| | 17 | |
|
| | 18 | | public void ClearCacheItem(string item) |
| | 19 | | { |
| 0 | 20 | | _cache.Remove(item); |
| 0 | 21 | | } |
| | 22 | |
|
| | 23 | | public void Clear() |
| | 24 | | { |
| 0 | 25 | | if (_cache == null) |
| | 26 | | { |
| 0 | 27 | | throw new ArgumentNullException("Memory cache must not be null"); |
| | 28 | | } |
| 0 | 29 | | else if (_cache is MemoryCache memCache) |
| | 30 | | { |
| 0 | 31 | | memCache.Compact(1.0); |
| 0 | 32 | | return; |
| | 33 | | } |
| | 34 | | else |
| | 35 | | { |
| 0 | 36 | | MethodInfo clearMethod = _cache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Public); |
| 0 | 37 | | if (clearMethod != null) |
| | 38 | | { |
| 0 | 39 | | clearMethod.Invoke(_cache, null); |
| 0 | 40 | | return; |
| | 41 | | } |
| | 42 | | else |
| | 43 | | { |
| 0 | 44 | | PropertyInfo prop = _cache.GetType().GetProperty("EntriesCollection", BindingFlags.Instance | BindingFla |
| 0 | 45 | | if (prop != null) |
| | 46 | | { |
| 0 | 47 | | object innerCache = prop.GetValue(_cache); |
| 0 | 48 | | if (innerCache != null) |
| | 49 | | { |
| 0 | 50 | | clearMethod = innerCache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Publi |
| 0 | 51 | | if (clearMethod != null) |
| | 52 | | { |
| 0 | 53 | | clearMethod.Invoke(innerCache, null); |
| 0 | 54 | | return; |
| | 55 | | } |
| | 56 | | } |
| | 57 | | } |
| | 58 | | } |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | throw new InvalidOperationException("Unable to clear memory cache instance of type " + _cache.GetType().FullName |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public T GetOrSetFromCache<T>(string cacheKey, Func<T> getDataFunc) |
| | 65 | | { |
| 0 | 66 | | if (_cache.TryGetValue(cacheKey, out T cachedData)) |
| | 67 | | { |
| 0 | 68 | | return cachedData; |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | var data = getDataFunc(); |
| | 72 | |
|
| 0 | 73 | | var cacheEntryOptions = new MemoryCacheEntryOptions |
| 0 | 74 | | { |
| 0 | 75 | | AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_appSettings.Cache.RefreshSeconds) |
| 0 | 76 | | }; |
| | 77 | |
|
| 0 | 78 | | _cache.Set(cacheKey, data, cacheEntryOptions); |
| | 79 | |
|
| 0 | 80 | | return data; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public async Task<T> GetOrSetFromCacheAsync<T>(string cacheKey, Func<Task<T>> getDataFunc, bool useCache) |
| | 84 | | { |
| 0 | 85 | | if (!useCache || _cache == null) |
| | 86 | | { |
| 0 | 87 | | return await getDataFunc(); |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | if (_cache.TryGetValue(cacheKey, out T cachedData)) |
| | 91 | | { |
| 0 | 92 | | return cachedData; |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | var data = await getDataFunc(); |
| | 96 | |
|
| 0 | 97 | | var cacheEntryOptions = new MemoryCacheEntryOptions |
| 0 | 98 | | { |
| 0 | 99 | | AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_appSettings.Cache.RefreshSeconds) |
| 0 | 100 | | }; |
| | 101 | |
|
| 0 | 102 | | _cache.Set(cacheKey, data, cacheEntryOptions); |
| | 103 | |
|
| 0 | 104 | | return data; |
| 0 | 105 | | } |
| | 106 | | } |