Beispiel #1
0
        public override TResult ExecuteAsync <TResult>(Expression expression, CancellationToken cancellationToken = default)
        {
            var cachingResult = ReadFromCache <TResult>(expression);

            if (cachingResult.HasResult)
            {
                return(cachingResult.WrapAsyncResult(cachingResult.CacheEntry.Value));
            }

            var result = base.ExecuteAsync <TResult>(cachingResult.Expression, cancellationToken);

            if (!cachingResult.CanPut || cancellationToken.IsCancellationRequested)
            {
                return(result);
            }

            using (var scope = new DbContextScope((HookingDbContext)_currentContext.Context, lazyLoading: false))
            {
                var cacheValue = cachingResult.ConvertQueryAsyncResult(result).Await();

                if (cacheValue.Count <= cachingResult.Policy.MaxRows.Value)
                {
                    var entry = new DbCacheEntry
                    {
                        Key       = cachingResult.CacheKey,
                        Value     = cacheValue.Value,
                        ValueType = cacheValue.Value?.GetType()
                    };

                    _cache.Put(cachingResult.CacheKey, entry, cachingResult.Policy);

                    Log(DbCachingEventId.QueryResultCached,
                        "Has put query result to cache. Key: {0}, Type: {1}, Policy: {2}.",
                        cachingResult.CacheKey.Key,
                        typeof(TResult),
                        cachingResult.Policy);
                }
                else
                {
                    Log(DbCachingEventId.MaxRowsExceeded,
                        "Max rows limit exceeded. Will not cache. Actual: {0}, Limit: {1} Key: {2}, Type: {3}.",
                        cacheValue.Count,
                        cachingResult.Policy.MaxRows.Value,
                        cachingResult.CacheKey.Key,
                        typeof(TResult));
                }

                return(cachingResult.WrapAsyncResult(cacheValue.Value));
            }
        }
 public virtual void PutItem(DbTransaction transaction, string key, object value, IEnumerable <string> dependentEntitySets, TimeSpan?duration)
 {
     if (transaction == null)
     {
         _cache.Put(key, value, dependentEntitySets, duration);
     }
 }