/// <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);
        }
        /// <summary>Gets query cache unique key.</summary>
        /// <returns>The query cache unique key.</returns>
        public string GetQueryCacheUniqueKey(string[] tags)
        {
            var cacheKey = new StringBuilder();

            var mainKey = QueryCacheManager.GetCacheKey(OriginalQueryable, tags);

            if (QueryCacheManager.UseFirstTagAsCacheKey || QueryCacheManager.UseTagsAsCacheKey)
            {
                // ONLY need to resolve once
                return(mainKey);
            }

            // ADD query main
            cacheKey.AppendLine("Query Main");
            cacheKey.AppendLine(mainKey);
            cacheKey.AppendLine("---");
            cacheKey.AppendLine();

            for (var i = 0; i < Childs.Count; i++)
            {
                var child    = Childs[i].GetFilteredQuery(OriginalQueryable);
                var childKey = QueryCacheManager.GetCacheKey(child, tags);

                // ADD query child
                cacheKey.AppendLine("Query Child: " + i);
                cacheKey.AppendLine(childKey);
                cacheKey.AppendLine("---");
                cacheKey.AppendLine();
            }

            return(cacheKey.ToString());
        }
Esempio 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 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);
        }
        /// <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);
        }
Esempio n. 5
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">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 Task <IEnumerable <T> > FromCacheAsync <T>(this IQueryable <T> query, CacheItemPolicy policy, params string[] tags) where T : class
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(Task.Run(() => {
                    return (IEnumerable <T>)query.AsNoTracking().ToList();
                }));
            }

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

            var result = Task.Run(() =>
            {
                var item = QueryCacheManager.Get <List <T> >(key);

                if (item == null)
                {
                    item = query.AsNoTracking().ToList();
                    item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item;
                    QueryCacheManager.AddCacheTag(key, tags);
                    QueryCacheManager.AddCacheTag(key, typeof(T).Name + QueryCacheManager.CacheTypeSuffix);
                }

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

            return(result);
        }
Esempio 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 asynchronously 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 Task <T> FromCacheAsync <T>(this QueryDeferred <T> query, CacheItemPolicy policy, params string[] tags)
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(Task.Run(() =>
                {
                    return query.Execute();
                }));
            }

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

            var result = Task.Run(() =>
            {
                var item = QueryCacheManager.GetDeferred(key);

                if (item == null)
                {
                    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);
                }

                item = item.IfDbNullThenNull();

                return((T)item);
            });

            return(result);
        }
        /// <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="tags">
        ///     A variable-length parameters list containing tags to expire cached
        ///     entries.
        /// </param>
        /// <returns>The result of the query.</returns>
        public static Task <IEnumerable <T> > FromCacheAsync <T>(this IQueryable <T> query, DateTimeOffset absoluteExpiration, params string[] tags) where T : class
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(Task.Run(() => {
                    return (IEnumerable <T>)query.AsNoTracking().ToList();
                }));
            }

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

            var result = Task.Run(() =>
            {
                var item = QueryCacheManager.Get <List <T> >(key);

                if (item == null)
                {
                    item = query.AsNoTracking().ToList();
                    item = QueryCacheManager.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
                    QueryCacheManager.AddCacheTag(key, tags);
                }

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

            return(result);
        }
        /// <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">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 Task <T> FromCacheAsync <T>(this QueryDeferred <T> query, CacheItemPolicy policy, params string[] tags)
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var result = Task.Run(() =>
            {
                var item = QueryCacheManager.Cache.Get(key);

                if (item == null)
                {
                    item = query.Execute();
                    item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, policy) ?? item;
                    QueryCacheManager.AddCacheTag(key, tags);
                }
                else
                {
                    if (item == DBNull.Value)
                    {
                        item = null;
                    }
                }

                return((T)item);
            });

            return(result);
        }
        /// <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);
        }
