public async Task <IEnumerable <CategoryDto> > Handle(GetCategoriesQuery request, CancellationToken cancellationToken) { if (!_memoryCacheManager.TryGetValue(nameof(GetCategoriesQuery), out IEnumerable <CategoryDto> categoryDtos)) { var categories = await _unitOfWork.Categories.GetAllAsync(); categoryDtos = categories.Select(c => new CategoryDto { Id = c.Id, Name = c.Name }); _memoryCacheManager.Set(nameof(GetCategoriesQuery), categoryDtos); } return(categoryDtos); }
public async Task <IEnumerable <CategoryDto> > Handle(GetCategoriesQuery request, CancellationToken cancellationToken) { if (!_memoryCache.TryGetValue(nameof(GetCategoriesQuery), out IEnumerable <CategoryDto> categoryDtos)) { var categories = await _context.Categories.ToListAsync(); var cacheExpiryOptions = new MemoryCacheEntryOptions { AbsoluteExpiration = DateTime.Now.AddMinutes(5), Priority = CacheItemPriority.High, SlidingExpiration = TimeSpan.FromMinutes(2) }; categoryDtos = categories.Select(c => new CategoryDto { Id = c.Id, Name = c.Name }); _memoryCache.Set(nameof(GetCategoriesQuery), categoryDtos, cacheExpiryOptions); } return(categoryDtos); }