An implementation of a Hybrid Output Cache store using HybridCache plus more to support dependency injection (DI)
This repository allows the implementation of IDistributedCache services ie Sql Server Cache, NCache, and Azure CosmosDb be used for cache storage and Distributed Output Caching in a production environment using the new HybridCache library currently in preview mode. Prior to this, developers would have to invest in a Redis Cache to provide consistent shared access between server nodes in a distributed architecture. The new HybridCache looks to bridge the gap between IMemoryCache and RedisCache.
Important
This project is based on packages that are in evaluation therefore we will not be creating an official nuget package.
In the meantime, add a folder to your project and copy the three files in the repo: HybridOutputCacheOptionsSetup.cs, HybridOutputCacheStore.cs, and OutputHybridCacheServiceExtensions.cs
Other warnings from Microsoft:
HybridCache is currently still in preview but will be fully released after .NET 9.0 in a future minor release of .NET Extensions.
Remove Cache By Tag
Add your IDistriubtedCache service:
builder
.Services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = builder.Configuration.GetValue<string>(CacheSettings.ConnectionString);
options.SchemaName = CacheSettings.SchemaName;
options.TableName = CacheSettings.TableName;
})
Add HybridCache (preview warning):
#pragma warning disable EXTEXP0018 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
builder
.Services.AddHybridCache(options =>
{
options.DefaultEntryOptions = new Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryOptions() { Flags = Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryFlags.DisableLocalCache };
});
#pragma warning restore EXTEXP0018 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
Add HybridOutputCache:
builder.Services.AddHybridOutputCache(options =>
{
options.AddBasePolicy(builder =>
builder.Expire(TimeSpan.FromMinutes(10)));
});
Use OutputCache as you normally would after you build your services:
app.UseOutputCache();
Finally attach the CacheOutput() to your endpoint:
app.MapRemoteBffApiEndpoint("/catalog", builder.Configuration.GetValue<string>(HttpClientBaseAddresses.CatalogApi) + "/api/catalog")
.CacheOutput();