public virtual TResult Execute <TResult>(Expression query) { Check.NotNull(query, nameof(query)); var queryContext = _queryContextFactory.Create(); query = ExtractParameters(query, queryContext); return(CompileQuery <TResult>(query)(queryContext)); }
public virtual TResult Execute <TResult>([NotNull] Expression expression) { Check.NotNull(expression, nameof(expression)); var queryContext = _queryContextFactory.Create(); queryContext.ContextType = _context.GetType(); return(_compiledQueryCache.Execute <TResult>(expression, _database, queryContext)); }
public override TResult Execute <TResult>(Expression query) { Check.NotNull(query, nameof(query)); var queryContext = _queryContextFactory.Create(); // search for cacheable operator and extract parameter var cachableExpressionVisitor = new CachableExpressionVisitor(); query = cachableExpressionVisitor.GetExtractCachableParameter(query, out bool isCacheable, out CacheableOptions options); query = _queryModelGenerator.ExtractParameters(_logger, query, queryContext); // if cacheable operator is part of the query use cache logic if (isCacheable) { // generate key to identify query var queryKey = _cacheProvider.CreateQueryKey(query, queryContext.ParameterValues); if (_cacheProvider.TryGetCachedResult <TResult>(queryKey, out TResult cacheResult)) { _logger.Logger.Log <object>(LogLevel.Debug, CacheableEventId.CacheHit, queryKey, null, _logFormatter); //cache was hit, so return cached query result return(cacheResult); } else // cache was not hit { var cacheKey = _compiledQueryCacheKeyGenerator.GenerateCacheKey(query, false); var compiledQuery = _compiledQueryCache.GetOrAddQuery(cacheKey, () => CompileQueryCore <TResult>(query, _queryModelGenerator, _database, _logger, _contextType)); // excecute query var queryResult = compiledQuery(queryContext); // add query result to cache if (ShouldResultBeCached(queryResult, options)) { _cacheProvider.SetCachedResult <TResult>(queryKey, queryResult, options.TimeToLive); } _logger.Logger.Log <object>(LogLevel.Debug, CacheableEventId.QueryResultCached, queryKey, null, _logFormatter); return(queryResult); } } else { // return default query result var cacheKey = _compiledQueryCacheKeyGenerator.GenerateCacheKey(query, false); var compiledQuery = _compiledQueryCache.GetOrAddQuery(cacheKey, () => CompileQueryCore <TResult>(query, _queryModelGenerator, _database, _logger, _contextType)); return(compiledQuery(queryContext)); } }
/// <summary> /// use no compiler /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="query"></param> /// <returns></returns> private TResult ShardingExecute <TResult>(Expression query) { var queryContext = _queryContextFactory.Create(); query = ExtractParameters(query, queryContext, _logger); var compiledQuery = CompileQueryCore <TResult>(_database, query, _model, false); return(compiledQuery(queryContext)); }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public virtual TResult Execute <TResult>(Expression query) { var queryContext = _queryContextFactory.Create(); query = ExtractParameters(query, queryContext, _logger); var compiledQuery = _compiledQueryCache .GetOrAddQuery( _compiledQueryCacheKeyGenerator.GenerateCacheKey(query, async: false), () => CompileQueryCore <TResult>(_database, query, _model, false)); return(compiledQuery(queryContext)); }
private HashCodeCombiner GetExpressionHash(Expression expression) { var queryContext = _queryContextFactory.Create(); expression = _queryCompiler.ExtractParameters( expression, queryContext, _logger, parameterize: false); var hashCode = ExpressionEqualityComparer.Instance.GetHashCode(expression); var combiner = HashCodeCombiner.Start().Add(hashCode); var parameterValues = queryContext.ParameterValues; if (parameterValues.Count > 0) { // If query has parameters add to combiner foreach (var p in parameterValues) { combiner.Add(p.Key); combiner.Add(p.Value); } } return(combiner); }
private IQueryContext CreateQueryContext() { _logger.Debug("Creating query context..."); var queryContext = _queryContextFactory.Create(); // todo: no need for IQueryContext i think. just use dictionary queryContext.Bag = _contextBagData.ToDictionary(d => d.Key, d => d.Value); // shallow copy return(queryContext); }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual TResult Execute <TResult>(Expression query) { Check.NotNull(query, nameof(query)); var queryContext = _queryContextFactory.Create(); query = ExtractParameters(query, queryContext); var compiledQuery = _compiledQueryCache .GetOrAddQuery(_compiledQueryCacheKeyGenerator.GenerateCacheKey(query, async: false), () => CompileQueryCore <TResult>(query, NodeTypeProvider, _database, _logger, _contextType)); return(compiledQuery(queryContext)); }