| | | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using Services.DataServices; |
| | | 4 | | |
| | | 5 | | namespace 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"; |
| | 0 | 15 | | private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(10); |
| | | 16 | | |
| | 0 | 17 | | public UniqueValuesCache(IMemoryCache memoryCache, OrdersDataServices orderService) |
| | | 18 | | { |
| | 0 | 19 | | _cache = memoryCache; |
| | 0 | 20 | | _orderService = orderService; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public Task<Dictionary<string, int>> GetUniqueArticles() => |
| | 0 | 24 | | GetOrSetCache(CacheKeyArticles, _orderService.GetUniqueArticles); |
| | | 25 | | |
| | | 26 | | public Task<Dictionary<string, int>> GetUniqueDeliveryCitys() => |
| | 0 | 27 | | GetOrSetCache(CacheKeyDeliveryCities, _orderService.GetUniqueDeliveryCities); |
| | | 28 | | |
| | | 29 | | public Task<Dictionary<string, int>> GetUniqueShipmentNumbers() => |
| | 0 | 30 | | GetOrSetCache(CacheKeyShipmentNumbers, _orderService.GetUniqueShipmentNumbers); |
| | | 31 | | |
| | | 32 | | public Task<Dictionary<string, int>> GetArticleCountsForOrdersAsync( |
| | | 33 | | IEnumerable<Order> orders, bool includeArchived = false) => |
| | 0 | 34 | | _orderService.GetArticleCountsForOrdersAsync(orders, includeArchived); |
| | | 35 | | |
| | | 36 | | public Task<Dictionary<string, int>> GetDeliveryCityCountsForOrdersAsync( |
| | | 37 | | IEnumerable<Order> orders, bool includeArchived = false) => |
| | 0 | 38 | | _orderService.GetDeliveryCityCountsForOrdersAsync(orders, includeArchived); |
| | | 39 | | |
| | | 40 | | public Task<Dictionary<string, int>> GetShipmentBaseCountsForOrdersAsync( |
| | | 41 | | IEnumerable<Order> orders, bool includeArchived = false) => |
| | 0 | 42 | | _orderService.GetShipmentBaseCountsForOrdersAsync(orders, includeArchived); |
| | | 43 | | |
| | | 44 | | public async Task UpdateCache() |
| | | 45 | | { |
| | 0 | 46 | | _cache.Remove(CacheKeyArticles); |
| | 0 | 47 | | _cache.Remove(CacheKeyDeliveryCities); |
| | 0 | 48 | | _cache.Remove(CacheKeyShipmentNumbers); |
| | 0 | 49 | | await Task.CompletedTask; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | private async Task<Dictionary<string, int>> GetOrSetCache( |
| | | 53 | | string cacheKey, |
| | | 54 | | Func<Task<Dictionary<string, int>>> dataRetrievalFunction, |
| | | 55 | | bool forceUpdate = false) |
| | | 56 | | { |
| | 0 | 57 | | if (!forceUpdate && _cache.TryGetValue(cacheKey, out Dictionary<string, int>? cachedData) && cachedData != n |
| | | 58 | | { |
| | 0 | 59 | | return cachedData; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | var data = await dataRetrievalFunction(); |
| | 0 | 63 | | _cache.Set(cacheKey, data, _cacheDuration); |
| | 0 | 64 | | return data; |
| | 0 | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |