diff --git a/.gitignore b/.gitignore index 718941c..e71f35b 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ nuget.exe project.lock.json /.vs/ .build/ -.testPublish/ \ No newline at end of file +.testPublish/ +launchSettings.json \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.ResponseCaching/Internal/MemoryResponseCacheStore.cs b/src/Microsoft.AspNetCore.ResponseCaching/Internal/MemoryResponseCacheStore.cs index 1a49f19..02191c9 100644 --- a/src/Microsoft.AspNetCore.ResponseCaching/Internal/MemoryResponseCacheStore.cs +++ b/src/Microsoft.AspNetCore.ResponseCaching/Internal/MemoryResponseCacheStore.cs @@ -24,11 +24,11 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal public Task GetAsync(string key) { var entry = _cache.Get(key); - - if (entry is MemoryCachedResponse) + + var memoryCachedResponse = entry as MemoryCachedResponse; + if (memoryCachedResponse != null) { - var memoryCachedResponse = (MemoryCachedResponse)entry; - return Task.FromResult(new CachedResponse() + return Task.FromResult(new CachedResponse { Created = memoryCachedResponse.Created, StatusCode = memoryCachedResponse.StatusCode, @@ -44,15 +44,15 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal public async Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor) { - if (entry is CachedResponse) + var cachedResponse = entry as CachedResponse; + if (cachedResponse != null) { - var cachedResponse = (CachedResponse)entry; var segmentStream = new SegmentWriteStream(StreamUtilities.BodySegmentSize); await cachedResponse.Body.CopyToAsync(segmentStream); _cache.Set( key, - new MemoryCachedResponse() + new MemoryCachedResponse { Created = cachedResponse.Created, StatusCode = cachedResponse.StatusCode, @@ -60,7 +60,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal BodySegments = segmentStream.GetSegments(), BodyLength = segmentStream.Length }, - new MemoryCacheEntryOptions() + new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = validFor }); @@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal _cache.Set( key, entry, - new MemoryCacheEntryOptions() + new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = validFor }); diff --git a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCacheMiddleware.cs b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCacheMiddleware.cs index 6be81a9..000e031 100644 --- a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCacheMiddleware.cs +++ b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCacheMiddleware.cs @@ -111,8 +111,14 @@ namespace Microsoft.AspNetCore.ResponseCaching } } - internal async Task TryServeCachedResponseAsync(ResponseCacheContext context, CachedResponse cachedResponse) + internal async Task TryServeCachedResponseAsync(ResponseCacheContext context, IResponseCacheEntry cacheEntry) { + var cachedResponse = cacheEntry as CachedResponse; + if (cachedResponse == null) + { + return false; + } + context.CachedResponse = cachedResponse; context.CachedResponseHeaders = new ResponseHeaders(cachedResponse.Headers); context.ResponseTime = _options.SystemClock.UtcNow; @@ -171,24 +177,26 @@ namespace Microsoft.AspNetCore.ResponseCaching context.BaseKey = _keyProvider.CreateBaseKey(context); var cacheEntry = await _store.GetAsync(context.BaseKey); - if (cacheEntry is CachedVaryByRules) + var cachedVaryByRules = cacheEntry as CachedVaryByRules; + if (cachedVaryByRules != null) { // Request contains vary rules, recompute key(s) and try again - context.CachedVaryByRules = (CachedVaryByRules)cacheEntry; + context.CachedVaryByRules = cachedVaryByRules; foreach (var varyKey in _keyProvider.CreateLookupVaryByKeys(context)) { - cacheEntry = await _store.GetAsync(varyKey); - - if (cacheEntry is CachedResponse && await TryServeCachedResponseAsync(context, (CachedResponse)cacheEntry)) + if (await TryServeCachedResponseAsync(context, await _store.GetAsync(varyKey))) { return true; } } } - else if (cacheEntry is CachedResponse && await TryServeCachedResponseAsync(context, (CachedResponse)cacheEntry)) + else { - return true; + if (await TryServeCachedResponseAsync(context, cacheEntry)) + { + return true; + } } if (context.RequestCacheControlHeaderValue.OnlyIfCached)