CodeCargo.Nats.DistributedCache 0.2.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package CodeCargo.Nats.DistributedCache --version 0.2.0
                    
NuGet\Install-Package CodeCargo.Nats.DistributedCache -Version 0.2.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="CodeCargo.Nats.DistributedCache" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeCargo.Nats.DistributedCache" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="CodeCargo.Nats.DistributedCache" />
                    
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 CodeCargo.Nats.DistributedCache --version 0.2.0
                    
#r "nuget: CodeCargo.Nats.DistributedCache, 0.2.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.
#addin nuget:?package=CodeCargo.Nats.DistributedCache&version=0.2.0
                    
Install CodeCargo.Nats.DistributedCache as a Cake Addin
#tool nuget:?package=CodeCargo.Nats.DistributedCache&version=0.2.0
                    
Install CodeCargo.Nats.DistributedCache as a Cake Tool

CodeCargo.Nats.DistributedCache CodeCargo.Nats.HybridCacheExtensions

NATS Distributed Cache

Overview

A .NET 8+ library for using NATS with HybridCache or as an IDistributedCache directly.

Requirements

  • NATS 2.11 or later
  • A NATS KV bucket with LimitMarkerTTL set for per-key TTL support. Example:
    // assuming an INatsConnection natsConnection
    var kvContext = natsConnection.CreateKeyValueStoreContext();
    await kvContext.CreateOrUpdateStoreAsync(new NatsKVConfig("cache") { LimitMarkerTTL = TimeSpan.FromSeconds(1) });
    

Use with HybridCache

The CodeCargo.Nats.HybridCacheExtensions package provides an extension method that:

  1. Adds the NATS IDistributedCache
  2. Adds HybridCache
  3. Configures HybridCache to use the NATS Connection's serializer registry

Install

dotnet add package CodeCargo.Nats.HybridCacheExtensions
dotnet add package NATS.Net

Example

using CodeCargo.Nats.HybridCacheExtensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NATS.Client.Core;
using NATS.Client.Hosting;
using NATS.Client.KeyValueStore;
using NATS.Net;

const string natsUrl = "nats://localhost:4222";
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
    services.AddNats(configureOpts: options => options with { Url = natsUrl });

    services.AddNatsHybridCache(options =>
    {
        options.BucketName = "cache";
    });
});

var host = builder.Build();
var natsConnection = host.Services.GetRequiredService<INatsConnection>();
var kvContext = natsConnection.CreateKeyValueStoreContext();
await kvContext.CreateOrUpdateStoreAsync(new NatsKVConfig("cache")
{
    LimitMarkerTTL = TimeSpan.FromSeconds(1)
});

await host.RunAsync();

Use IDistributedCache Directly

Install

dotnet add package CodeCargo.Nats.DistributedCache
dotnet add package NATS.Net

Example

using CodeCargo.Nats.DistributedCache;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NATS.Client.Core;
using NATS.Client.Hosting;
using NATS.Client.KeyValueStore;
using NATS.Net;

const string natsUrl = "nats://localhost:4222";
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
    services.AddNats(configureOpts: options => options with { Url = natsUrl });

    services.AddNatsDistributedCache(options =>
    {
        options.BucketName = "cache";
    });
});

var host = builder.Build();
var natsConnection = host.Services.GetRequiredService<INatsConnection>();
var kvContext = natsConnection.CreateKeyValueStoreContext();
await kvContext.CreateOrUpdateStoreAsync(new NatsKVConfig("cache")
{
    LimitMarkerTTL = TimeSpan.FromSeconds(1)
});

await host.RunAsync();

Additional Resources

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 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on CodeCargo.Nats.DistributedCache:

Package Downloads
CodeCargo.Nats.HybridCacheExtensions

Extensions for using HybridCache with NATS.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.0-preview.2 160 5/26/2025
0.3.0-preview.1 111 5/25/2025
0.2.0 155 5/19/2025
0.2.0-preview.1 121 5/19/2025
0.1.2 147 5/16/2025
0.1.1 170 5/16/2025
0.1.0 186 5/12/2025