| | | 1 | | using Microsoft.Extensions.Caching.Memory; |
| | | 2 | | using OzonDomains.Models.OrderCarts; |
| | | 3 | | using OzonRepositories.Data; |
| | | 4 | | |
| | | 5 | | namespace Services.CacheServices.Cache.CartCache |
| | | 6 | | { |
| | | 7 | | public class CartCache : IRedisAppCache<OrderCart> |
| | | 8 | | { |
| | | 9 | | private readonly IMemoryCache _memoryCache; |
| | | 10 | | private readonly OrderCartRepository _cartRepository; |
| | | 11 | | private const string CacheKey = "OrderCarts"; |
| | | 12 | | private const string TotalCountCacheKey = "OrderCartTotalCount"; |
| | | 13 | | private const int InitialLoadCount = 10; |
| | | 14 | | private const int IncrementCount = 5; |
| | | 15 | | |
| | 0 | 16 | | public CartCache(IMemoryCache memoryCache, OrderCartRepository cartRepository) |
| | | 17 | | { |
| | 0 | 18 | | _memoryCache = memoryCache; |
| | 0 | 19 | | _cartRepository = cartRepository; |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | public int GetIncrementCount() |
| | | 23 | | { |
| | 0 | 24 | | return IncrementCount; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | private void SetCache(string key, object value, TimeSpan expiration) |
| | | 28 | | { |
| | 0 | 29 | | var options = new MemoryCacheEntryOptions() |
| | 0 | 30 | | .SetAbsoluteExpiration(expiration); |
| | 0 | 31 | | _memoryCache.Set(key, value, options); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | private T? GetCache<T>(string key) |
| | | 35 | | { |
| | 0 | 36 | | return _memoryCache.TryGetValue(key, out T? value) ? value : default; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task<List<OrderCart>> Get(int page) |
| | | 40 | | { |
| | | 41 | | const int pageSize = IncrementCount; |
| | 0 | 42 | | string pageKey = $"{CacheKey}_Page_{page}"; |
| | | 43 | | |
| | 0 | 44 | | var cachedPage = GetCache<List<OrderCart>>(pageKey); |
| | 0 | 45 | | if (cachedPage != null) |
| | 0 | 46 | | return cachedPage; |
| | | 47 | | |
| | 0 | 48 | | var freshData = await _cartRepository.GetWithPagination((page - 1) * pageSize, pageSize); |
| | 0 | 49 | | SetCache(pageKey, freshData, TimeSpan.FromMinutes(10)); |
| | | 50 | | |
| | 0 | 51 | | return freshData; |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | |
| | | 55 | | |
| | | 56 | | public async Task<List<OrderCart>> Get() |
| | | 57 | | { |
| | 0 | 58 | | var cachedCarts = GetCache<List<OrderCart>>(CacheKey); |
| | 0 | 59 | | if (cachedCarts != null) |
| | 0 | 60 | | return cachedCarts.OrderByDescending(c => c.CreatedAt).ToList(); |
| | | 61 | | |
| | 0 | 62 | | await Update(); |
| | 0 | 63 | | return GetCache<List<OrderCart>>(CacheKey) |
| | 0 | 64 | | ?.OrderByDescending(c => c.CreatedAt).ToList() |
| | 0 | 65 | | ?? new List<OrderCart>(); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task Update() |
| | | 69 | | { |
| | | 70 | | const int pageSize = IncrementCount; |
| | 0 | 71 | | string pageKey = $"{CacheKey}_Page_1"; |
| | 0 | 72 | | var freshData = await _cartRepository.GetWithPagination(0, pageSize); |
| | 0 | 73 | | SetCache(pageKey, freshData, TimeSpan.FromMinutes(10)); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public async Task UpdateCacheIncrementally(OrderCart updatedCart) |
| | | 77 | | { |
| | 0 | 78 | | await Task.CompletedTask; |
| | 0 | 79 | | var cachedCarts = GetCache<List<OrderCart>>(CacheKey) ?? new List<OrderCart>(); |
| | 0 | 80 | | var index = cachedCarts.FindIndex(c => c.Id == updatedCart.Id); |
| | | 81 | | |
| | 0 | 82 | | if (index != -1) |
| | 0 | 83 | | cachedCarts[index] = updatedCart; |
| | | 84 | | else |
| | 0 | 85 | | cachedCarts.Add(updatedCart); |
| | | 86 | | |
| | 0 | 87 | | SetCache(CacheKey, cachedCarts, TimeSpan.FromMinutes(10)); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | public async Task RemoveCartFromCache(int cartId) |
| | | 91 | | { |
| | 0 | 92 | | await Task.CompletedTask; |
| | 0 | 93 | | var cachedCarts = GetCache<List<OrderCart>>(CacheKey); |
| | 0 | 94 | | if (cachedCarts == null) return; |
| | | 95 | | |
| | 0 | 96 | | cachedCarts.RemoveAll(c => c.Id == cartId); |
| | 0 | 97 | | SetCache(CacheKey, cachedCarts, TimeSpan.FromMinutes(10)); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | public async Task<int> GetTotalCount() |
| | | 101 | | { |
| | 0 | 102 | | if (_memoryCache.TryGetValue(TotalCountCacheKey, out int cachedCount)) |
| | | 103 | | { |
| | 0 | 104 | | return cachedCount; |
| | | 105 | | } |
| | 0 | 106 | | int count = await _cartRepository.GetTotalCount(); |
| | 0 | 107 | | _memoryCache.Set(TotalCountCacheKey, count, TimeSpan.FromMinutes(10)); |
| | 0 | 108 | | return count; |
| | 0 | 109 | | } |
| | | 110 | | } |
| | | 111 | | } |