| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Caching.Memory; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using OzonRepositories.Context; |
| | | 6 | | using Services.CacheServices; |
| | | 7 | | |
| | | 8 | | namespace Services.CacheServices.Cache.EtProducerCache |
| | | 9 | | { |
| | | 10 | | public class EtProducerCache : IRedisAppCache<EtProducer> |
| | | 11 | | { |
| | | 12 | | private readonly IMemoryCache _memoryCache; |
| | | 13 | | private readonly JcEtalonContext _jcEtalonContext; |
| | | 14 | | private const string CacheKey = "EtProducers"; |
| | | 15 | | private const int InitialLoadCount = 500; |
| | | 16 | | private const int IncrementCount = 100; |
| | 1 | 17 | | private int _maxPage = 1; |
| | 1 | 18 | | private int _maxItemsToLoad = InitialLoadCount; |
| | | 19 | | |
| | 1 | 20 | | public EtProducerCache(IMemoryCache memoryCache, JcEtalonContext jcEtalonContext) |
| | | 21 | | { |
| | 1 | 22 | | _memoryCache = memoryCache; |
| | 1 | 23 | | _jcEtalonContext = jcEtalonContext; |
| | 1 | 24 | | } |
| | | 25 | | |
| | | 26 | | private void SetCache(string key, object value, TimeSpan expiration) |
| | | 27 | | { |
| | 1 | 28 | | var cacheEntryOptions = new MemoryCacheEntryOptions() |
| | 1 | 29 | | .SetAbsoluteExpiration(expiration); |
| | 1 | 30 | | _memoryCache.Set(key, value, cacheEntryOptions); |
| | 1 | 31 | | } |
| | | 32 | | |
| | | 33 | | private T? GetCache<T>(string key) |
| | | 34 | | { |
| | 1 | 35 | | return _memoryCache.TryGetValue(key, out T? value) ? value : default; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task<List<EtProducer>> Get(int page) |
| | | 39 | | { |
| | 1 | 40 | | var cachedEtProducers = GetCache<List<EtProducer>>(CacheKey); |
| | 1 | 41 | | if (cachedEtProducers == null) |
| | | 42 | | { |
| | 1 | 43 | | cachedEtProducers = (await _jcEtalonContext.EtProducers.OrderBy(e => e.Id).Take(InitialLoadCount).ToList |
| | 1 | 44 | | .ToList(); |
| | 1 | 45 | | _maxPage = 10; |
| | 1 | 46 | | SetCache(CacheKey, cachedEtProducers, TimeSpan.FromMinutes(10)); |
| | | 47 | | } |
| | | 48 | | |
| | 1 | 49 | | int requiredItemCount = page * IncrementCount; |
| | 1 | 50 | | if (cachedEtProducers.Count < requiredItemCount) |
| | | 51 | | { |
| | 1 | 52 | | int itemsToLoad = requiredItemCount - cachedEtProducers.Count; |
| | 1 | 53 | | var additionalEtProducers = await _jcEtalonContext.EtProducers |
| | 1 | 54 | | .OrderBy(e => e.Id) |
| | 1 | 55 | | .Skip(cachedEtProducers.Count) |
| | 1 | 56 | | .Take(itemsToLoad) |
| | 1 | 57 | | .ToListAsync(); |
| | | 58 | | |
| | 1 | 59 | | if (additionalEtProducers.Any()) |
| | | 60 | | { |
| | 0 | 61 | | cachedEtProducers.AddRange(additionalEtProducers); |
| | 0 | 62 | | SetCache(CacheKey, cachedEtProducers, TimeSpan.FromMinutes(10)); |
| | | 63 | | } |
| | | 64 | | |
| | 1 | 65 | | _maxPage = Math.Max(_maxPage, page); |
| | 1 | 66 | | _maxItemsToLoad = itemsToLoad; |
| | | 67 | | } |
| | | 68 | | |
| | 1 | 69 | | return cachedEtProducers |
| | 1 | 70 | | .Take(requiredItemCount) |
| | 5 | 71 | | .OrderBy(e => e.Name) |
| | 1 | 72 | | .ToList(); |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | public async Task<List<EtProducer>> Get() |
| | | 76 | | { |
| | 0 | 77 | | var cachedEtProducers = GetCache<List<EtProducer>>(CacheKey); |
| | 0 | 78 | | if (cachedEtProducers != null) |
| | 0 | 79 | | return cachedEtProducers.OrderBy(e => e.Name).ToList(); |
| | | 80 | | |
| | 0 | 81 | | await Update(); |
| | 0 | 82 | | cachedEtProducers = GetCache<List<EtProducer>>(CacheKey); |
| | | 83 | | |
| | 0 | 84 | | if (cachedEtProducers != null) |
| | 0 | 85 | | return cachedEtProducers.OrderBy(e => e.Name).ToList(); |
| | | 86 | | |
| | 0 | 87 | | throw new InvalidOperationException("Данные не найдены в кэше после обновления."); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | public async Task Update() |
| | | 91 | | { |
| | 0 | 92 | | var producers = await _jcEtalonContext.EtProducers |
| | 0 | 93 | | .OrderBy(e => e.Id) |
| | 0 | 94 | | .Take(_maxItemsToLoad) |
| | 0 | 95 | | .ToListAsync(); |
| | 0 | 96 | | SetCache(CacheKey, producers, TimeSpan.FromMinutes(10)); |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | public async Task UpdateCacheIncrementally(int producerId) |
| | | 100 | | { |
| | 0 | 101 | | var updatedProducer = await _jcEtalonContext.EtProducers |
| | 0 | 102 | | .FirstOrDefaultAsync(p => p.Id == producerId); |
| | 0 | 103 | | if (updatedProducer == null) return; |
| | | 104 | | |
| | 0 | 105 | | var cachedEtProducers = GetCache<List<EtProducer>>(CacheKey) ?? new List<EtProducer>(); |
| | 0 | 106 | | var existingProducerIndex = cachedEtProducers.FindIndex(p => p.Id == producerId); |
| | | 107 | | |
| | 0 | 108 | | if (existingProducerIndex != -1) |
| | | 109 | | { |
| | 0 | 110 | | cachedEtProducers[existingProducerIndex] = updatedProducer; |
| | | 111 | | } |
| | | 112 | | else |
| | | 113 | | { |
| | 0 | 114 | | cachedEtProducers.Add(updatedProducer); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | SetCache(CacheKey, cachedEtProducers, TimeSpan.FromMinutes(10)); |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | public async Task RemoveProducerFromCache(int producerId) |
| | | 121 | | { |
| | 0 | 122 | | await Task.CompletedTask; |
| | 0 | 123 | | var cachedEtProducers = GetCache<List<EtProducer>>(CacheKey); |
| | 0 | 124 | | if (cachedEtProducers == null) return; |
| | | 125 | | |
| | 0 | 126 | | cachedEtProducers.RemoveAll(p => p.Id == producerId); |
| | 0 | 127 | | SetCache(CacheKey, cachedEtProducers, TimeSpan.FromMinutes(10)); |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | public async Task RemoveProducersFromCache(int[] producerIds) |
| | | 131 | | { |
| | 0 | 132 | | await Task.CompletedTask; |
| | 0 | 133 | | var cachedEtProducers = GetCache<List<EtProducer>>(CacheKey); |
| | 0 | 134 | | if (cachedEtProducers == null) return; |
| | | 135 | | |
| | 0 | 136 | | cachedEtProducers.RemoveAll(p => producerIds.Contains(p.Id)); |
| | 0 | 137 | | SetCache(CacheKey, cachedEtProducers, TimeSpan.FromMinutes(10)); |
| | 0 | 138 | | } |
| | | 139 | | } |
| | | 140 | | } |