Exemple #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 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);
        }
        /// <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);
        }
Exemple #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">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);
        }
Exemple #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 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="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);
        }
Exemple #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 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);
        }
Exemple #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 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 DbSqlQuery <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)
            {
                item = query.AsNoTracking().ToList();
                item = QueryCacheManager.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 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
        {
            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)
            {
                item = await query.AsNoTracking().ToListAsync(cancellationToken).ConfigureAwait(false);

                item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? 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 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);
        }