| | | 1 | | using ConsoleApp1; |
| | | 2 | | using Microsoft.Extensions.Caching.Memory; |
| | | 3 | | using TreeGrouping.Application.DbService; |
| | | 4 | | using TreeGrouping.Application.DbService.Models; |
| | | 5 | | |
| | | 6 | | namespace TreeGrouping.Application.CategoryService; |
| | | 7 | | |
| | | 8 | | public class CategoryCacheService |
| | | 9 | | { |
| | | 10 | | private readonly IDatabaseService _dbService; |
| | | 11 | | private readonly IMemoryCache _cache; |
| | 3 | 12 | | private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(30); |
| | | 13 | | |
| | 3 | 14 | | public CategoryCacheService(IDatabaseService dbService, IMemoryCache cache) |
| | | 15 | | { |
| | 3 | 16 | | _dbService = dbService; |
| | 3 | 17 | | _cache = cache; |
| | 3 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<List<CategoryModel>> GetCategoriesFromCache(string cacheKey, StoredProcedureType storedProcedureTy |
| | | 21 | | { |
| | 2 | 22 | | if (!_cache.TryGetValue(cacheKey, out List<CategoryModel> categories)) |
| | | 23 | | { |
| | 1 | 24 | | categories = (await _dbService.ExecuteStoredProcedureAsync(storedProcedureType)).ToList(); |
| | 1 | 25 | | _cache.Set(cacheKey, categories, _cacheDuration); |
| | | 26 | | } |
| | 2 | 27 | | return categories; |
| | 2 | 28 | | } |
| | | 29 | | public async Task<List<CategoryModel>> UpdateCache(string cacheKey, StoredProcedureType storedProcedureType) |
| | | 30 | | { |
| | 1 | 31 | | var updatedCategories = (await _dbService.ExecuteStoredProcedureAsync(storedProcedureType)).ToList(); |
| | 1 | 32 | | _cache.Set(cacheKey, updatedCategories, _cacheDuration); |
| | 1 | 33 | | return updatedCategories; |
| | 1 | 34 | | } |
| | | 35 | | } |