Always overwrite the VaryBy entry to ensure expiry is updated

This commit is contained in:
John Luo 2016-09-16 13:40:35 -07:00
parent d710e44c63
commit 7f638c1385
2 changed files with 11 additions and 7 deletions

View File

@ -201,7 +201,6 @@ namespace Microsoft.AspNetCore.ResponseCaching
if (_policyProvider.IsResponseCacheable(context)) if (_policyProvider.IsResponseCacheable(context))
{ {
context.ShouldCacheResponse = true; context.ShouldCacheResponse = true;
context.BaseKey = _keyProvider.CreateBaseKey(context);
// Create the cache entry now // Create the cache entry now
var response = context.HttpContext.Response; var response = context.HttpContext.Response;
@ -230,10 +229,11 @@ namespace Microsoft.AspNetCore.ResponseCaching
Headers = normalizedVaryHeaderValue, Headers = normalizedVaryHeaderValue,
Params = normalizedVaryParamsValue 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); context.StorageVaryKey = _keyProvider.CreateStorageVaryByKey(context);
} }

View File

@ -320,7 +320,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
} }
[Fact] [Fact]
public void FinalizeCacheHeaders_UpdateCachedVaryByRules_IfNotEquivalentToPrevious() public async Task FinalizeCacheHeaders_UpdateCachedVaryByRules_IfNotEquivalentToPrevious()
{ {
var store = new TestResponseCacheStore(); var store = new TestResponseCacheStore();
var middleware = TestUtils.CreateTestMiddleware(store); var middleware = TestUtils.CreateTestMiddleware(store);
@ -336,6 +336,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
}; };
context.CachedVaryByRules = cachedVaryByRules; context.CachedVaryByRules = cachedVaryByRules;
await middleware.TryServeFromCacheAsync(context);
middleware.FinalizeCacheHeaders(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(1, store.SetCount); Assert.Equal(1, store.SetCount);
@ -343,7 +344,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
} }
[Fact] [Fact]
public void FinalizeCacheHeaders_DoNotUpdateCachedVaryByRules_IfEquivalentToPrevious() public async Task FinalizeCacheHeaders_DoNotUpdateCachedVaryByRules_IfEquivalentToPrevious()
{ {
var store = new TestResponseCacheStore(); var store = new TestResponseCacheStore();
var middleware = TestUtils.CreateTestMiddleware(store); var middleware = TestUtils.CreateTestMiddleware(store);
@ -360,9 +361,11 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
}; };
context.CachedVaryByRules = cachedVaryByRules; context.CachedVaryByRules = cachedVaryByRules;
await middleware.TryServeFromCacheAsync(context);
middleware.FinalizeCacheHeaders(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); Assert.Same(cachedVaryByRules, context.CachedVaryByRules);
} }
@ -411,12 +414,13 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
} }
[Fact] [Fact]
public void FinalizeCachingHeaders_SplitsVaryHeaderByCommas() public async Task FinalizeCacheHeaders_SplitsVaryHeaderByCommas()
{ {
var middleware = TestUtils.CreateTestMiddleware(); var middleware = TestUtils.CreateTestMiddleware();
var context = TestUtils.CreateTestContext(); var context = TestUtils.CreateTestContext();
context.HttpContext.Response.Headers[HeaderNames.Vary] = "HeaderB, heaDera"; context.HttpContext.Response.Headers[HeaderNames.Vary] = "HeaderB, heaDera";
await middleware.TryServeFromCacheAsync(context);
middleware.FinalizeCacheHeaders(context); middleware.FinalizeCacheHeaders(context);
Assert.Equal(new StringValues(new[] { "HEADERA", "HEADERB" }), context.CachedVaryByRules.Headers); Assert.Equal(new StringValues(new[] { "HEADERA", "HEADERB" }), context.CachedVaryByRules.Headers);