Skip to content

stebet/Stebet.Nats.DistributedCache

Repository files navigation

Stebet.Nats.DistributedCache

codecov NuGet License

A distributed cache implementation for .NET Core using NATS 2.11 or higher as the backing store. This package provides a simple way to integrate NATS-based distributed caching into your ASP.NET Core applications.

Features

  • Implementation of the IDistributedCache interface
    • REQUIRES NATS Server 2.11 OR HIGHER
    • Sliding expiration is not currently supported

Installation

Install the package from NuGet:

dotnet add package Stebet.Nats.DistributedCache

Or via the NuGet Package Manager:

Install-Package Stebet.Nats.DistributedCache

Basic Usage

Add to services in Program.cs or Startup.cs

using Stebet.Nats.DistributedCache;

...
// Add a NATS connection (see https://www.nuget.org/packages/NATS.Extensions.Microsoft.DependencyInjection)
builder.services.AddNatsClient(nats => nats.ConfigureOptions(opts => opts with { Url = "nats://localhost:4222" }));

// Add NATS distributed cache
builder.Services.AddNatsDistributedCache(options =>
{
    options.BucketName = "MyCacheBucket";
});
...

Use the cache in your controllers or services

using Microsoft.Extensions.Caching.Distributed;

public class WeatherController : Controller
{
    private readonly IDistributedCache _cache;

    public WeatherController(IDistributedCache cache)
    {
        _cache = cache;
    }

    public async Task<IActionResult> GetForecast(string location)
    {
        var cacheKey = $"weather:{location}";
        
        // Try to get from cache first
        var cachedForecast = await _cache.GetStringAsync(cacheKey);
        if (cachedForecast != null)
        {
            return Ok(JsonSerializer.Deserialize<WeatherForecast>(cachedForecast));
        }
        
        // Cache miss - get from service
        var forecast = await _weatherService.GetForecastAsync(location);
        
        // Save to cache with expiration
        var options = new DistributedCacheEntryOptions
        {
            AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
        };
        
        await _cache.SetStringAsync(
            cacheKey, 
            JsonSerializer.Serialize(forecast),
            options);
            
        return Ok(forecast);
    }
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published