コード例 #1
0
        /// <inheritdoc />
        public async Task <FluentHttpResponse> Invoke(FluentHttpMiddlewareContext context)
        {
            var request = context.Request;

            var options = request.GetResponseCachingOptions(_options);

            if (options.ShouldIgnore || options.Matcher != null && !options.Matcher(request))
            {
                return(await _next(context));
            }
            var hash = request.GetHash();
            FluentHttpResponse response = null;

            if (!options.IsWriteOnly)
            {
                response = await _service.Get(hash);
            }
            if (response != null)
            {
                foreach (var item in request.Items)
                {
                    response.Items[item.Key] = item.Value;
                }

                _logger.LogInformation("Pre-request - Returning a cached response {hash}", hash);
                return(response);
            }

            response = await _next(context);

            _logger.LogInformation("Post-Response - Caching request... {hash}", hash);
            await _service.Set(hash, response);

            return(response);
        }