示例#1
0
        private CompiledQuery GetOrAdd(
            Expression query,
            QueryContext queryContext,
            IDataStore dataStore,
            bool isAsync,
            Func <Expression, IDataStore, CompiledQuery> compiler)
        {
            var parameterizedQuery
                = ParameterExtractingExpressionTreeVisitor
                  .ExtractParameters(query, queryContext);

            var cacheKey
                = dataStore.Model.GetHashCode().ToString()
                  + isAsync
                  + new ExpressionStringBuilder()
                  .Build(query);

            CompiledQuery compiledQuery;

            lock (_compiledQueryLockObject)
            {
                if (!_memoryCache.TryGetValue(cacheKey, out compiledQuery))
                {
                    compiledQuery = compiler(parameterizedQuery, dataStore);
                    _memoryCache.Set(cacheKey, compiledQuery);
                }
            }

            return(compiledQuery);
        }
        private CompiledQuery GetOrAdd(
            Expression query,
            QueryContext queryContext,
            IDataStore dataStore,
            bool isAsync,
            Func <Expression, IDataStore, CompiledQuery> compiler)
        {
            var parameterizedQuery
                = ParameterExtractingExpressionTreeVisitor
                  .ExtractParameters(query, queryContext);

            var cacheKey
                = dataStore.Model.GetHashCode().ToString()
                  + isAsync
                  + new ExpressionStringBuilder()
                  .Build(query);

            var compiledQuery
                = _memoryCache.GetOrSet(
                      cacheKey,
                      Tuple.Create(parameterizedQuery, dataStore),
                      c =>
            {
                var tuple = (Tuple <Expression, IDataStore>)c.State;

                return(compiler(tuple.Item1, tuple.Item2));
            });

            return(compiledQuery);
        }