GM.Caching 1.1.0

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

<p align="center"> <img src="https://raw.githubusercontent.com/gmetskhvarishvili/GM.Caching/master/icon.png" alt="GM.Caching" width="140" height="140" /> </p>

GM.Caching

CI NuGet License: MIT

A small, provider-agnostic caching abstraction for .NET. Depend on one interface — ICacheService — with typed get/set, GetOrCreateAsync (single-flight, no cache stampede), and absolute/sliding expiration. Ships an in-memory implementation; add a distributed backend with GM.Caching.Redis. Targets .NET 10.

Packages

The two packages version and release together (lockstep):

Package What it gives you
GM.Caching ICacheService, CacheEntryOptions, and an in-memory implementation over IMemoryCache (AddGMCaching()).
GM.Caching.Redis A Redis implementation over StackExchange.Redis (AddGMRedisCaching()) — same interface, distributed.
dotnet add package GM.Caching          # in-memory
dotnet add package GM.Caching.Redis    # + Redis backend

Quick start

In-memory

using GM.Caching;

builder.Services.AddGMCaching();               // optionally: o => o.KeyPrefix = "myapp:"

Redis

using GM.Caching.Redis;

builder.Services.AddGMRedisCaching(o =>
{
    o.ConnectionString = "localhost:6379";
    o.KeyPrefix = "myapp:";
});
// or bind from configuration: builder.Services.AddGMRedisCaching(builder.Configuration, "Redis");

It replaces the in-memory cache if AddGMCaching() ran first (GM.API registers it by default), so the order of the two calls doesn't matter; any other ICacheService you registered still wins.

Use it

Inject ICacheService — the same code works with either backend:

public class WeatherService(ICacheService cache, IWeatherApi api)
{
    public Task<Forecast> GetAsync(string city) =>
        cache.GetOrCreateAsync(
            $"forecast:{city}",
            ct => api.FetchAsync(city, ct),
            CacheEntryOptions.Absolute(TimeSpan.FromMinutes(10)));
}

GetOrCreateAsync is single-flight: if many callers ask for the same missing key at once, the factory runs once and everyone gets that result — no stampede on your database or upstream API.

The interface

Task<T?>  GetAsync<T>(string key, CancellationToken ct = default);
Task      SetAsync<T>(string key, T value, CacheEntryOptions? options = null, CancellationToken ct = default);
Task<T>   GetOrCreateAsync<T>(string key, Func<CancellationToken, Task<T>> factory, CacheEntryOptions? options = null, CancellationToken ct = default);
Task<bool> ExistsAsync(string key, CancellationToken ct = default);
Task      RemoveAsync(string key, CancellationToken ct = default);

CacheEntryOptions.Absolute(ttl) / CacheEntryOptions.Sliding(window) set per-entry expiration; CacheServiceOptions.KeyPrefix and DefaultAbsoluteExpiration apply provider-wide. The Redis provider JSON-serializes values and refreshes the key TTL on read for sliding entries.

Repository layout

GM.Caching/              # ICacheService, options, in-memory implementation
GM.Caching.Redis/        # Redis implementation (StackExchange.Redis)
tests/GM.Caching.Tests/  # xUnit tests for the in-memory cache

Building & testing

dotnet build -c Release
dotnet test  -c Release

Releasing

Versioning is automated from Conventional Commits — see CONTRIBUTING.md. Both packages share one version (Directory.Build.props) and publish together to nuget.org on each release.

License

MIT — see LICENSE.

Product Compatible and additional computed target framework versions.
.NET 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 (8)

Showing the top 5 NuGet packages that depend on GM.Caching:

Package Downloads
GM.API

An opinionated ASP.NET Core Web API starter for .NET. A single AddGMAPI/UseGMAPI pair wires up controllers, CORS, API versioning, Swagger docs, FluentValidation, Serilog, and middlewares for exception handling, request logging (with sensitive-data masking) and request localization. Loads secrets into configuration from a local appsecrets.json or the store the SECRETS_PROVIDER environment variable names (Doppler, AWS Secrets Manager, Azure Key Vault, HashiCorp Vault). Bundles the GM.API.* companion packages.

GM.Idempotency

Idempotency-key management for the GM.* ecosystem. A provider-agnostic IIdempotencyService (IsProcessed / MarkAsProcessed / TryGetCachedResult, plus a race-safe ExecuteAsync check-and-set) backed by GM.Caching for TTL-based dedup-key storage and guarded by GM.DistributedLock so two near-simultaneous duplicates cannot both pass the "not yet processed" gate. Stores enough of the original result to replay it safely. Flexible key strategy (caller-supplied or derived/hashed) and one call — AddGMIdempotency() — to wire it up. HTTP middleware and GM.Mediator behavior ship in the GM.Idempotency.Http / .Mediator packages.

GM.RateLimiting

Distributed rate limiting for the GM.* ecosystem. A provider-agnostic IRateLimiterService with policy-based checks (TryAcquireAsync(key, policy)) and three per-policy algorithms — fixed window, sliding window, and token bucket. Counters live in GM.Caching so limits hold across every service instance (unlike ASP.NET Core's per-process limiter), and the check-and-increment is made atomic by a swappable store (default backed by GM.DistributedLock). Composable key strategies (per-user / per-API-key / per-IP / per-endpoint), policies from appsettings or fluent code, and a 429-friendly result (limit / remaining / retry-after). One call — AddGMRateLimiting() — to wire it up.

GM.Caching.Redis

A Redis-backed ICacheService for GM.Caching (StackExchange.Redis): distributed, JSON-serialized caching with GetOrCreateAsync, key prefixing, and absolute/sliding expiration. Register with AddGMRedisCaching().

GM.HealthChecks.Caching

Cache health check for GM.HealthChecks: does a Set/Get/Remove round-trip through GM.Caching's ICacheService, so it verifies whichever backend is registered (in-memory or Redis). Register with AddGMCacheCheck().

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 332 11/8/2026
1.1.0 142 11/8/2026
1.0.1 1,314 8/31/2026
1.0.0 1,920 8/2/2026