Esempio n. 1
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (BypassPrivateCache(request))
            {
                var response = await base.SendAsync(request, cancellationToken);

                response = await ProcessResponseCaching(response);

                return(response);
            }

            var requestUrl = request.RequestUri.AbsoluteUri;

            if (_cache.IsExpired(requestUrl))
            {
                var eTag = _cache.GetETag(requestUrl);
                if (eTag != null)
                {
                    request.Headers.IfNoneMatch.TryParseAdd(eTag);
                }

                var response = await base.SendAsync(request, cancellationToken);

                response = await ProcessResponseCaching(response);

                if (response.StatusCode == HttpStatusCode.NotModified)
                {
                    response.StatusCode = HttpStatusCode.OK;
                }

                return(response);
            }

            var cachedResponse = _cache.Get(requestUrl);

            var bytes = Convert.FromBase64String(cachedResponse);


            var httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                //Content = new StringContent(cachedResponse)
                Content = new StreamContent(new MemoryStream(bytes))
            };

            return(httpResponse);
        }