Esempio n. 10
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="tags">
        ///     A variable-length parameters list containing tags to expire cached
        ///     entries.
        /// </param>
        /// <returns>The result of the query.</returns>
        public static Task <T> FromCacheAsync <T>(this QueryDeferred <T> query, DateTimeOffset absoluteExpiration, params string[] tags)
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(Task.Run(() =>
                {
                    return query.Execute();
                }));
            }

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

            var result = Task.Run(() =>
            {
                var item = QueryCacheManager.Cache.Get(key);

                if (item == null)
                {
                    item = query.Execute();
                    item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
                    QueryCacheManager.AddCacheTag(key, tags);
                }

                item = item.IfDbNullThenNull();

                return((T)item);
            });

            return(result);
        }
Esempio n. 11
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);
        }
        /// <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="options">The cache entry options 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 Task <T> FromCacheAsync <T>(this QueryDeferred <T> query, MemoryCacheEntryOptions options, params string[] tags)
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var result = Task.Run(() =>
            {
                object item;
                if (!QueryCacheManager.Cache.TryGetValue(key, out item))
                {
                    item = query.Execute();
                    item = QueryCacheManager.Cache.Set(key, item ?? DBNull.Value, options);
                    QueryCacheManager.AddCacheTag(key, tags);
                }
                else
                {
                    if (item == DBNull.Value)
                    {
                        item = null;
                    }
                }

                return((T)item);
            });

            return(result);
        }
Esempio n. 13
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);
        }
        public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext)
        {
            var context = interceptionContext.DbContexts.FirstOrDefault();

            if (context != null)
            {
                var sets = context.GetObjectContext().ObjectStateManager
                           .GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified)
                           .Select(x => x.EntitySet).Distinct().ToList();

                sets.ForEach(x => QueryCacheManager.ExpireTag(QueryCacheManager.PrefixTagSet + x.Name));
            }
        }
Esempio n. 15
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">The generic type of the query.</typeparam>
        /// <param name="query">The query to cache in the QueryCacheManager.</param>
        /// <param name="options">The cache entry options 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, MemoryCacheEntryOptions options, params string[] tags)
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            object item;

            if (!QueryCacheManager.Cache.TryGetValue(key, out item))
            {
                item = query.Execute();
                item = QueryCacheManager.Cache.Set(key, item, options);
                QueryCacheManager.AddCacheTag(key, tags);
            }

            return((T)item);
        }
Esempio n. 16
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
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Cache.Get(key);

            if (item == null)
            {
                item = query.AsNoTracking().ToList();
                item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
            }

            return((IEnumerable <T>)item);
        }
Esempio n. 17
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
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Cache.Get(key);

            if (item == null)
            {
                item = query.AsNoTracking().ToList();
                item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
            }

            return((IEnumerable <T>)item);
        }
Esempio n. 18
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">The generic type of the query.</typeparam>
        /// <param name="query">The query to cache in the QueryCacheManager.</param>
        /// <param name="options">The cache entry options 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, MemoryCacheEntryOptions options, params string[] tags) where T : class
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            object item;

            if (!QueryCacheManager.Cache.TryGetValue(key, out item))
            {
                item = query.AsNoTracking().ToList();
                item = QueryCacheManager.Cache.Set(key, item, options);
                QueryCacheManager.AddCacheTag(key, tags);
            }

            return((IEnumerable <T>)item);
        }
Esempio n. 19
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)
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Cache.Get(key);

            if (item == null)
            {
                item = query.Execute();
                item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
            }

            return((T)item);
        }
Esempio n. 20
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)
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Cache.Get(key);

            if (item == null)
            {
                item = query.Execute();
                item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
            }

            return((T)item);
        }
        /// <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
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Cache.Get(key);

            if (item == null)
            {
                item = await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);

                item = QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
            }

            return((IEnumerable <T>)item);
        }
        /// <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="options">The cache entry options to use to cache the query.</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, MemoryCacheEntryOptions options, CancellationToken cancellationToken = default(CancellationToken), params string[] tags) where T : class
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            object item;

            if (!QueryCacheManager.Cache.TryGetValue(key, out item))
            {
                item = await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);

                item = QueryCacheManager.Cache.Set(key, item, options);
                QueryCacheManager.AddCacheTag(key, tags);
            }

            return((IEnumerable <T>)item);
        }
