diff --git a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCacheMiddleware.cs b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCacheMiddleware.cs index 1e5da7d23d..b57bea561b 100644 --- a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCacheMiddleware.cs +++ b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCacheMiddleware.cs @@ -201,7 +201,6 @@ namespace Microsoft.AspNetCore.ResponseCaching if (_policyProvider.IsResponseCacheable(context)) { context.ShouldCacheResponse = true; - context.BaseKey = _keyProvider.CreateBaseKey(context); // Create the cache entry now var response = context.HttpContext.Response; @@ -230,10 +229,11 @@ namespace Microsoft.AspNetCore.ResponseCaching Headers = normalizedVaryHeaderValue, Params = normalizedVaryParamsValue }; - - _store.Set(context.BaseKey, context.CachedVaryByRules, context.CachedResponseValidFor); } + // Always overwrite the CachedVaryByRules to update the expiry information + _store.Set(context.BaseKey, context.CachedVaryByRules, context.CachedResponseValidFor); + context.StorageVaryKey = _keyProvider.CreateStorageVaryByKey(context); } diff --git a/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCacheMiddlewareTests.cs b/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCacheMiddlewareTests.cs index 8ef4b51bf0..4b66d8b21b 100644 --- a/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCacheMiddlewareTests.cs +++ b/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCacheMiddlewareTests.cs @@ -320,7 +320,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests } [Fact] - public void FinalizeCacheHeaders_UpdateCachedVaryByRules_IfNotEquivalentToPrevious() + public async Task FinalizeCacheHeaders_UpdateCachedVaryByRules_IfNotEquivalentToPrevious() { var store = new TestResponseCacheStore(); var middleware = TestUtils.CreateTestMiddleware(store); @@ -336,6 +336,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests }; context.CachedVaryByRules = cachedVaryByRules; + await middleware.TryServeFromCacheAsync(context); middleware.FinalizeCacheHeaders(context); Assert.Equal(1, store.SetCount); @@ -343,7 +344,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests } [Fact] - public void FinalizeCacheHeaders_DoNotUpdateCachedVaryByRules_IfEquivalentToPrevious() + public async Task FinalizeCacheHeaders_DoNotUpdateCachedVaryByRules_IfEquivalentToPrevious() { var store = new TestResponseCacheStore(); var middleware = TestUtils.CreateTestMiddleware(store); @@ -360,9 +361,11 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests }; context.CachedVaryByRules = cachedVaryByRules; + await middleware.TryServeFromCacheAsync(context); middleware.FinalizeCacheHeaders(context); - Assert.Equal(0, store.SetCount); + // An update to the cache is always made but the entry should be the same + Assert.Equal(1, store.SetCount); Assert.Same(cachedVaryByRules, context.CachedVaryByRules); } @@ -411,12 +414,13 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests } [Fact] - public void FinalizeCachingHeaders_SplitsVaryHeaderByCommas() + public async Task FinalizeCacheHeaders_SplitsVaryHeaderByCommas() { var middleware = TestUtils.CreateTestMiddleware(); var context = TestUtils.CreateTestContext(); context.HttpContext.Response.Headers[HeaderNames.Vary] = "HeaderB, heaDera"; + await middleware.TryServeFromCacheAsync(context); middleware.FinalizeCacheHeaders(context); Assert.Equal(new StringValues(new[] { "HEADERA", "HEADERB" }), context.CachedVaryByRules.Headers);