コード例 #1
0
        protected virtual async Task SetContentToCacheAsync(
            ZeusCachingContext context,
            ZeusCachingProfileOptions options,
            string key,
            object content,
            ICachingAdapter cachingAdapter)
        {
            object wrappedContent;

            if (options.WrappingResultHandler != null)
            {
                wrappedContent = options.WrappingResultHandler(_serviceProvider, context.ExecutionContext.HttpContext, content);
            }
            else
            {
                wrappedContent = content;
            }

            if (wrappedContent is string)
            {
                await cachingAdapter.SetContentAsync(key, (string)wrappedContent, context.SlidingExpiration, null, context.AbsoluteExpirationRelativeToNow);
            }
            else
            {
                // Serialize the response
                var json = Serializer(wrappedContent);
                await cachingAdapter.SetContentAsync(key, json, context.SlidingExpiration, null, context.AbsoluteExpirationRelativeToNow);
            }
        }
コード例 #2
0
        public async Task ProcessRequestAsync(ZeusCachingContext context, ActionExecutionDelegate next)
        {
            if (!_options.IsEnabled || IsDisabledGlobally.Value)
            {
                await next();

                return;
            }

            var profileOptions = _ZeusCachingProfileResolver.GetOptions(context.ProfileName);

            if (!profileOptions.IsEnabled)
            {
                await next();

                return;
            }

            if (!profileOptions.CachingPredicate(_serviceProvider, context.ExecutionContext.HttpContext))
            {
                await next();

                return;
            }

            if (profileOptions.CacheKeyHandler == null)
            {
                throw new InvalidOperationException("No CacheKeyHandler is registered.");
            }

            var httpResponse   = context.ExecutionContext.HttpContext.Response;
            var key            = profileOptions.CacheKeyHandler(_serviceProvider, context.ExecutionContext.HttpContext);
            var cachingAdapter = _cachingAdapterFactory.Create(profileOptions.CachingAdapterMode);
            var response       = await cachingAdapter.GetContentAsync(key);

            if (string.IsNullOrWhiteSpace(response))
            {
                var resultContext = await next?.Invoke();

                // Don't cache errors
                if (resultContext.Exception != null)
                {
                    return;
                }

                if (IsSuccessStatusCode(httpResponse.StatusCode))
                {
                    if (_actionResultCachingContextAdapter.TryGetContent(resultContext.Result, out object content))
                    {
                        await SetContentToCacheAsync(context, profileOptions, key, content, cachingAdapter);
                    }
                    else
                    {
                        // Only supported ActionResult content gets cached.
                    }
                }
            }
            else
            {
                context.ExecutionContext.Result             = _actionResultCachingContextAdapter.CreateResult(response);
                httpResponse.Headers[ZeusCachingHeaderName] = $"{response?.Length}";
            }
        }