/// <summary> /// Creates a new <see cref="OutputCacheMiddleware"/>. /// </summary> /// <param name="next">The <see cref="RequestDelegate"/> representing the next middleware in the pipeline.</param> /// <param name="options">The options for this middleware.</param> /// <param name="loggerFactory">The <see cref="ILoggerFactory"/> used for logging.</param> /// <param name="outputCache">The <see cref="IOutputCacheStore"/> store.</param> /// <param name="poolProvider">The <see cref="ObjectPoolProvider"/> used for creating <see cref="ObjectPool"/> instances.</param> public OutputCacheMiddleware( RequestDelegate next, IOptions <OutputCacheOptions> options, ILoggerFactory loggerFactory, IOutputCacheStore outputCache, ObjectPoolProvider poolProvider ) : this( next, options, loggerFactory, outputCache, new OutputCacheKeyProvider(poolProvider, options)) { }
// for testing internal OutputCacheMiddleware( RequestDelegate next, IOptions <OutputCacheOptions> options, ILoggerFactory loggerFactory, IOutputCacheStore cache, IOutputCacheKeyProvider keyProvider) { ArgumentNullException.ThrowIfNull(next); ArgumentNullException.ThrowIfNull(options); ArgumentNullException.ThrowIfNull(loggerFactory); ArgumentNullException.ThrowIfNull(cache); ArgumentNullException.ThrowIfNull(keyProvider); _next = next; _options = options.Value; _logger = loggerFactory.CreateLogger <OutputCacheMiddleware>(); _store = cache; _keyProvider = keyProvider; _outputCacheEntryDispatcher = new(); _requestDispatcher = new(); }
public static async ValueTask <OutputCacheEntry?> GetAsync(string key, IOutputCacheStore store, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(key); var content = await store.GetAsync(key, cancellationToken); if (content == null) { return(null); } var formatter = JsonSerializer.Deserialize(content, FormatterEntrySerializerContext.Default.FormatterEntry); if (formatter == null) { return(null); } var outputCacheEntry = new OutputCacheEntry { StatusCode = formatter.StatusCode, Created = formatter.Created, Tags = formatter.Tags, Headers = new(), Body = new CachedResponseBody(formatter.Body, formatter.Body.Sum(x => x.Length)) }; if (formatter.Headers != null) { foreach (var header in formatter.Headers) { outputCacheEntry.Headers.TryAdd(header.Key, header.Value); } } return(outputCacheEntry); }
public static async ValueTask StoreAsync(string key, OutputCacheEntry value, TimeSpan duration, IOutputCacheStore store, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(value); ArgumentNullException.ThrowIfNull(value.Body); ArgumentNullException.ThrowIfNull(value.Headers); var formatterEntry = new FormatterEntry { StatusCode = value.StatusCode, Created = value.Created, Tags = value.Tags, Body = value.Body.Segments }; if (value.Headers != null) { formatterEntry.Headers = new(); foreach (var header in value.Headers) { formatterEntry.Headers.TryAdd(header.Key, header.Value.ToArray()); } } using var bufferStream = new MemoryStream(); JsonSerializer.Serialize(bufferStream, formatterEntry, FormatterEntrySerializerContext.Default.FormatterEntry); await store.SetAsync(key, bufferStream.ToArray(), value.Tags ?? Array.Empty <string>(), duration, cancellationToken); }