/// <summary> /// Asyncronously returns all key-value cache stores associated with a group /// Returns null if there is no match with the group parameter /// Refrains from using this if possible, as this queries the whole keyspace and may cause block in server /// Use GetCacheAsync instead when possible /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cache"></param> /// <param name="group"></param> /// <param name="token"></param> /// <returns></returns> public static async Task <T[]> GetCacheGroupAsync <T>(this IDistributedCache cache, string group, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); string[] keyList = GetKeys(group); if (keyList == null || keyList.Length == 0) { return(null); } T[] result = new T[keyList.Length]; for (int i = 0; i < result.Length; ++i) { result[i] = await cache.GetCacheAsync <T>(keyList[i]); } return(result); }
public async Task <IReadOnlyCollection <ObjectVm> > Handle(ObjectQuery query, CancellationToken cancellationToken) { var cacheKey = $"objets-all"; var objectsFromCache = await _distributedCache .GetCacheAsync <IReadOnlyCollection <ObjectVm> >(cacheKey); if (objectsFromCache != null) { return(objectsFromCache); } var objectsFromDb = await _objectService.GetObjectsAsync(); await _distributedCache .SetCacheAsStringAsync(cacheKey, objectsFromDb, 30); return(objectsFromDb); }
public async Task <IReadOnlyCollection <DataFieldVm> > Handle(DataFieldsQuery query, CancellationToken cancellationToken) { var cacheKey = $"datafields-all"; var dataFieldssFromCache = await _distributedCache .GetCacheAsync <IReadOnlyCollection <DataFieldVm> >(cacheKey); if (dataFieldssFromCache != null) { return(dataFieldssFromCache); } var readingsFromDb = await _dataFieldService.GetDataFieldsAsync(); await _distributedCache .SetCacheAsStringAsync(cacheKey, readingsFromDb, 30); return(readingsFromDb); }
public async Task <IReadOnlyCollection <BuildingVm> > Handle(BuildingQuery query, CancellationToken cancellationToken) { var cacheKey = $"buildings-all"; var buildingsFromCache = await _distributedCache .GetCacheAsync <IReadOnlyCollection <BuildingVm> >(cacheKey); if (buildingsFromCache != null) { return(buildingsFromCache); } var readingsFromDb = await _buildingService.GetBuildingsAsync(); await _distributedCache .SetCacheAsStringAsync(cacheKey, readingsFromDb, 30); return(readingsFromDb); }
public async Task <IReadOnlyCollection <ReadingVm> > Handle(ReadingQuery args, CancellationToken cancellationToken) { args.ToDate = args.ToDate.ToEndOfTheDate(); var cacheKey = $"reading-{args.BuildingId}-{args.ObjectId}-{args.DataFieldId}-{args.FromDate}-{args.ToDate}"; var readingsFromCache = await _distributedCache .GetCacheAsync <IReadOnlyCollection <ReadingVm> >(cacheKey); if (readingsFromCache != null) { return(readingsFromCache); } var readingsFromDb = await _readingService .GetReadingsAsync(args); await _distributedCache .SetCacheAsStringAsync(cacheKey, readingsFromDb, 30); return(readingsFromDb); }
public async Task <ActionResult <ProductDTO> > GetProductsAsync([FromQuery] ProductQueryDTO productQueryDTO) { if (productQueryDTO == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var projectQuery = _mapper.Map <ProductQuery>(productQueryDTO); var cacheKey = $"{nameof(TestController)}_{nameof(GetProductsAsync)}_{Request.QueryString.Value}"; var gotCache = await _cache.GetCacheAsync <Product>("notExist"); Console.WriteLine(gotCache is null); // await _cache.CreateCacheAsync<Product>( // "notExist", // async () => await Task<string>.Run(() => { return new Product { PId = Guid.NewGuid() }; }), // options => options.SetSlidingExpiration(TimeSpan.FromSeconds(15))); await _cache.CreateCacheAsync <Product>( "notExist", () => { return(new Product { PId = Guid.NewGuid() }); }, options => options.SetSlidingExpiration(TimeSpan.FromSeconds(15))); gotCache = await _cache.GetCacheAsync <Product>("notExist"); Console.WriteLine(gotCache is null); // // sync type // var syncProduct = // await _cache.CreateOrGetCacheAsync<IEnumerable<Product>>("syncProduct", // () => _repository.GetProductsSync(projectQuery), // options => options.SetSlidingExpiration(TimeSpan.FromSeconds(15))); // Console.WriteLine("----------- sync project" + syncProduct.First().Description); var pagedProducts = await _cache.CreateOrGetCacheAsync <PagedListBase <Product> >(cacheKey, async() => await _repository.GetProducts(projectQuery), options => options.SetSlidingExpiration(TimeSpan.FromSeconds(15))); var filterProps = new Dictionary <string, object>(); filterProps.Add("name", projectQuery.Name); filterProps.Add("description", projectQuery.Description); Response.SetPaginationHead(pagedProducts, projectQuery, filterProps, values => _generator.GetUriByAction(HttpContext, controller: "Product", action: "GetProducts", values: values), meta => JsonSerializer.Serialize(meta, new JsonSerializerOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }) ); var mappedProducts = _mapper.Map <IEnumerable <ProductDTO> >(pagedProducts); return(Ok(mappedProducts.ToDynamicIEnumerable(projectQuery.Fields))); }