示例#1
0
        public void Put(DbCacheKey key, DbCacheEntry value, DbCachingPolicy policy)
        {
            var cacheKey = BuildKey(key.Key);

            using (_cache.AcquireKeyLock(cacheKey))
            {
                _cache.Put(cacheKey, value, new CacheEntryOptions().ExpiresIn(policy.ExpirationTimeout.Value));

                foreach (var set in key.EntitySets)
                {
                    var lookup = GetLookupSet(set);
                    lookup.Add(cacheKey);
                }
            }
        }
示例#2
0
        public virtual DbCacheKey GenerateQueryKey(Expression expression, DbCachingPolicy policy)
        {
            var hash = GetExpressionHash(expression);

            var key = _keysCache.GetOrAdd(hash.CombinedHash, key =>
            {
                var visitor = new DependencyVisitor();
                visitor.ExtractDependencies(expression);

                return(new DbCacheKey
                {
                    Key = hash.CombinedHashString,
                    EntitySets = visitor.Types.Select(x => GenerateDependencyKey(x)).ToArray()
                });
            });

            return(key);
        }
示例#3
0
        /// <summary>
        /// Returns a new query where the result will be cached.
        /// Only untracked entities will be cached.
        /// </summary>
        /// <typeparam name="T">The type of entity being queried.</typeparam>
        /// <param name="source">The source query.</param>
        /// <param name="options">Options how to handle cached query results.</param>
        /// <returns>A new query where the result set will be cached.</returns>
        public static IQueryable <T> AsCaching <T>(this IQueryable <T> source, [NotParameterized] DbCachingPolicy policy)
            where T : BaseEntity
        {
            Guard.NotNull(source, nameof(source));
            Guard.NotNull(policy, nameof(policy));

            if (source.Provider is not CachingQueryProvider)
            {
                // The AsCaching() method expression will result in a LINQ translation error
                // if ef caching is not active.
                return(source);
            }

            return
                (source.Provider.CreateQuery <T>(
                     Expression.Call(
                         instance: null,
                         method: AsCachingMethodInfo.MakeGenericMethod(typeof(T)),
                         arg0: source.Expression,
                         arg1: Expression.Constant(policy))));
        }
示例#4
0
 public DbCacheEntry Get(DbCacheKey key, DbCachingPolicy policy)
 {
     return(_cache.Get <DbCacheEntry>(BuildKey(key.Key)));
 }