/// <summary> /// Executes the query represented by a specified expression tree to cache its results. /// </summary> /// <param name="expression">An expression tree that represents a LINQ query.</param> /// <param name="materializer">How to run the query.</param> /// <returns>The value that results from executing the specified query.</returns> public object Materialize(Expression expression, Func <object> materializer) { var cacheKey = _cacheKeyProvider.GetEFCacheKey( _query, expression, _efCachePolicy.KeyHashPrefix, _efCachePolicy.SaltKey); _debugInfo.EFCacheKey = cacheKey; var queryCacheKey = cacheKey.KeyHash; var result = _cacheServiceProvider.GetValue(queryCacheKey); if (result != null) { _debugInfo.IsCacheHit = true; return(result); } result = materializer(); _cacheServiceProvider.StoreRootCacheKeys(cacheKey.CacheDependencies); if (_efCachePolicy.AbsoluteExpiration == null) { _efCachePolicy.AbsoluteExpiration = DateTime.Now.AddMinutes(20); } _cacheServiceProvider.InsertValue( queryCacheKey, result, cacheKey.CacheDependencies, _efCachePolicy.AbsoluteExpiration.Value, _efCachePolicy.Priority); return(result); }
public void TestCacheInvalidationWithTwoRoots() { _cacheService.StoreRootCacheKeys(new[] { "entity1.model", "entity2.model" }); _cacheService.InsertValue("EF_key1", "value1", new[] { "entity1.model", "entity2.model" }, DateTime.Now.AddMinutes(10)); _cacheService.StoreRootCacheKeys(new[] { "entity1.model", "entity2.model" }); _cacheService.InsertValue("EF_key2", "value2", new[] { "entity1.model", "entity2.model" }, DateTime.Now.AddMinutes(10)); var value1 = _cacheService.GetValue("EF_key1"); Assert.IsNotNull(value1); var value2 = _cacheService.GetValue("EF_key2"); Assert.IsNotNull(value2); _cacheService.InvalidateCacheDependencies(new[] { "entity2.model" }); value1 = _cacheService.GetValue("EF_key1"); Assert.IsNull(value1); value2 = _cacheService.GetValue("EF_key2"); Assert.IsNull(value2); var keys = _cacheService.GetAllEFCachedKeys(); var key1 = keys.FirstOrDefault(key => key == "EF_key1"); Assert.IsNull(key1); Assert.AreEqual(0, keys.Count); }