< Summary

Information
Class: MRA.Infrastructure.Cache.MicrosoftCacheProvider
Assembly: MRA.Infrastructure
File(s): D:\a\MiguelRomerART\MiguelRomerART\MRA.Infrastructure\Cache\MicrosoftCacheProvider.cs
Line coverage
8%
Covered lines: 4
Uncovered lines: 41
Coverable lines: 45
Total lines: 106
Line coverage: 8.8%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ClearCacheItem(...)100%210%
Clear()0%156120%
GetOrSetFromCache(...)0%620%
GetOrSetFromCacheAsync()0%4260%

File(s)

D:\a\MiguelRomerART\MiguelRomerART\MRA.Infrastructure\Cache\MicrosoftCacheProvider.cs

#LineLine coverage
 1using Microsoft.Extensions.Caching.Memory;
 2using MRA.Infrastructure.Settings;
 3using System.Reflection;
 4
 5namespace MRA.Infrastructure.Cache;
 6
 7public class MicrosoftCacheProvider : ICacheProvider
 8{
 9    internal readonly IMemoryCache _cache;
 10    internal readonly AppSettings _appSettings;
 11
 112    public MicrosoftCacheProvider(AppSettings appSettings, IMemoryCache cache)
 13    {
 114        _cache = cache;
 115        _appSettings = appSettings;
 116    }
 17
 18    public void ClearCacheItem(string item)
 19    {
 020        _cache.Remove(item);
 021    }
 22
 23    public void Clear()
 24    {
 025        if (_cache == null)
 26        {
 027            throw new ArgumentNullException("Memory cache must not be null");
 28        }
 029        else if (_cache is MemoryCache memCache)
 30        {
 031            memCache.Compact(1.0);
 032            return;
 33        }
 34        else
 35        {
 036            MethodInfo clearMethod = _cache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Public);
 037            if (clearMethod != null)
 38            {
 039                clearMethod.Invoke(_cache, null);
 040                return;
 41            }
 42            else
 43            {
 044                PropertyInfo prop = _cache.GetType().GetProperty("EntriesCollection", BindingFlags.Instance | BindingFla
 045                if (prop != null)
 46                {
 047                    object innerCache = prop.GetValue(_cache);
 048                    if (innerCache != null)
 49                    {
 050                        clearMethod = innerCache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Publi
 051                        if (clearMethod != null)
 52                        {
 053                            clearMethod.Invoke(innerCache, null);
 054                            return;
 55                        }
 56                    }
 57                }
 58            }
 59        }
 60
 061        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    {
 066        if (_cache.TryGetValue(cacheKey, out T cachedData))
 67        {
 068            return cachedData;
 69        }
 70
 071        var data = getDataFunc();
 72
 073        var cacheEntryOptions = new MemoryCacheEntryOptions
 074        {
 075            AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_appSettings.Cache.RefreshSeconds)
 076        };
 77
 078        _cache.Set(cacheKey, data, cacheEntryOptions);
 79
 080        return data;
 81    }
 82
 83    public async Task<T> GetOrSetFromCacheAsync<T>(string cacheKey, Func<Task<T>> getDataFunc, bool useCache)
 84    {
 085        if (!useCache || _cache == null)
 86        {
 087            return await getDataFunc();
 88        }
 89
 090        if (_cache.TryGetValue(cacheKey, out T cachedData))
 91        {
 092            return cachedData;
 93        }
 94
 095        var data = await getDataFunc();
 96
 097        var cacheEntryOptions = new MemoryCacheEntryOptions
 098        {
 099            AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_appSettings.Cache.RefreshSeconds)
 0100        };
 101
 0102        _cache.Set(cacheKey, data, cacheEntryOptions);
 103
 0104        return data;
 0105    }
 106}