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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Carter.Cache.Redis" Version="0.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Carter.Cache.Redis" Version="0.4.0" />
                    
Directory.Packages.props
<PackageReference Include="Carter.Cache.Redis" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Carter.Cache.Redis --version 0.4.0
                    
#r "nuget: Carter.Cache.Redis, 0.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Carter.Cache.Redis@0.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Carter.Cache.Redis&version=0.4.0
                    
Install as a Cake Addin
#tool nuget:?package=Carter.Cache.Redis&version=0.4.0
                    
Install as a Cake Tool

Carter.Cache Mit License

An extensible library to cache your Carter modules.

Builds

Github Branch Coverage
.NET master CodeCov

Packages

Package NuGet (Stable) MyGet (Prerelease)
Carter.Cache NuGet MyGet
Carter.Cache.Memcached NuGet MyGet
Carter.Cache.Redis NuGet MyGet

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

  1. 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();
  1. Add the caching middleware before mapping Carter modules:
var app = builder.Build();

//The rest of your configuration usage ....

app.UseCarterCaching();
app.MapCarter();

app.Run();

  1. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0 118 9/16/2026
0.3.2 204 1/13/2026
0.3.1 283 10/23/2025
0.3.0 303 12/11/2024
0.2.0 345 9/7/2023
0.1.1 470 1/28/2023
0.1.0 485 1/9/2023
0.0.8 512 12/31/2021
0.0.7 562 11/3/2021
0.0.6 614 9/11/2021
0.0.5 557 6/30/2021

Addition of target: .NET 10