Пример #1
0
        public async Task <CacheQueryResult> QueryCacheAsync(HttpRequestMessage request)
        {
            // Do we have anything stored for this method and URI?  Return entries for all variants
            var cacheEntryList = await _contentStore.GetEntriesAsync(new CacheKey(request.RequestUri, request.Method)).ConfigureAwait(false);

            if (cacheEntryList == null)  // Should I use null or Count() == 0 ?
            {
                return(CacheQueryResult.CannotUseCache());
            }

            // Find the first matching variant based on the vary header fields defined in the cacheEntry
            // and the values in the request
            var selectedEntry = MatchVariant(request, cacheEntryList);


            // Do we have a matching variant representation?
            if (selectedEntry == null)
            {
                return(CacheQueryResult.CannotUseCache());
            }

            // Get the complete response, including body based on the selected variant
            var response = await _contentStore.GetResponseAsync(selectedEntry.VariantId).ConfigureAwait(false);

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

            if ((requestCacheControl.NoCache || selectedEntry.CacheControl.NoCache))
            {
                return(CacheQueryResult.Revalidate(selectedEntry, response));
            }

            // Is it fresh?
            if (selectedEntry.IsFresh())
            {
                if (requestCacheControl.MinFresh != null)
                {
                    var age = HttpCache.CalculateAge(response);
                    if (age <= requestCacheControl.MinFresh)
                    {
                        return(CacheQueryResult.ReturnStored(selectedEntry, response));
                    }
                }
                else
                {
                    return(CacheQueryResult.ReturnStored(selectedEntry, response));
                }
            }

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

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

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