예제 #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="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);
        }
        /// <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);
        }
예제 #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);
        }
예제 #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">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);
        }
예제 #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 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);
        }
예제 #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">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>
        ///     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);
        }
        public QueryCacheItemTracker Initialize <T>(QueryDeferred <T> query)
        {
            if (QueryCacheManager.IsAutoExpireCacheEnabled)
            {
                Context = query.Query.GetObjectQuery().Context;
                AddHook();
            }

            return(this);
        }
예제 #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 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);
        }
예제 #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 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);
        }
예제 #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 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);
        }
예제 #12
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);
        }
예제 #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 <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);
        }
예제 #14
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)
        {
            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);
        }
예제 #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="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);
        }
예제 #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">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.Cache.Get(key);

            if (item == null)
            {
                item = query.Execute();

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

            item = item.IfDbNullThenNull();

            return((T)item);
        }
        /// <summary>Gets cached keys used to cache or retrieve a query from the QueryCacheManager.</summary>
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="query">The query to cache or retrieve from the QueryCacheManager.</param>
        /// <param name="tags">A variable-length parameters list containing tags to create the cache key.</param>
        /// <returns>The cache key used to cache or retrieve a query from the QueryCacheManager.</returns>
        public static string GetCacheKey <T>(QueryDeferred <T> query, string[] tags)
        {
            var sb = new StringBuilder();

#if EF5 || EF6
            var objectQuery = query.Query.GetObjectQuery();

            sb.AppendLine(CachePrefix);
            sb.AppendLine(string.Join(";", tags));
            sb.AppendLine(objectQuery.ToTraceString());

            foreach (var parameter in objectQuery.Parameters)
            {
                sb.Append(parameter.Name);
                sb.Append(";");
                sb.Append(parameter.Value);
                sb.AppendLine(";");
            }
#elif EFCORE
            var command = query.Query.CreateCommand();

            sb.AppendLine(CachePrefix);
            sb.AppendLine(string.Join(";", tags));
            sb.AppendLine(command.CommandText);

            foreach (var parameter in command.Parameters)
            {
                sb.Append(parameter.Name);
                sb.Append(";");
                sb.Append(parameter.Value);
                sb.AppendLine(";");
            }
#endif

            return(sb.ToString());
        }
예제 #18
0
 /// <summary>Gets cached keys used to cache or retrieve a query from the QueryCacheManager.</summary>
 /// <typeparam name="T">Generic type parameter.</typeparam>
 /// <param name="query">The query to cache or retrieve from the QueryCacheManager.</param>
 /// <param name="tags">A variable-length parameters list containing tags to create the cache key.</param>
 /// <returns>The cache key used to cache or retrieve a query from the QueryCacheManager.</returns>
 public static string GetCacheKey <T>(QueryDeferred <T> query, string[] tags)
 {
     return(GetCacheKey(query.Query, tags));
 }