Esempio n. 23
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="options">The cache entry options to use to cache the query.</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, MemoryCacheEntryOptions options, CancellationToken cancellationToken = default(CancellationToken), params string[] tags)
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            object item;

            if (!QueryCacheManager.Cache.TryGetValue(key, out item))
            {
                item = await query.ExecuteAsync(cancellationToken).ConfigureAwait(false);

                item = QueryCacheManager.Cache.Set(key, item ?? DBNull.Value, options);
                QueryCacheManager.AddCacheTag(key, tags);
            }

            item = item.IfDbNullThenNull();

            return((T)item);
        }
        /// <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">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="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, CacheItemPolicy policy, CancellationToken cancellationToken = default(CancellationToken), params string[] tags) where T : class
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Cache.Get(key);

            if (item == null)
            {
                item = await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);

                item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
            }

            item = item.IfDbNullThenNull();

            return((IEnumerable <T>)item);
        }
Esempio n. 25
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)
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var item = QueryCacheManager.Cache.Get(key);

            if (item == null)
            {
                item = await query.ExecuteAsync(cancellationToken).ConfigureAwait(false);

                item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, absoluteExpiration) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
            }

            item = item.IfDbNullThenNull();

            return((T)item);
        }
Esempio n. 26
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)
            {
                item = query.ToList();
                item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
            }

            return((IEnumerable <T>)item);
        }
Esempio n. 27
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="tags">
        ///     A variable-length parameters list containing tag to use for the cache
        ///     key and expiration.
        /// </param>
        /// <returns>The result of the query.</returns>
        public static Task <T> FromCacheAsync <T>(this QueryDelayed <T> query, DateTimeOffset absoluteExpiration, params string[] tags)
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var result = Task.Run(() =>
            {
                var item = QueryCacheManager.Cache.Get(key);

                if (item == null)
                {
                    item = query.Execute();
                    QueryCacheManager.Cache.AddOrGetExisting(key, item, absoluteExpiration);
                    QueryCacheManager.AddCacheTag(key, tags);
                }

                return((T)item);
            });

            return(result);
        }
        /// <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">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 Task <IEnumerable <T> > FromCacheAsync <T>(this IQueryable <T> query, CacheItemPolicy policy, params string[] tags) where T : class
        {
            var key = QueryCacheManager.GetCacheKey(query, tags);

            var result = Task.Run(() =>
            {
                var item = QueryCacheManager.Cache.Get(key);

                if (item == null)
                {
                    item = query.AsNoTracking().ToList();
                    item = QueryCacheManager.Cache.AddOrGetExisting(key, item, policy) ?? item;
                    QueryCacheManager.AddCacheTag(key, tags);
                }

                item = item.IfDbNullThenNull();

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

            return(result);
        }
        /// <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">The generic type of the query.</typeparam>
        /// <param name="query">The query to cache in the QueryCacheManager.</param>
        /// <param name="options">The cache entry options 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, MemoryCacheEntryOptions options, params string[] tags)
        {
            if (!QueryCacheManager.IsEnabled)
            {
                return(query.Execute());
            }

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

            object item;

            if (!QueryCacheManager.Cache.TryGetValue(key, out item))
            {
                item = query.Execute();

                item = QueryCacheManager.Cache.Set(key, item ?? DBNull.Value, options);
                QueryCacheManager.AddCacheTag(key, tags);
            }

            item = item.IfDbNullThenNull();

            return((T)item);
        }
Esempio n. 30
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">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="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, CacheItemPolicy policy, 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.Cache.Get(key);

            if (item == null)
            {
                item = await query.ExecuteAsync(cancellationToken).ConfigureAwait(false);

                item = QueryCacheManager.Cache.AddOrGetExisting(key, item ?? DBNull.Value, policy) ?? item;
                QueryCacheManager.AddCacheTag(key, tags);
            }

            item = item.IfDbNullThenNull();

            return((T)item);
        }