/// <summary>
        /// Gets an EF query and returns its hashed key to store in the cache.
        /// </summary>
        /// <param name="query">The EF query.</param>
        /// <param name="expression">An expression tree that represents a LINQ query.</param>
        /// <param name="saltKey">If you think the computed hash of the query is not enough, set this value.</param>
        /// <returns>Information of the computed key of the input LINQ query.</returns>
        public EFCacheKey GetEFCacheKey(IQueryable query, Expression expression, string saltKey = "")
        {
            var queryCompiler = (QueryCompiler)_queryCompilerField.GetValue(query.Provider);

            var(expressionKeyHash, modifiedExpression) = getExpressionKeyHash(queryCompiler, _cacheKeyHashProvider, expression);
            var cachedKey = _keysCacheManager.Get <EFCacheKey>(expressionKeyHash);

            if (cachedKey != null)
            {
                return(cachedKey);
            }

            var expressionPrinter = new ExpressionPrinter();
            var sql = expressionPrinter.PrintDebug(modifiedExpression);

            var expressionVisitorResult = EFQueryExpressionVisitor.GetDebugView(expression);
            var key     = $"{sql};{expressionVisitorResult.DebugView};{saltKey}";
            var keyHash = _cacheKeyHashProvider.ComputeHash(key);

            var cacheKey = new EFCacheKey
            {
                Key               = key,
                KeyHash           = keyHash,
                CacheDependencies = expressionVisitorResult.Types
            };

            setCache(expressionKeyHash, cacheKey);
            return(cacheKey);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Write out the given AST
 /// </summary>
 public static EFQueryDebugView GetDebugView(Expression node)
 {
     using (var writer = new StringWriter(CultureInfo.CurrentCulture))
     {
         var efQueryExpressionVisitor = new EFQueryExpressionVisitor(writer);
         efQueryExpressionVisitor.writeTo(node);
         var types = efQueryExpressionVisitor._types;
         return(new EFQueryDebugView {
             DebugView = writer.ToString(), Types = types
         });
     }
 }