Carter.Cache.Redis
0.4.0
dotnet add package Carter.Cache.Redis --version 0.4.0
NuGet\Install-Package Carter.Cache.Redis -Version 0.4.0
<PackageReference Include="Carter.Cache.Redis" Version="0.4.0" />
<PackageVersion Include="Carter.Cache.Redis" Version="0.4.0" />
<PackageReference Include="Carter.Cache.Redis" />
paket add Carter.Cache.Redis --version 0.4.0
#r "nuget: Carter.Cache.Redis, 0.4.0"
#:package Carter.Cache.Redis@0.4.0
#addin nuget:?package=Carter.Cache.Redis&version=0.4.0
#tool nuget:?package=Carter.Cache.Redis&version=0.4.0
Carter.Cache 
An extensible library to cache your Carter modules.
Builds
| Github | Branch | Coverage |
|---|---|---|
| master |
Packages
| Package | NuGet (Stable) | MyGet (Prerelease) |
|---|---|---|
| Carter.Cache | ||
| Carter.Cache.Memcached | ||
| Carter.Cache.Redis |
Installation
Install via nuget
PM> Install-Package Carter.Cache
This library depends on Carter to properly work, you can install Carter using the following command:
PM> Install-Package Carter
Sample usage
- Add carter caching to your Program.cs:
var builder = WebApplication.CreateBuilder(args);
//The rest of your Program.cs configuration ....
//It is recommended to always provide a caching max size limit
builder.Services.AddCarterCaching(new CachingOption(2048));
builder.Services.AddCarter();
- Add the caching middleware before mapping Carter modules:
var app = builder.Build();
//The rest of your configuration usage ....
app.UseCarterCaching();
app.MapCarter();
app.Run();
- Mark a response as cacheable inside your route handler:
public class HomeModule : ICarterModule
{
public void AddRoutes(IEndpointRouteBuilder app)
{
app.MapGet("/", (HttpContext ctx) =>
{
ctx.AsCacheable(10); //In Seconds
ctx.Response.StatusCode = 200;
return ctx.Response.WriteAsync("Hello world");
});
}
}
The default configuration does use the Microsoft.Extensions.Caching.Memory library as a default caching mechanism. Note: By default memory caching can put lots of pressure on the memory of your system, please refer to the following microsoft in-memory cache docs for basics on usage.
Customization
Define a custom store by implementing ICacheStore, including the inherited Dispose() method:
public interface ICacheStore : IDisposable
{
bool TryGetValue(string key, out CachedResponse cachedResponse);
void Set(string key, CachedResponse response, TimeSpan expiration);
void Remove(string key);
}
Also a custom Key can be easily defined by implementing the ICacheKey interface:
public interface ICacheKey
{
string Get(HttpRequest request);
}
Redis store
A redis store which includes the dependency on StackExchange.Redis and can be used as a replacement of the memory store.
Install using dotnet add package Carter.Cache.Redis or Package Manager Install-Package Carter.Cache.Redis. In Program.cs, replace the default in-memory caching registration with:
//The rest of your Program.cs ....
builder.Services.AddSingleton<ICacheStore>(new RedisStore("127.0.0.1:6379"));
builder.Services.AddSingleton(provider => new CachingOption()
{
Store = provider.GetRequiredService<ICacheStore>()
});
IServiceProvider serviceProvider = builder.Services.BuildServiceProvider();
builder.Services.AddCarterCaching(serviceProvider.GetRequiredService<CachingOption>());
builder.Services.AddCarter();
You can optionally pass System.Text.Json serializer options to RedisStore:
//The rest of your Program.cs ....
var jsonOptions = new System.Text.Json.JsonSerializerOptions() { AllowTrailingCommas = true };
builder.Services.AddSingleton<ICacheStore>(new RedisStore("127.0.0.1:6379", jsonOptions));
builder.Services.AddSingleton(provider => new CachingOption()
{
Store = provider.GetRequiredService<ICacheStore>()
});
IServiceProvider serviceProvider = builder.Services.BuildServiceProvider();
builder.Services.AddCarterCaching(serviceProvider.GetRequiredService<CachingOption>());
builder.Services.AddCarter();
Memcached store
Alternatively, use the Memcached store backed by EnyimMemcachedCore.
Install using dotnet add package Carter.Cache.Memcached or Package Manager Install-Package Carter.Cache.Memcached. In Program.cs, replace the default in-memory caching registration with:
//The rest of your Program.cs ....
//Point to the server / port desired
builder.Services.AddEnyimMemcached(options => options.AddServer("127.0.0.1", 11211));
//Resolve the IMemcachedClient dependency using EnyimMemcached
builder.Services.AddSingleton<ICacheStore>(provider => new MemcachedStore(provider.GetRequiredService<IMemcachedClient>()));
//Define Caching options using the store configured
builder.Services.AddSingleton(provider => new CachingOption()
{
Store = provider.GetRequiredService<ICacheStore>()
});
IServiceProvider serviceProvider = builder.Services.BuildServiceProvider();
//Pass it as a dependency to the add
builder.Services.AddCarterCaching(serviceProvider.GetRequiredService<CachingOption>());
builder.Services.AddCarter();
For more information check the samples included.
The sample applications expose their OpenAPI document at /openapi/v1.json and the Scalar API reference at /scalar.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Carter.Cache (>= 0.4.0)
- StackExchange.Redis (>= 2.9.32)
-
net8.0
- Carter.Cache (>= 0.4.0)
- StackExchange.Redis (>= 2.9.32)
- System.Text.Json (>= 10.0.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Addition of target: .NET 10