< Summary

Information
Class: TreeGrouping.Application.CategoryService.CategoryCacheService
Assembly: TreeGrouping.Application
File(s): /home/runner/work/Studio2Prod/Studio2Prod/TreeGrouping.Application/CategoryService/CategoryCacheService.cs
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 35
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetCategoriesFromCache()100%22100%
UpdateCache()100%11100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/TreeGrouping.Application/CategoryService/CategoryCacheService.cs

#LineLine coverage
 1using ConsoleApp1;
 2using Microsoft.Extensions.Caching.Memory;
 3using TreeGrouping.Application.DbService;
 4using TreeGrouping.Application.DbService.Models;
 5
 6namespace TreeGrouping.Application.CategoryService;
 7
 8public class CategoryCacheService
 9{
 10    private readonly IDatabaseService _dbService;
 11    private readonly IMemoryCache _cache;
 312    private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(30);
 13
 314    public CategoryCacheService(IDatabaseService dbService, IMemoryCache cache)
 15    {
 316        _dbService = dbService;
 317        _cache = cache;
 318    }
 19
 20    public async Task<List<CategoryModel>> GetCategoriesFromCache(string cacheKey, StoredProcedureType storedProcedureTy
 21    {
 222        if (!_cache.TryGetValue(cacheKey, out List<CategoryModel> categories))
 23        {
 124            categories = (await _dbService.ExecuteStoredProcedureAsync(storedProcedureType)).ToList();
 125            _cache.Set(cacheKey, categories, _cacheDuration);
 26        }
 227        return categories;
 228    }
 29    public async Task<List<CategoryModel>> UpdateCache(string cacheKey, StoredProcedureType storedProcedureType)
 30    {
 131        var updatedCategories = (await _dbService.ExecuteStoredProcedureAsync(storedProcedureType)).ToList();
 132        _cache.Set(cacheKey, updatedCategories, _cacheDuration);
 133        return updatedCategories;
 134    }
 35}