public async Task <IActionResult> GetOrders([FromQuery] OrderQueryDto queryDto) { IEnumerable <OrderViewModel> orders = default; if (!queryDto.Cacheable) { orders = await _mediator.Send(queryDto); return(Ok(orders)); } var cacheKey = $"order-{queryDto.CustomerId}-{queryDto.OrderId}-{queryDto.DeliveredAt}-{queryDto.OrderedAt}-{queryDto.IsDelivered}-{queryDto.Cacheable}"; orders = await _redisCacheClient.GetAsync <IEnumerable <OrderViewModel> >(cacheKey); if (orders != null) { Response.Headers.Add("X-DataSource", $"From-Cache"); return(Ok(orders)); } orders = await _mediator.Send(queryDto); if (orders.Any()) { await _redisCacheClient.AddAsync(cacheKey, orders, 300); } return(Ok(orders)); }
public async Task <IActionResult> GetProducts([FromQuery] ProductQueryDto queryDto) { IEnumerable <ProductViewModel> products = default; if (!queryDto.Cacheable) { products = await _mediator.Send(queryDto); return(Ok(products)); } var cacheKey = $"product-{queryDto.ProductName}-{queryDto.CategoryId}"; products = await _redisCacheClient.GetAsync <IEnumerable <ProductViewModel> >(cacheKey); if (products != null) { Response.Headers.Add("X-DataSource", $"From-Cache"); return(Ok(products)); } if (products.Any()) { await _redisCacheClient.AddAsync(cacheKey, products, 300); } return(Ok(products)); }
public async Task <IActionResult> GetOrders([FromQuery] SalesQueryDto queryDto) { var cacheKey = $"sales-{queryDto.CustomerId}-{queryDto.Reference}-{queryDto.SoldAt}-{queryDto.OperatedBy}"; var salesFromCache = await _redisCacheClient.GetAsync <IEnumerable <SalesViewModel> >(cacheKey); if (salesFromCache != null) { Response.Headers.Add("X-DataSource", $"From-Cache"); return(Ok(salesFromCache)); } var salesFromDb = await _mediator.Send(queryDto); if (salesFromDb.Any()) { await _redisCacheClient.AddAsync(cacheKey, salesFromDb, 300); } return(Ok(salesFromDb)); }
public static void SetCache(string key, object value) { _cache.AddAsync(key, value); }