Exemple #1
0
 public PrivateCacheHandler(HttpMessageHandler innerHandler, HttpCache httpCache)
 {
     _httpCache   = httpCache;
     InnerHandler = innerHandler;
 }
 public PrivateCacheHandler(HttpMessageHandler innerHandler, HttpCache httpCache)
 {
     _httpCache = httpCache;
     InnerHandler = innerHandler;
 }
Exemple #3
0
        public async Task <CacheQueryResult> QueryCacheAsync(HttpRequestMessage request)
        {
            // Do we have anything stored for this method and URI?
            var cacheEntry = await _contentStore.GetEntryAsync(new PrimaryCacheKey(request.RequestUri, request.Method));

            if (cacheEntry == null)
            {
                return(CacheQueryResult.CannotUseCache());
            }

            // Do we have a matching variant representation?
            var secondaryKey = cacheEntry.CreateSecondaryKey(request); //, cacheEntry.VaryHeaders

            if (secondaryKey == "*")                                   // Vary: * never matches
            {
                return(CacheQueryResult.CannotUseCache());
            }
            var selectedResponse = await _contentStore.GetContentAsync(cacheEntry, secondaryKey);

            if (selectedResponse == null)
            {
                return(CacheQueryResult.CannotUseCache());
            }

            // Do caching directives require that we revalidate it regardless of freshness?
            var requestCacheControl = request.Headers.CacheControl ?? new CacheControlHeaderValue();

            if ((requestCacheControl.NoCache || selectedResponse.CacheControl.NoCache))
            {
                return(CacheQueryResult.Revalidate(selectedResponse));
            }

            // Is it fresh?
            if (selectedResponse.IsFresh())
            {
                if (requestCacheControl.MinFresh != null)
                {
                    if (HttpCache.CalculateAge(selectedResponse.Response) <= requestCacheControl.MinFresh)
                    {
                        return(CacheQueryResult.ReturnStored(selectedResponse));
                    }
                }
                else
                {
                    return(CacheQueryResult.ReturnStored(selectedResponse));
                }
            }

            // Did the client say we can serve it stale?
            if (requestCacheControl.MaxStale)
            {
                if (requestCacheControl.MaxStaleLimit != null)
                {
                    if ((DateTime.UtcNow - selectedResponse.Expires) <= requestCacheControl.MaxStaleLimit)
                    {
                        return(CacheQueryResult.ReturnStored(selectedResponse));
                    }
                }
                else
                {
                    return(CacheQueryResult.ReturnStored(selectedResponse));
                }
            }

            // Do we have a selector to allow us to do a conditional request to revalidate it?
            if (selectedResponse.HasValidator)
            {
                return(CacheQueryResult.Revalidate(selectedResponse));
            }

            // Can't do anything to help
            return(CacheQueryResult.CannotUseCache());
        }