Internalize iresponsecachekeyprovider (#59)
Make IResponseCacheKeyProvider internal
This commit is contained in:
parent
10381b0456
commit
24385e74c4
|
|
@ -40,7 +40,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
|
|
||||||
private static IServiceCollection AddResponseCacheServices(this IServiceCollection services)
|
private static IServiceCollection AddResponseCacheServices(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.TryAdd(ServiceDescriptor.Singleton<IResponseCacheKeyProvider, ResponseCacheKeyProvider>());
|
|
||||||
services.TryAdd(ServiceDescriptor.Singleton<IResponseCachePolicyProvider, ResponseCachePolicyProvider>());
|
services.TryAdd(ServiceDescriptor.Singleton<IResponseCachePolicyProvider, ResponseCachePolicyProvider>());
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.ResponseCaching
|
namespace Microsoft.AspNetCore.ResponseCaching.Internal
|
||||||
{
|
{
|
||||||
public interface IResponseCacheKeyProvider
|
internal interface IResponseCacheKeyProvider
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a base key for a response cache entry.
|
/// Create a base key for a response cache entry.
|
||||||
|
|
@ -10,9 +10,9 @@ using Microsoft.Extensions.ObjectPool;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.ResponseCaching
|
namespace Microsoft.AspNetCore.ResponseCaching.Internal
|
||||||
{
|
{
|
||||||
public class ResponseCacheKeyProvider : IResponseCacheKeyProvider
|
internal class ResponseCacheKeyProvider : IResponseCacheKeyProvider
|
||||||
{
|
{
|
||||||
// Use the record separator for delimiting components of the cache key to avoid possible collisions
|
// Use the record separator for delimiting components of the cache key to avoid possible collisions
|
||||||
private static readonly char KeyDelimiter = '\x1e';
|
private static readonly char KeyDelimiter = '\x1e';
|
||||||
|
|
@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
|
||||||
private readonly ObjectPool<StringBuilder> _builderPool;
|
private readonly ObjectPool<StringBuilder> _builderPool;
|
||||||
private readonly ResponseCacheOptions _options;
|
private readonly ResponseCacheOptions _options;
|
||||||
|
|
||||||
public ResponseCacheKeyProvider(ObjectPoolProvider poolProvider, IOptions<ResponseCacheOptions> options)
|
internal ResponseCacheKeyProvider(ObjectPoolProvider poolProvider, IOptions<ResponseCacheOptions> options)
|
||||||
{
|
{
|
||||||
if (poolProvider == null)
|
if (poolProvider == null)
|
||||||
{
|
{
|
||||||
|
|
@ -35,13 +35,13 @@ namespace Microsoft.AspNetCore.ResponseCaching
|
||||||
_options = options.Value;
|
_options = options.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual IEnumerable<string> CreateLookupVaryByKeys(ResponseCacheContext context)
|
public IEnumerable<string> CreateLookupVaryByKeys(ResponseCacheContext context)
|
||||||
{
|
{
|
||||||
return new string[] { CreateStorageVaryByKey(context) };
|
return new string[] { CreateStorageVaryByKey(context) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET<delimiter>/PATH
|
// GET<delimiter>/PATH
|
||||||
public virtual string CreateBaseKey(ResponseCacheContext context)
|
public string CreateBaseKey(ResponseCacheContext context)
|
||||||
{
|
{
|
||||||
if (context == null)
|
if (context == null)
|
||||||
{
|
{
|
||||||
|
|
@ -67,7 +67,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
|
||||||
}
|
}
|
||||||
|
|
||||||
// BaseKey<delimiter>H<delimiter>HeaderName=HeaderValue<delimiter>Q<delimiter>QueryName=QueryValue
|
// BaseKey<delimiter>H<delimiter>HeaderName=HeaderValue<delimiter>Q<delimiter>QueryName=QueryValue
|
||||||
public virtual string CreateStorageVaryByKey(ResponseCacheContext context)
|
public string CreateStorageVaryByKey(ResponseCacheContext context)
|
||||||
{
|
{
|
||||||
if (context == null)
|
if (context == null)
|
||||||
{
|
{
|
||||||
|
|
@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Http.Features;
|
||||||
using Microsoft.AspNetCore.Http.Headers;
|
using Microsoft.AspNetCore.Http.Headers;
|
||||||
using Microsoft.AspNetCore.ResponseCaching.Internal;
|
using Microsoft.AspNetCore.ResponseCaching.Internal;
|
||||||
using Microsoft.Extensions.Internal;
|
using Microsoft.Extensions.Internal;
|
||||||
|
using Microsoft.Extensions.ObjectPool;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
|
@ -28,6 +29,17 @@ namespace Microsoft.AspNetCore.ResponseCaching
|
||||||
private readonly Func<object, Task> _onStartingCallback;
|
private readonly Func<object, Task> _onStartingCallback;
|
||||||
|
|
||||||
public ResponseCacheMiddleware(
|
public ResponseCacheMiddleware(
|
||||||
|
RequestDelegate next,
|
||||||
|
IResponseCacheStore store,
|
||||||
|
IOptions<ResponseCacheOptions> options,
|
||||||
|
IResponseCachePolicyProvider policyProvider,
|
||||||
|
ObjectPoolProvider poolProvider)
|
||||||
|
:this (next, store, options, policyProvider, new ResponseCacheKeyProvider(poolProvider, options))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal for testing
|
||||||
|
internal ResponseCacheMiddleware(
|
||||||
RequestDelegate next,
|
RequestDelegate next,
|
||||||
IResponseCacheStore store,
|
IResponseCacheStore store,
|
||||||
IOptions<ResponseCacheOptions> options,
|
IOptions<ResponseCacheOptions> options,
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
public async Task TryServeFromCacheAsync_VaryByRuleFound_CachedResponseNotFound_Fails()
|
public async Task TryServeFromCacheAsync_VaryByRuleFound_CachedResponseNotFound_Fails()
|
||||||
{
|
{
|
||||||
var store = new TestResponseCacheStore();
|
var store = new TestResponseCacheStore();
|
||||||
var middleware = TestUtils.CreateTestMiddleware(store: store, keyProvider: new TestResponseCacheKeyProvider("BaseKey"));
|
var middleware = TestUtils.CreateTestMiddleware(store: store, keyProvider: new TestResponseCacheKeyProvider("BaseKey", "VaryKey"));
|
||||||
var context = TestUtils.CreateTestContext();
|
var context = TestUtils.CreateTestContext();
|
||||||
|
|
||||||
await store.SetAsync(
|
await store.SetAsync(
|
||||||
|
|
@ -73,7 +73,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Tests
|
||||||
TimeSpan.Zero);
|
TimeSpan.Zero);
|
||||||
|
|
||||||
Assert.False(await middleware.TryServeFromCacheAsync(context));
|
Assert.False(await middleware.TryServeFromCacheAsync(context));
|
||||||
Assert.Equal(1, store.GetCount);
|
Assert.Equal(2, store.GetCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue