< Summary

Information
Class: Services.CacheServices.Cache.UniqueValuesCache.UniqueValuesCache
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/CacheServices/Cache/UniqueValuesCache/UniqueValuesCache.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 67
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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%210%
GetUniqueArticles()100%210%
GetUniqueDeliveryCitys()100%210%
GetUniqueShipmentNumbers()100%210%
GetArticleCountsForOrdersAsync(...)100%210%
GetDeliveryCityCountsForOrdersAsync(...)100%210%
GetShipmentBaseCountsForOrdersAsync(...)100%210%
UpdateCache()100%210%
GetOrSetCache()0%4260%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/CacheServices/Cache/UniqueValuesCache/UniqueValuesCache.cs

#LineLine coverage
 1using Microsoft.Extensions.Caching.Memory;
 2using OzonDomains.Models;
 3using Services.DataServices;
 4
 5namespace Services.CacheServices.Cache.UniqueValuesCache
 6{
 7    public class UniqueValuesCache : IUniqueValuesCache
 8    {
 9        private readonly IMemoryCache _cache;
 10        private readonly OrdersDataServices _orderService;
 11
 12        private const string CacheKeyArticles = "UniqueArticles";
 13        private const string CacheKeyDeliveryCities = "UniqueDeliveryCities";
 14        private const string CacheKeyShipmentNumbers = "UniqueShipmentNumbers";
 015        private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(10);
 16
 017        public UniqueValuesCache(IMemoryCache memoryCache, OrdersDataServices orderService)
 18        {
 019            _cache = memoryCache;
 020            _orderService = orderService;
 021        }
 22
 23        public Task<Dictionary<string, int>> GetUniqueArticles() =>
 024            GetOrSetCache(CacheKeyArticles, _orderService.GetUniqueArticles);
 25
 26        public Task<Dictionary<string, int>> GetUniqueDeliveryCitys() =>
 027            GetOrSetCache(CacheKeyDeliveryCities, _orderService.GetUniqueDeliveryCities);
 28
 29        public Task<Dictionary<string, int>> GetUniqueShipmentNumbers() =>
 030            GetOrSetCache(CacheKeyShipmentNumbers, _orderService.GetUniqueShipmentNumbers);
 31
 32        public Task<Dictionary<string, int>> GetArticleCountsForOrdersAsync(
 33            IEnumerable<Order> orders, bool includeArchived = false) =>
 034            _orderService.GetArticleCountsForOrdersAsync(orders, includeArchived);
 35
 36        public Task<Dictionary<string, int>> GetDeliveryCityCountsForOrdersAsync(
 37            IEnumerable<Order> orders, bool includeArchived = false) =>
 038            _orderService.GetDeliveryCityCountsForOrdersAsync(orders, includeArchived);
 39
 40        public Task<Dictionary<string, int>> GetShipmentBaseCountsForOrdersAsync(
 41            IEnumerable<Order> orders, bool includeArchived = false) =>
 042            _orderService.GetShipmentBaseCountsForOrdersAsync(orders, includeArchived);
 43
 44        public async Task UpdateCache()
 45        {
 046            _cache.Remove(CacheKeyArticles);
 047            _cache.Remove(CacheKeyDeliveryCities);
 048            _cache.Remove(CacheKeyShipmentNumbers);
 049            await Task.CompletedTask;
 050        }
 51
 52        private async Task<Dictionary<string, int>> GetOrSetCache(
 53            string cacheKey,
 54            Func<Task<Dictionary<string, int>>> dataRetrievalFunction,
 55            bool forceUpdate = false)
 56        {
 057            if (!forceUpdate && _cache.TryGetValue(cacheKey, out Dictionary<string, int>? cachedData) && cachedData != n
 58            {
 059                return cachedData;
 60            }
 61
 062            var data = await dataRetrievalFunction();
 063            _cache.Set(cacheKey, data, _cacheDuration);
 064            return data;
 065        }
 66    }
 67}