Commit edac6272 authored by Mário Hatalčík's avatar Mário Hatalčík
Browse files

remove caching from product details

parent eb502289
Loading
Loading
Loading
Loading
Loading
+4 −16
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ public class ProductService(
    private string CacheKeyFiltered(ProductFilterDto productFilter) => $"{_cacheKeyPrefix}-product{productFilter.GetHashCode()}";
    private string CacheKeyFilteredDetail(ProductFilterDto productFilter) => $"{_cacheKeyPrefix}-productDetail{productFilter.GetHashCode()}";
    private string CacheKeyProduct(int id) => $"{_cacheKeyPrefix}-product{id}";
    private string CacheKeyProductDetail(int id) => $"{_cacheKeyPrefix}-productDetail{id}";

    public async Task<ProductDto?> CreateProductAsync(ProductDto productDto)
    {
@@ -97,27 +96,18 @@ public class ProductService(

    public async Task<ProductDetailDto?> GetProductDetailByIdAsync(int id)
    {
        var cacheKey = CacheKeyProductDetail(id);
        if (!memoryCache.TryGetValue(cacheKey, out Product? value))
        {
            value = await productRepository.GetByIdAsync(id, nameof(Product.Manufacturer),
        var value = await productRepository.GetByIdAsync(id, nameof(Product.Manufacturer),
            nameof(Product.Reviews), $"{nameof(Product.Reviews)}.{nameof(Review.User)}", nameof(Product.Tags));

        if (value is null) return null;

        value.Reviews = value.Reviews.OrderByDescending(r => r.CreatedAt).ToList();
            var cacheEntryOptions = new MemoryCacheEntryOptions()
                .SetAbsoluteExpiration(TimeSpan.FromSeconds(30));
            memoryCache.Set(cacheKey, value, cacheEntryOptions);
        }

        return mapper.Map<ProductDetailDto>(value);
    }

    public async Task<ProductDto?> UpdateProductAsync(ProductDto productDto, int userId)
    {
        var cacheKeyDetail = CacheKeyProductDetail(productDto.Id);
        memoryCache.Remove(cacheKeyDetail);
        var cacheKey1 = CacheKeyProduct(productDto.Id);
        memoryCache.Remove(cacheKey1);
        var oldProduct = await productUnitOfWork.ProductRepository.GetByIdAsync(productDto.Id);
@@ -139,8 +129,6 @@ public class ProductService(

    public Task<bool> DeleteProductByIdAsync(int id)
    {
        var cacheKeyDetail = CacheKeyProductDetail(id);
        memoryCache.Remove(cacheKeyDetail);
        var cacheKey1 = CacheKeyProduct(id);
        memoryCache.Remove(cacheKey1);
        return productRepository.DeleteAsync(id);