public async Task UpdateFreshnessAsync(CacheQueryResult result, HttpResponseMessage notModifiedResponse) { var selectedEntry = result.SelectedEntry; UpdateCacheEntry(notModifiedResponse, selectedEntry); await _contentStore.UpdateEntryAsync(selectedEntry, result.SelectedResponse).ConfigureAwait(false); //TODO }
public static void ApplyConditionalHeaders(CacheQueryResult result, HttpRequestMessage request) { Debug.Assert(result.SelectedEntry != null); if (result.SelectedEntry == null || !result.SelectedEntry.HasValidator) { return; } if (result.SelectedEntry.Etag != null) { request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(result.SelectedEntry.Etag)); } else { if (result.SelectedEntry.LastModified != null) { request.Headers.IfModifiedSince = result.SelectedEntry.LastModified; } } }
public static void ApplyConditionalHeaders(CacheQueryResult result, HttpRequestMessage request) { Debug.Assert(result.SelectedEntry != null); if (result.SelectedEntry == null || !result.SelectedEntry.HasValidator) return; if (result.SelectedEntry.Etag != null) { request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(result.SelectedEntry.Etag)); } else { if (result.SelectedEntry.LastModified != null) { request.Headers.IfModifiedSince = result.SelectedEntry.LastModified; } } }
public async Task UpdateFreshnessAsync(CacheQueryResult result, HttpResponseMessage notModifiedResponse ) { var selectedEntry = result.SelectedEntry; UpdateCacheEntry(notModifiedResponse, selectedEntry); await _contentStore.UpdateEntryAsync(selectedEntry, result.SelectedResponse).ConfigureAwait(false); //TODO }
public async Task <CacheQueryResult> QueryCacheAsync(HttpRequestMessage request) { // Do we have anything stored for this method and URI? 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()); } var selectedEntry = MatchVariant(request, cacheEntryList); // Do we have a matching variant representation? if (selectedEntry == null) { return(CacheQueryResult.CannotUseCache()); } 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()); }