< Summary

Information
Class: Services.CacheServices.Cache.CartCache.CartCache
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/CacheServices/Cache/CartCache/CartCache.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 50
Coverable lines: 50
Total lines: 111
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
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%
GetIncrementCount()100%210%
SetCache(...)100%210%
GetCache(...)0%620%
Get()0%620%
Get()0%4260%
Update()100%210%
UpdateCacheIncrementally()0%2040%
RemoveCartFromCache()0%620%
GetTotalCount()0%620%

File(s)

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

#LineLine coverage
 1using Microsoft.Extensions.Caching.Memory;
 2using OzonDomains.Models.OrderCarts;
 3using OzonRepositories.Data;
 4
 5namespace 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
 016        public CartCache(IMemoryCache memoryCache, OrderCartRepository cartRepository)
 17        {
 018            _memoryCache = memoryCache;
 019            _cartRepository = cartRepository;
 020        }
 21
 22        public int GetIncrementCount()
 23        {
 024            return IncrementCount;
 25        }
 26
 27        private void SetCache(string key, object value, TimeSpan expiration)
 28        {
 029            var options = new MemoryCacheEntryOptions()
 030                .SetAbsoluteExpiration(expiration);
 031            _memoryCache.Set(key, value, options);
 032        }
 33
 34        private T? GetCache<T>(string key)
 35        {
 036            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;
 042            string pageKey = $"{CacheKey}_Page_{page}";
 43
 044            var cachedPage = GetCache<List<OrderCart>>(pageKey);
 045            if (cachedPage != null)
 046                return cachedPage;
 47
 048            var freshData = await _cartRepository.GetWithPagination((page - 1) * pageSize, pageSize);
 049            SetCache(pageKey, freshData, TimeSpan.FromMinutes(10));
 50
 051            return freshData;
 052        }
 53
 54
 55
 56        public async Task<List<OrderCart>> Get()
 57        {
 058            var cachedCarts = GetCache<List<OrderCart>>(CacheKey);
 059            if (cachedCarts != null)
 060                return cachedCarts.OrderByDescending(c => c.CreatedAt).ToList();
 61
 062            await Update();
 063            return GetCache<List<OrderCart>>(CacheKey)
 064                ?.OrderByDescending(c => c.CreatedAt).ToList()
 065                ?? new List<OrderCart>();
 066        }
 67
 68        public async Task Update()
 69        {
 70            const int pageSize = IncrementCount;
 071            string pageKey = $"{CacheKey}_Page_1";
 072            var freshData = await _cartRepository.GetWithPagination(0, pageSize);
 073            SetCache(pageKey, freshData, TimeSpan.FromMinutes(10));
 074        }
 75
 76        public async Task UpdateCacheIncrementally(OrderCart updatedCart)
 77        {
 078            await Task.CompletedTask;
 079            var cachedCarts = GetCache<List<OrderCart>>(CacheKey) ?? new List<OrderCart>();
 080            var index = cachedCarts.FindIndex(c => c.Id == updatedCart.Id);
 81
 082            if (index != -1)
 083                cachedCarts[index] = updatedCart;
 84            else
 085                cachedCarts.Add(updatedCart);
 86
 087            SetCache(CacheKey, cachedCarts, TimeSpan.FromMinutes(10));
 088        }
 89
 90        public async Task RemoveCartFromCache(int cartId)
 91        {
 092            await Task.CompletedTask;
 093            var cachedCarts = GetCache<List<OrderCart>>(CacheKey);
 094            if (cachedCarts == null) return;
 95
 096            cachedCarts.RemoveAll(c => c.Id == cartId);
 097            SetCache(CacheKey, cachedCarts, TimeSpan.FromMinutes(10));
 098        }
 99
 100        public async Task<int> GetTotalCount()
 101        {
 0102            if (_memoryCache.TryGetValue(TotalCountCacheKey, out int cachedCount))
 103            {
 0104                return cachedCount;
 105            }
 0106            int count = await _cartRepository.GetTotalCount();
 0107            _memoryCache.Set(TotalCountCacheKey, count, TimeSpan.FromMinutes(10));
 0108            return count;
 0109        }
 110    }
 111}