Exemplo n.º 1
0
        /// <summary>
        ///     Return the result of the <paramref name="query" /> from the cache. If the query is not cached
        ///     yet, the query is materialized and cached before being returned.
        /// </summary>
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="query">The query to cache in the QueryCacheManager.</param>
        /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
        /// <param name="tags">
        ///     A variable-length parameters list containing tags to expire cached
        ///     entries.
        /// </param>
        /// <returns>The result of the query.</returns>
        public static T FromCache <T>(this QueryDeferred <T> query, DateTimeOffset absoluteExpiration, params string[] tags)
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(query.Execute());
            }

            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.GetDeferred(key);

            if (item == null)
            {
#if EF6
                using (var handler = new QueryCacheItemTracker().Initialize(query))
                {
                    item = query.Execute();

                    item = QueryCacheManager.AddOrGetExistingDeferred <T>(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
                    QueryCacheManager.AddCacheTag(handler, key, tags);
                }
#else
                item = query.Execute();

                item = QueryCacheManager.AddOrGetExistingDeferred <T>(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
#endif
            }

            item = item.IfDbNullThenNull();

            return((T)item);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Return the result of the <paramref name="query" /> from the cache. If the query is not cached
        ///     yet, the query is materialized asynchronously and cached before being returned.
        /// </summary>
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="query">The query to cache in the QueryCacheManager.</param>
        /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="tags">
        ///     A variable-length parameters list containing tags to expire cached
        ///     entries.
        /// </param>
        /// <returns>The result of the query.</returns>
        public static async Task <T> FromCacheAsync <T>(this QueryDeferred <T> query, DateTimeOffset absoluteExpiration, CancellationToken cancellationToken = default(CancellationToken), params string[] tags)
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(await query.ExecuteAsync(cancellationToken).ConfigureAwait(false));
            }

            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.GetDeferred(key);

            if (item == null)
            {
                using (var handler = new QueryCacheItemTracker().Initialize(query))
                {
                    item = await query.ExecuteAsync(cancellationToken).ConfigureAwait(false);

                    item = QueryCacheManager.AddOrGetExistingDeferred <T>(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
                    QueryCacheManager.AddCacheTag(handler, key, tags);
                    QueryCacheManager.AddCacheTag(key, typeof(T).Name + QueryCacheManager.CacheTypeSuffix);
                }
            }

            item = item.IfDbNullThenNull();

            return((T)item);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Return the result of the <paramref name="query" /> from the cache. If the query is not cached
        ///     yet, the query is materialized and cached before being returned.
        /// </summary>
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="query">The query to cache in the QueryCacheManager.</param>
        /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
        /// <param name="tags">
        ///     A variable-length parameters list containing tags to expire cached
        ///     entries.
        /// </param>
        /// <returns>The result of the query.</returns>
        public static IEnumerable <T> FromCache <T>(this IQueryable <T> query, DateTimeOffset absoluteExpiration, params string[] tags) where T : class
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(query.AsNoTracking().ToList());
            }

            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Get <List <T> >(key);

            if (item == null)
            {
#if EF6
                using (var handler = new QueryCacheItemTracker().Initialize(query))
                {
                    item = query.AsNoTracking().ToList();
                    item = QueryCacheManager.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
                    QueryCacheManager.AddCacheTag(handler, key, tags);
                }
#else
                item = query.AsNoTracking().ToList();
                item = QueryCacheManager.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
#endif
            }

            return((IEnumerable <T>)item);
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Return the result of the <paramref name="query" /> from the cache. If the query is not cached
        ///     yet, the query is materialized asynchronously and cached before being returned.
        /// </summary>
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="query">The query to cache in the QueryCacheManager.</param>
        /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="tags">
        ///     A variable-length parameters list containing tags to expire cached
        ///     entries.
        /// </param>
        /// <returns>The result of the query.</returns>
        public static async Task <IEnumerable <T> > FromCacheAsync <T>(this IQueryable <T> query, DateTimeOffset absoluteExpiration, CancellationToken cancellationToken = default(CancellationToken), params string[] tags) where T : class
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false));
            }

            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Get <List <T> >(key);

            if (item == null)
            {
                using (var handler = new QueryCacheItemTracker().Initialize(query))
                {
                    item = await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);

                    item = QueryCacheManager.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
                    QueryCacheManager.AddCacheTag(handler, key, tags);
                    QueryCacheManager.AddCacheTag(key, typeof(T).Name + QueryCacheManager.CacheTypeSuffix);
                }
            }

            return((IEnumerable <T>)item);
        }
        internal static void AddCacheTag(QueryCacheItemTracker handler, string cacheKey, params string[] tags)
        {
            if (IsAutoExpireCacheEnabled && handler.HookAdded)
            {
                var model = handler.Context.GetDbContext().GetModel();

                var types          = handler.MaterializedEntities.Select(x => ObjectContext.GetObjectType(x.GetType())).Distinct().ToList();
                var entitySetNames = new List <string>();

                foreach (var type in types)
                {
                    var conceptual = model.ConceptualModel.EntityTypes.FirstOrDefault(x => x.Name == type.Name);
                    if (conceptual == null)
                    {
                        // we don't care, the cache will simply not be reset
                        continue;
                    }

                    entitySetNames.Add(conceptual.EntitySet.Name);
                }

                if (tags == null)
                {
                    tags = new string[0];
                }

                var tagsList = tags.ToList();
                tagsList.AddRange(entitySetNames.Select(x => PrefixTagSet + x));
                tags = tagsList.ToArray();
            }

            AddCacheTag(cacheKey, tags);
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Return the result of the <paramref name="query" /> from the cache. If the query is not cached
        ///     yet, the query is materialized and cached before being returned.
        /// </summary>
        /// <typeparam name="T">The generic type of the query.</typeparam>
        /// <param name="query">The query to cache in the QueryCacheManager.</param>
        /// <param name="policy">The policy to use to cache the query.</param>
        /// <param name="tags">
        ///     A variable-length parameters list containing tags to expire cached
        ///     entries.
        /// </param>
        /// <returns>The result of the query.</returns>
        public static IEnumerable <T> FromCache <T>(this IQueryable <T> query, CacheItemPolicy policy, params string[] tags) where T : class
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(query.AsNoTracking().ToList());
            }
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Get <List <T> >(key);

            if (item == null)
            {
#if EF6
                using (var handler = new QueryCacheItemTracker().Initialize(query))
                {
                    item = query.AsNoTracking().ToList();
                    item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item;
                    QueryCacheManager.AddCacheTag(handler, key, tags);
                    QueryCacheManager.AddCacheTag(key, typeof(T).Name + QueryCacheManager.CacheTypeSuffix);
                }
#else
                item = query.AsNoTracking().ToList();
                item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
                QueryCacheManager.AddCacheTag(key, typeof(T).Name + QueryCacheManager.CacheTypeSuffix);
#endif
            }

            return((IEnumerable <T>)item);
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Return the result of the <paramref name="query" /> from the cache. If the query is not cached
        ///     yet, the query is materialized and cached before being returned.
        /// </summary>
        /// <typeparam name="T">The generic type of the query.</typeparam>
        /// <param name="query">The query to cache in the QueryCacheManager.</param>
        /// <param name="policy">The policy to use to cache the query.</param>
        /// <param name="tags">
        ///     A variable-length parameters list containing tags to expire cached
        ///     entries.
        /// </param>
        /// <returns>The result of the query.</returns>
        public static T FromCache <T>(this QueryDeferred <T> query, CacheItemPolicy policy, params string[] tags)
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(query.Execute());
            }

            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.GetDeferred(key);

            if (item == null)
            {
#if EF6
                using (var handler = new QueryCacheItemTracker().Initialize(query))
                {
                    item = query.Execute();

                    item = QueryCacheManager.AddOrGetExistingDeferred <T>(key, item ?? DBNull.Value, policy) ?? item;
                    QueryCacheManager.AddCacheTag(handler, key, tags);
                    QueryCacheManager.AddCacheTag(key, typeof(T).Name + QueryCacheManager.CacheTypeSuffix);
                }
#else
                item = query.Execute();

                item = QueryCacheManager.AddOrGetExistingDeferred <T>(key, item ?? DBNull.Value, policy) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
                QueryCacheManager.AddCacheTag(key, typeof(T).Name + QueryCacheManager.CacheTypeSuffix);
#endif
            }

            item = item.IfDbNullThenNull();

            return((T)item);
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Return the result of the <paramref name="query" /> from the cache. If the query is not cached yet, the query
        ///     is materialized and cached before being returned.
        /// </summary>
        /// <typeparam name="T">The generic type of the query.</typeparam>
        /// <param name="query">The query to cache the result.</param>
        /// <param name="policy">The eviction details for the cached result.</param>
        /// <param name="tags">(Optional) The tag list that can be used to expire cached result.</param>
        /// <returns>The result of the query.</returns>
        public static IEnumerable <T> FromCache <T>(this DbRawSqlQuery <T> query, CacheItemPolicy policy, params string[] tags) where T : class
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(query.ToList());
            }
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Get <List <T> >(key);

            if (item == null)
            {
                using (var handler = new QueryCacheItemTracker().Initialize(query))
                {
                    item = query.ToList();
                    item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item;
                    QueryCacheManager.AddCacheTag(handler, key, tags);
                }
            }

            return((IEnumerable <T>)item);
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Return the result of the <paramref name="query" /> from the cache. If the query is not cached yet, the query
        ///     is materialized and cached before being returned.
        /// </summary>
        /// <typeparam name="T">The generic type of the query.</typeparam>
        /// <param name="query">The query to cache the result.</param>
        /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
        /// <param name="tags">(Optional) The tag list that can be used to expire cached result.</param>
        /// <returns>The result of the query.</returns>
        public static IEnumerable <T> FromCache <T>(this DbRawSqlQuery <T> query, DateTimeOffset absoluteExpiration, params string[] tags) where T : class
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(query.ToList());
            }

            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Get <List <T> >(key);

            if (item == null)
            {
                using (var handler = new QueryCacheItemTracker().Initialize(query))
                {
                    item = query.ToList();
                    item = QueryCacheManager.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
                    QueryCacheManager.AddCacheTag(handler, key, tags);
                    QueryCacheManager.AddCacheTag(key, typeof(T).Name + QueryCacheManager.CacheTypeSuffix);
                }
            }

            return((IEnumerable <T>)item);
        }