| | | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | | 2 | | using Newtonsoft.Json; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using Services.DataServices; |
| | | 5 | | |
| | | 6 | | namespace Services.CacheServices.Cache.OzonOrdersCache |
| | | 7 | | { |
| | | 8 | | public class TransactionCache : IRedisAppCache<Transaction> |
| | | 9 | | { |
| | | 10 | | private readonly IMemoryCache _memoryCache; |
| | | 11 | | private readonly TransactionDataServices _transactionsService; |
| | | 12 | | private const string CacheKey = "Transactions"; |
| | | 13 | | private const int InitialLoadCount = 100; |
| | | 14 | | private const int IncrementCount = 30; |
| | 8 | 15 | | private int _maxPage = 1; |
| | 8 | 16 | | private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(10); |
| | 8 | 17 | | private int _maxItemsToLoad = InitialLoadCount; |
| | | 18 | | |
| | 8 | 19 | | public TransactionCache(IMemoryCache memoryCache, TransactionDataServices transactionsService) |
| | | 20 | | { |
| | 8 | 21 | | _memoryCache = memoryCache; |
| | 8 | 22 | | _transactionsService = transactionsService; |
| | 8 | 23 | | } |
| | | 24 | | |
| | | 25 | | private void SetCache(string key, object value, TimeSpan expiration) |
| | | 26 | | { |
| | 0 | 27 | | var cacheEntryOptions = new MemoryCacheEntryOptions() |
| | 0 | 28 | | .SetAbsoluteExpiration(expiration); |
| | 0 | 29 | | _memoryCache.Set(key, value, cacheEntryOptions); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | private T? GetCache<T>(string key) |
| | | 33 | | { |
| | 0 | 34 | | return _memoryCache.TryGetValue(key, out T? value) ? value : default; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<List<Transaction>> Get(int page) |
| | | 38 | | { |
| | 0 | 39 | | await Task.CompletedTask; |
| | 0 | 40 | | var cachedTransactions = GetCache<List<Transaction>>(CacheKey); |
| | 0 | 41 | | if (cachedTransactions == null) |
| | | 42 | | { |
| | 0 | 43 | | cachedTransactions = _transactionsService.GetTransactions(0, InitialLoadCount).ToList(); |
| | 0 | 44 | | _maxPage = InitialLoadCount / IncrementCount; |
| | 0 | 45 | | SetCache(CacheKey, cachedTransactions, _cacheDuration); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | int requiredItemCount = page * IncrementCount; |
| | 0 | 49 | | if (cachedTransactions.Count < requiredItemCount) |
| | | 50 | | { |
| | 0 | 51 | | int itemsToLoad = requiredItemCount - cachedTransactions.Count; |
| | 0 | 52 | | List<Transaction> additionalTransactions = _transactionsService.GetTransactions(cachedTransactions.Count |
| | 0 | 53 | | if (additionalTransactions.Any()) |
| | | 54 | | { |
| | 0 | 55 | | cachedTransactions.AddRange(additionalTransactions); |
| | 0 | 56 | | SetCache(CacheKey, cachedTransactions, _cacheDuration); |
| | | 57 | | } |
| | 0 | 58 | | _maxPage = Math.Max(_maxPage, page); |
| | 0 | 59 | | _maxItemsToLoad = itemsToLoad; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | return cachedTransactions.Take(requiredItemCount).ToList(); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | public async Task<List<Transaction>> Get() |
| | | 66 | | { |
| | 0 | 67 | | await Task.CompletedTask; |
| | 0 | 68 | | var transactions = GetCache<List<Transaction>>(CacheKey); |
| | 0 | 69 | | if (transactions != null) |
| | | 70 | | { |
| | 0 | 71 | | return transactions.ToList(); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | throw new NotImplementedException("Данные не найдены в кэше"); |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | public async Task<List<Transaction>> Set() |
| | | 78 | | { |
| | 0 | 79 | | await Task.CompletedTask; |
| | 0 | 80 | | List<Transaction> newTransactions = _transactionsService.GetTransactions().ToList(); |
| | 0 | 81 | | SetCache(CacheKey, newTransactions, _cacheDuration); |
| | 0 | 82 | | return newTransactions; |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public async Task UpdateCacheIncrementally(int transactionId) |
| | | 86 | | { |
| | 5 | 87 | | var updatedTransaction = await _transactionsService.GetTransaction(transactionId); |
| | | 88 | | |
| | 5 | 89 | | if (updatedTransaction != null) |
| | | 90 | | { |
| | 0 | 91 | | var cachedTransactions = GetCache<List<Transaction>>(CacheKey); |
| | 0 | 92 | | if (cachedTransactions != null) |
| | | 93 | | { |
| | 0 | 94 | | var existingTransactionIndex = cachedTransactions.FindIndex(tr => tr.Id == transactionId); |
| | | 95 | | |
| | 0 | 96 | | if (existingTransactionIndex != -1) |
| | | 97 | | { |
| | 0 | 98 | | cachedTransactions[existingTransactionIndex] = updatedTransaction; |
| | | 99 | | } |
| | | 100 | | else |
| | | 101 | | { |
| | 0 | 102 | | cachedTransactions.Insert(0, updatedTransaction); |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | SetCache(CacheKey, cachedTransactions, _cacheDuration); |
| | | 106 | | } |
| | | 107 | | } |
| | 5 | 108 | | } |
| | | 109 | | |
| | | 110 | | public async Task Update() |
| | | 111 | | { |
| | 0 | 112 | | await Task.CompletedTask; |
| | 0 | 113 | | var existing = GetCache<List<Transaction>>(CacheKey); |
| | 0 | 114 | | int count = existing != null ? Math.Max(existing.Count, InitialLoadCount) : InitialLoadCount; |
| | 0 | 115 | | List<Transaction> cachedOrders = _transactionsService.GetTransactions(0, count).ToList(); |
| | 0 | 116 | | SetCache(CacheKey, cachedOrders, _cacheDuration); |
| | 0 | 117 | | } |
| | | 118 | | } |
| | | 119 | | } |