GarnetWrapper 1.0.1

dotnet add package GarnetWrapper --version 1.0.1                
NuGet\Install-Package GarnetWrapper -Version 1.0.1                
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="GarnetWrapper" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GarnetWrapper --version 1.0.1                
#r "nuget: GarnetWrapper, 1.0.1"                
#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.
// Install GarnetWrapper as a Cake Addin
#addin nuget:?package=GarnetWrapper&version=1.0.1

// Install GarnetWrapper as a Cake Tool
#tool nuget:?package=GarnetWrapper&version=1.0.1                

GarnetWrapper

A high-performance .NET wrapper for Microsoft Research's Garnet cache-store. This library provides a robust, feature-rich interface for interacting with Garnet cache, including support for compression, circuit breaking, and comprehensive metrics collection.

Features

  • 🚀 High-performance async operations
  • 🔄 Circuit breaker pattern for fault tolerance
  • 📊 Built-in metrics collection
  • 🗜️ Optional compression support
  • 🔒 Distributed locking capabilities
  • ⚡ Connection pooling and multiplexing
  • 📈 Health monitoring
  • 🔍 Comprehensive logging

Installation

dotnet add package GarnetWrapper

Quick Start

// Add services to DI container
services.AddGarnetCache(options =>
{
    options.Endpoints = new[] { "localhost:6379" };
    options.DefaultDatabase = 0;
    options.EnableCompression = true;
});

// Inject and use in your code
public class MyService
{
    private readonly IGarnetClient _cache;

    public MyService(IGarnetClient cache)
    {
        _cache = cache;
    }

    public async Task<User> GetUserAsync(string userId)
    {
        return await _cache.GetAsync<User>($"user:{userId}");
    }
}

Configuration Options

public class GarnetOptions
{
    public string[] Endpoints { get; set; }
    public int DefaultDatabase { get; set; }
    public bool EnableCompression { get; set; }
    public TimeSpan DefaultExpiry { get; set; }
    public int MaxRetries { get; set; }
    public int RetryTimeout { get; set; }
}

Advanced Usage

Distributed Locking

// Acquire a lock
await _cache.LockAsync("my-lock-key", TimeSpan.FromMinutes(1));

// Release the lock
await _cache.UnlockAsync("my-lock-key");

Pattern Scanning

await foreach (string key in _cache.ScanAsync("user:*"))
{
    // Process each key
}

Metrics

The library includes built-in metrics collection using App.Metrics:

  • Operation latency
  • Cache hits/misses
  • Error rates
  • Circuit breaker status

Health Checks

Built-in health checks are available and can be configured with ASP.NET Core:

services.AddHealthChecks()
    .AddGarnetCheck();

Performance

Benchmark results for basic operations (runs on typical development machine):

Operation Mean Error StdDev
Set 152.3 μs 15.23 μs 0.89 μs
Get 148.7 μs 14.87 μs 0.87 μs
Delete 149.1 μs 14.91 μs 0.88 μs

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
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
1.0.1 35 1/7/2025
1.0.0 27 1/7/2025