From 3b0f01a8eccc394110fd5324ef74a813f5d9b6d9 Mon Sep 17 00:00:00 2001 From: John Luo Date: Wed, 31 Aug 2016 17:52:05 -0700 Subject: [PATCH] Customize key prefix instead of suffix --- ...cs => IResponseCachingCacheKeyModifier.cs} | 8 ++--- ...fixProvider.cs => NoopCacheKeyModifier.cs} | 4 +-- .../ResponseCachingContext.cs | 30 ++++++++----------- .../ResponseCachingMiddleware.cs | 12 ++++---- ...ponseCachingServiceCollectionExtensions.cs | 2 +- .../ResponseCachingContextTests.cs | 22 +++++++------- 6 files changed, 37 insertions(+), 41 deletions(-) rename src/Microsoft.AspNetCore.ResponseCaching/Interfaces/{IResponseCachingCacheKeySuffixProvider.cs => IResponseCachingCacheKeyModifier.cs} (57%) rename src/Microsoft.AspNetCore.ResponseCaching/Internal/{NoopCacheKeySuffixProvider.cs => NoopCacheKeyModifier.cs} (62%) diff --git a/src/Microsoft.AspNetCore.ResponseCaching/Interfaces/IResponseCachingCacheKeySuffixProvider.cs b/src/Microsoft.AspNetCore.ResponseCaching/Interfaces/IResponseCachingCacheKeyModifier.cs similarity index 57% rename from src/Microsoft.AspNetCore.ResponseCaching/Interfaces/IResponseCachingCacheKeySuffixProvider.cs rename to src/Microsoft.AspNetCore.ResponseCaching/Interfaces/IResponseCachingCacheKeyModifier.cs index 7514fa4254..d78c27cd90 100644 --- a/src/Microsoft.AspNetCore.ResponseCaching/Interfaces/IResponseCachingCacheKeySuffixProvider.cs +++ b/src/Microsoft.AspNetCore.ResponseCaching/Interfaces/IResponseCachingCacheKeyModifier.cs @@ -5,13 +5,13 @@ using Microsoft.AspNetCore.Http; namespace Microsoft.AspNetCore.ResponseCaching { - public interface IResponseCachingCacheKeySuffixProvider + public interface IResponseCachingCacheKeyModifier { /// - /// Create a key segment that is appended to the default cache key. + /// Create a key segment that is prepended to the default cache key. /// /// The . - /// The key segment that will be appended to the default cache key. - string CreateCustomKeySuffix(HttpContext httpContext); + /// The key segment that will be prepended to the default cache key. + string CreatKeyPrefix(HttpContext httpContext); } } diff --git a/src/Microsoft.AspNetCore.ResponseCaching/Internal/NoopCacheKeySuffixProvider.cs b/src/Microsoft.AspNetCore.ResponseCaching/Internal/NoopCacheKeyModifier.cs similarity index 62% rename from src/Microsoft.AspNetCore.ResponseCaching/Internal/NoopCacheKeySuffixProvider.cs rename to src/Microsoft.AspNetCore.ResponseCaching/Internal/NoopCacheKeyModifier.cs index 43daeb5a5d..a32332c366 100644 --- a/src/Microsoft.AspNetCore.ResponseCaching/Internal/NoopCacheKeySuffixProvider.cs +++ b/src/Microsoft.AspNetCore.ResponseCaching/Internal/NoopCacheKeyModifier.cs @@ -5,8 +5,8 @@ using Microsoft.AspNetCore.Http; namespace Microsoft.AspNetCore.ResponseCaching.Internal { - internal class NoopCacheKeySuffixProvider : IResponseCachingCacheKeySuffixProvider + internal class NoopCacheKeyModifier : IResponseCachingCacheKeyModifier { - public string CreateCustomKeySuffix(HttpContext httpContext) => null; + public string CreatKeyPrefix(HttpContext httpContext) => null; } } diff --git a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingContext.cs b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingContext.cs index 7caa7b285b..17ebe49caf 100644 --- a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingContext.cs +++ b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingContext.cs @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.ResponseCaching private readonly ISystemClock _clock; private readonly ObjectPool _builderPool; private readonly IResponseCachingCacheabilityValidator _cacheabilityValidator; - private readonly IResponseCachingCacheKeySuffixProvider _cacheKeySuffixProvider; + private readonly IResponseCachingCacheKeyModifier _cacheKeyModifier; private string _cacheKey; private ResponseType? _responseType; @@ -46,8 +46,8 @@ namespace Microsoft.AspNetCore.ResponseCaching IResponseCache cache, ObjectPool builderPool, IResponseCachingCacheabilityValidator cacheabilityValidator, - IResponseCachingCacheKeySuffixProvider cacheKeySuffixProvider) - : this(httpContext, cache, new SystemClock(), builderPool, cacheabilityValidator, cacheKeySuffixProvider) + IResponseCachingCacheKeyModifier cacheKeyModifier) + : this(httpContext, cache, new SystemClock(), builderPool, cacheabilityValidator, cacheKeyModifier) { } @@ -58,14 +58,14 @@ namespace Microsoft.AspNetCore.ResponseCaching ISystemClock clock, ObjectPool builderPool, IResponseCachingCacheabilityValidator cacheabilityValidator, - IResponseCachingCacheKeySuffixProvider cacheKeySuffixProvider) + IResponseCachingCacheKeyModifier cacheKeyModifier) { _httpContext = httpContext; _cache = cache; _clock = clock; _builderPool = builderPool; _cacheabilityValidator = cacheabilityValidator; - _cacheKeySuffixProvider = cacheKeySuffixProvider; + _cacheKeyModifier = cacheKeyModifier; } internal bool CacheResponse @@ -153,6 +153,14 @@ namespace Microsoft.AspNetCore.ResponseCaching try { + // Prepend custom cache key prefix + var customKeyPrefix = _cacheKeyModifier.CreatKeyPrefix(_httpContext); + if (!string.IsNullOrEmpty(customKeyPrefix)) + { + builder.Append(customKeyPrefix) + .Append(KeyDelimiter); + } + // Default key builder .Append(request.Method.ToUpperInvariant()) @@ -225,18 +233,6 @@ namespace Microsoft.AspNetCore.ResponseCaching } } - // Append custom cache key segment - var customKey = _cacheKeySuffixProvider.CreateCustomKeySuffix(_httpContext); - if (!string.IsNullOrEmpty(customKey)) - { - // Append a group separator for the custom segment of the cache key - builder.Append(KeyDelimiter) - .Append('C'); - - builder.Append(KeyDelimiter) - .Append(customKey); - } - return builder.ToString(); } finally diff --git a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingMiddleware.cs b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingMiddleware.cs index 9c6a922eb8..5fe8718134 100644 --- a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingMiddleware.cs +++ b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingMiddleware.cs @@ -22,14 +22,14 @@ namespace Microsoft.AspNetCore.ResponseCaching private readonly IResponseCache _cache; private readonly ObjectPool _builderPool; private readonly IResponseCachingCacheabilityValidator _cacheabilityValidator; - private readonly IResponseCachingCacheKeySuffixProvider _cacheKeySuffixProvider; + private readonly IResponseCachingCacheKeyModifier _cacheKeyModifier; public ResponseCachingMiddleware( RequestDelegate next, IResponseCache cache, ObjectPoolProvider poolProvider, IResponseCachingCacheabilityValidator cacheabilityValidator, - IResponseCachingCacheKeySuffixProvider cacheKeySuffixProvider) + IResponseCachingCacheKeyModifier cacheKeyModifier) { if (next == null) { @@ -47,16 +47,16 @@ namespace Microsoft.AspNetCore.ResponseCaching { throw new ArgumentNullException(nameof(cacheabilityValidator)); } - if (cacheKeySuffixProvider == null) + if (cacheKeyModifier == null) { - throw new ArgumentNullException(nameof(cacheKeySuffixProvider)); + throw new ArgumentNullException(nameof(cacheKeyModifier)); } _next = next; _cache = cache; _builderPool = poolProvider.CreateStringBuilderPool(); _cacheabilityValidator = cacheabilityValidator; - _cacheKeySuffixProvider = cacheKeySuffixProvider; + _cacheKeyModifier = cacheKeyModifier; } public async Task Invoke(HttpContext context) @@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.ResponseCaching _cache, _builderPool, _cacheabilityValidator, - _cacheKeySuffixProvider); + _cacheKeyModifier); // Should we attempt any caching logic? if (cachingContext.RequestIsCacheable()) diff --git a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingServiceCollectionExtensions.cs b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingServiceCollectionExtensions.cs index b6e18f7874..c2ad4a050d 100644 --- a/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingServiceCollectionExtensions.cs @@ -40,7 +40,7 @@ namespace Microsoft.Extensions.DependencyInjection private static IServiceCollection AddResponseCachingServices(this IServiceCollection services) { - services.TryAdd(ServiceDescriptor.Singleton()); + services.TryAdd(ServiceDescriptor.Singleton()); services.TryAdd(ServiceDescriptor.Singleton()); return services; diff --git a/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingContextTests.cs b/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingContextTests.cs index 54bd5a55ba..e66a33af06 100644 --- a/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingContextTests.cs +++ b/test/Microsoft.AspNetCore.ResponseCaching.Tests/ResponseCachingContextTests.cs @@ -258,22 +258,22 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests })); } - private class CustomizeKeySuffixProvider : IResponseCachingCacheKeySuffixProvider + private class KeyModifier : IResponseCachingCacheKeyModifier { - public string CreateCustomKeySuffix(HttpContext httpContext) => "CustomizedKey"; + public string CreatKeyPrefix(HttpContext httpContext) => "CustomizedKeyPrefix"; } [Fact] - public void CreateCacheKey_OptionalCacheKey_AppendedToDefaultKey() + public void CreateCacheKey_CacheKeyModifier_AddsPrefix() { var httpContext = new DefaultHttpContext(); httpContext.Request.Method = "GET"; httpContext.Request.Path = "/"; httpContext.Request.Headers["HeaderA"] = "ValueA"; httpContext.Request.Headers["HeaderB"] = "ValueB"; - var responseCachingContext = CreateTestContext(httpContext, new CustomizeKeySuffixProvider()); + var responseCachingContext = CreateTestContext(httpContext, new KeyModifier()); - Assert.Equal($"GET{KeyDelimiter}/{KeyDelimiter}H{KeyDelimiter}HeaderA=ValueA{KeyDelimiter}HeaderC=null{KeyDelimiter}C{KeyDelimiter}CustomizedKey", responseCachingContext.CreateCacheKey(new CachedVaryBy() + Assert.Equal($"CustomizedKeyPrefix{KeyDelimiter}GET{KeyDelimiter}/{KeyDelimiter}H{KeyDelimiter}HeaderA=ValueA{KeyDelimiter}HeaderC=null", responseCachingContext.CreateCacheKey(new CachedVaryBy() { Headers = new string[] { "HeaderA", "HeaderC" } })); @@ -831,15 +831,15 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests { return CreateTestContext( httpContext, - new NoopCacheKeySuffixProvider(), + new NoopCacheKeyModifier(), new NoopCacheabilityValidator()); } - private static ResponseCachingContext CreateTestContext(HttpContext httpContext, IResponseCachingCacheKeySuffixProvider cacheKeySuffixProvider) + private static ResponseCachingContext CreateTestContext(HttpContext httpContext, IResponseCachingCacheKeyModifier cacheKeyModifier) { return CreateTestContext( httpContext, - cacheKeySuffixProvider, + cacheKeyModifier, new NoopCacheabilityValidator()); } @@ -847,13 +847,13 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests { return CreateTestContext( httpContext, - new NoopCacheKeySuffixProvider(), + new NoopCacheKeyModifier(), cacheabilityValidator); } private static ResponseCachingContext CreateTestContext( HttpContext httpContext, - IResponseCachingCacheKeySuffixProvider cacheKeySuffixProvider, + IResponseCachingCacheKeyModifier cacheKeyModifier, IResponseCachingCacheabilityValidator cacheabilityValidator) { return new ResponseCachingContext( @@ -862,7 +862,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests new SystemClock(), new DefaultObjectPool(new StringBuilderPooledObjectPolicy()), cacheabilityValidator, - cacheKeySuffixProvider); + cacheKeyModifier); } private class TestResponseCache : IResponseCache