< Summary

Information
Class: Services.CacheServices.Cache.OzonOrdersCache.TransactionCache
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/CacheServices/Cache/OzonOrdersCache/TransactionCache.cs
Line coverage
17%
Covered lines: 10
Uncovered lines: 46
Coverable lines: 56
Total lines: 119
Line coverage: 17.8%
Branch coverage
11%
Covered branches: 2
Total branches: 18
Branch coverage: 11.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SetCache(...)100%210%
GetCache(...)0%620%
Get()0%4260%
Get()0%620%
Set()100%210%
UpdateCacheIncrementally()33.33%17633.33%
Update()0%620%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/CacheServices/Cache/OzonOrdersCache/TransactionCache.cs

#LineLine coverage
 1using Microsoft.Extensions.Caching.Memory;
 2using Newtonsoft.Json;
 3using OzonDomains.Models;
 4using Services.DataServices;
 5
 6namespace 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;
 815        private int _maxPage = 1;
 816        private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(10);
 817        private int _maxItemsToLoad = InitialLoadCount;
 18
 819        public TransactionCache(IMemoryCache memoryCache, TransactionDataServices transactionsService)
 20        {
 821            _memoryCache = memoryCache;
 822            _transactionsService = transactionsService;
 823        }
 24
 25        private void SetCache(string key, object value, TimeSpan expiration)
 26        {
 027            var cacheEntryOptions = new MemoryCacheEntryOptions()
 028                .SetAbsoluteExpiration(expiration);
 029            _memoryCache.Set(key, value, cacheEntryOptions);
 030        }
 31
 32        private T? GetCache<T>(string key)
 33        {
 034            return _memoryCache.TryGetValue(key, out T? value) ? value : default;
 35        }
 36
 37        public async Task<List<Transaction>> Get(int page)
 38        {
 039            await Task.CompletedTask;
 040            var cachedTransactions = GetCache<List<Transaction>>(CacheKey);
 041            if (cachedTransactions == null)
 42            {
 043                cachedTransactions = _transactionsService.GetTransactions(0, InitialLoadCount).ToList();
 044                _maxPage = InitialLoadCount / IncrementCount;
 045                SetCache(CacheKey, cachedTransactions, _cacheDuration);
 46            }
 47
 048            int requiredItemCount = page * IncrementCount;
 049            if (cachedTransactions.Count < requiredItemCount)
 50            {
 051                int itemsToLoad = requiredItemCount - cachedTransactions.Count;
 052                List<Transaction> additionalTransactions = _transactionsService.GetTransactions(cachedTransactions.Count
 053                if (additionalTransactions.Any())
 54                {
 055                    cachedTransactions.AddRange(additionalTransactions);
 056                    SetCache(CacheKey, cachedTransactions, _cacheDuration);
 57                }
 058                _maxPage = Math.Max(_maxPage, page);
 059                _maxItemsToLoad = itemsToLoad;
 60            }
 61
 062            return cachedTransactions.Take(requiredItemCount).ToList();
 063        }
 64
 65        public async Task<List<Transaction>> Get()
 66        {
 067            await Task.CompletedTask;
 068            var transactions = GetCache<List<Transaction>>(CacheKey);
 069            if (transactions != null)
 70            {
 071                return transactions.ToList();
 72            }
 73
 074            throw new NotImplementedException("Данные не найдены в кэше");
 075        }
 76
 77        public async Task<List<Transaction>> Set()
 78        {
 079            await Task.CompletedTask;
 080            List<Transaction> newTransactions = _transactionsService.GetTransactions().ToList();
 081            SetCache(CacheKey, newTransactions, _cacheDuration);
 082            return newTransactions;
 083        }
 84
 85        public async Task UpdateCacheIncrementally(int transactionId)
 86        {
 587            var updatedTransaction = await _transactionsService.GetTransaction(transactionId);
 88
 589            if (updatedTransaction != null)
 90            {
 091                var cachedTransactions = GetCache<List<Transaction>>(CacheKey);
 092                if (cachedTransactions != null)
 93                {
 094                    var existingTransactionIndex = cachedTransactions.FindIndex(tr => tr.Id == transactionId);
 95
 096                    if (existingTransactionIndex != -1)
 97                    {
 098                        cachedTransactions[existingTransactionIndex] = updatedTransaction;
 99                    }
 100                    else
 101                    {
 0102                        cachedTransactions.Insert(0, updatedTransaction);
 103                    }
 104
 0105                    SetCache(CacheKey, cachedTransactions, _cacheDuration);
 106                }
 107            }
 5108        }
 109
 110        public async Task Update()
 111        {
 0112            await Task.CompletedTask;
 0113            var existing = GetCache<List<Transaction>>(CacheKey);
 0114            int count = existing != null ? Math.Max(existing.Count, InitialLoadCount) : InitialLoadCount;
 0115            List<Transaction> cachedOrders = _transactionsService.GetTransactions(0, count).ToList();
 0116            SetCache(CacheKey, cachedOrders, _cacheDuration);
 0117        }
 118    }
 119}