예제 #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="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, params string[] tags)
 {
     return(query.FromCache(QueryCacheManager.DefaultCacheItemPolicy, tags));
 }
        /// <summary>Gets cached keys used to cache or retrieve a query from the QueryCacheManager.</summary>
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="query">The query to cache or retrieve from the QueryCacheManager.</param>
        /// <param name="tags">A variable-length parameters list containing tags to create the cache key.</param>
        /// <returns>The cache key used to cache or retrieve a query from the QueryCacheManager.</returns>
        public static string GetCacheKey <T>(QueryDeferred <T> query, string[] tags)
        {
            var sb = new StringBuilder();

#if EF5 || EF6
            var objectQuery = query.Query.GetObjectQuery();

            sb.AppendLine(CachePrefix);

            if (IncludeConnectionInCacheKey)
            {
                var connection = ((EntityConnection)objectQuery.Context.Connection).GetStoreConnection();

                // FORCE database name in case "ChangeDatabase()" method is used
                sb.AppendLine(connection.DataSource ?? "");
                sb.AppendLine(connection.Database ?? "");
                sb.AppendLine(connection.ConnectionString);
            }

            sb.AppendLine(string.Join(";", tags));
#endif

#if EF5
            sb.AppendLine(objectQuery.ToTraceString());

            foreach (var parameter in objectQuery.Parameters)
            {
                sb.Append(parameter.Name);
                sb.Append(";");
                sb.Append(parameter.Value);
                sb.AppendLine(";");
            }
#elif EF6
            var commandTextAndParameters = objectQuery.GetCommandTextAndParameters();
            sb.AppendLine(commandTextAndParameters.Item1);

            foreach (DbParameter parameter in commandTextAndParameters.Item2)
            {
                sb.Append(parameter.ParameterName);
                sb.Append(";");
                sb.Append(parameter.Value);
                sb.AppendLine(";");
            }
#elif EFCORE
            RelationalQueryContext queryContext;
            var command = query.Query.CreateCommand(out queryContext);

            sb.AppendLine(CachePrefix);

            if (IncludeConnectionInCacheKey)
            {
                var connection = queryContext.Connection.DbConnection;

                // FORCE database name in case "ChangeDatabase()" method is used
                sb.AppendLine(connection.DataSource ?? "");
                sb.AppendLine(connection.Database ?? "");
                sb.AppendLine(connection.ConnectionString);
            }

            sb.AppendLine(string.Join(";", tags));
            sb.AppendLine(command.CommandText);

            foreach (var parameter in queryContext.ParameterValues)
            {
                sb.Append(parameter.Key);
                sb.Append(";");
                sb.Append(parameter.Value);
                sb.AppendLine(";");
            }
#endif

            return(sb.ToString());
        }
예제 #21
0
 /// <summary>Gets cached keys used to cache or retrieve a query from the QueryCacheManager.</summary>
 /// <typeparam name="T">Generic type parameter.</typeparam>
 /// <param name="query">The query to cache or retrieve from the QueryCacheManager.</param>
 /// <param name="tags">A variable-length parameters list containing tags to create the cache key.</param>
 /// <returns>The cache key used to cache or retrieve a query from the QueryCacheManager.</returns>
 internal static string GetCacheKey <T>(QueryDeferred <T> query, string[] tags)
 {
     return(CachePrefix + string.Join(";", tags) + query.Query.Expression);
 }
예제 #22
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="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, params string[] tags)
 {
     return(query.FromCache(QueryCacheManager.DefaultMemoryCacheEntryOptions, tags));
 }
예제 #23
0
 /// <summary>
 ///     Return the result of the <paramref name="query" /> from the cache if possible. Otherwise, materialize
 ///     asynchronously the query and cache the result
 ///     before being returned.
 /// </summary>
 /// <typeparam name="T">Generic type parameter.</typeparam>
 /// <param name="query">The query to cache.</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, params string[] tags)
 {
     return(await query.FromCacheAsync(QueryCacheManager.DefaultMemoryCacheEntryOptions, tags));
 }
예제 #24
0
 /// <summary>
 ///     Return the result of the <paramref name="query" /> from the cache if possible. Otherwise,
 ///     materialize asynchronously the query and cache the result before being returned.
 /// </summary>
 /// <typeparam name="T">Generic type parameter.</typeparam>
 /// <param name="query">The query to cache.</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 Task <T> FromCacheAsync <T>(this QueryDeferred <T> query, CancellationToken cancellationToken = default(CancellationToken), params string[] tags)
 {
     return(query.FromCacheAsync(QueryCacheManager.DefaultMemoryCacheEntryOptions, cancellationToken, tags));
 }
예제 #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 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 Task <T> FromCacheAsync <T>(this QueryDeferred <T> query, MemoryCacheEntryOptions options, params string[] tags)
 {
     return(query.FromCacheAsync(options, default(CancellationToken), tags));
 }
예제 #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 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="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, params string[] tags)
 {
     return(query.FromCacheAsync(QueryCacheManager.DefaultCacheItemPolicy, default(CancellationToken), tags));
 }
예제 #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 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)
 {
     return(query.FromCacheAsync(absoluteExpiration, default(CancellationToken), tags));
 }