CodeCargo.Nats.DistributedCache 0.1.2

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

NuGet Version

CodeCargo.Nats.DistributedCache

Overview

A .NET 8+ library for integrating NATS as a distributed cache in ASP.NET Core applications. Supports the new HybridCache system for fast, scalable caching.

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) });
    

Installation

# add NATS Distributed Cache
dotnet add package CodeCargo.Nats.DistributedCache

# optional - add full NATS.Net (NATS Distributed Cache uses a subset of NATS.Net dependencies)
dotnet add package NATS.Net

# optional - add HybridCache
dotnet add package Microsoft.Extensions.Caching.Hybrid

Usage

See the Full Example here. This is the portion for registering services:

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;

// Set the NATS URL, this normally comes from configuration
const string natsUrl = "nats://localhost:4222";

// Create a host builder for a Console application
// For a Web Application you can use WebApplication.CreateBuilder(args)
var builder = Host.CreateDefaultBuilder(args);

// Add services to the container
builder.ConfigureServices(services =>
{
    // Add NATS client
    services.AddNats(configureOpts: options => options with { Url = natsUrl });

    // Add a NATS distributed cache
    services.AddNatsDistributedCache(options =>
    {
        options.BucketName = "cache";
    });

    // (Optional) Add HybridCache
    var hybridCacheServices = services.AddHybridCache();

    // (Optional) Use NATS Serializer for HybridCache
    hybridCacheServices.AddSerializerFactory(
        NatsOpts.Default.SerializerRegistry.ToHybridCacheSerializerFactory());

    // Add other services as needed
});

// Build the host
var host = builder.Build();

// Ensure that the KV Store is created
var natsConnection = host.Services.GetRequiredService<INatsConnection>();
var kvContext = natsConnection.CreateKeyValueStoreContext();
await kvContext.CreateOrUpdateStoreAsync(new NatsKVConfig("cache")
{
    LimitMarkerTTL = TimeSpan.FromSeconds(1)
});

// Start the host
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 210 5/26/2025
0.3.0-preview.1 111 5/25/2025
0.2.0 157 5/19/2025
0.2.0-preview.1 122 5/19/2025
0.1.2 148 5/16/2025
0.1.1 171 5/16/2025
0.1.0 187 5/12/2025