Tharga.Cache

A flexible .NET caching library with multiple cache strategies, pluggable persistence backends, eviction policies, and a Blazor monitoring UI. Register one line in DI, inject a cache, and get-or-load with a fetch delegate — the first call loads, subsequent calls within the fresh span return the cached value.

Packages

Package What it does
Tharga.Cache Core library with in-memory caching, the four cache interfaces, options, key building, and monitoring.
Tharga.Cache.Redis Redis persistence backend (IRedis).
Tharga.Cache.MongoDB MongoDB persistence backend (IMongoDB).
Tharga.Cache.File File-based persistence backend (IFile).
Tharga.Cache.Blazor Blazor monitoring UI components.
Tharga.Cache.Mcp MCP (Model Context Protocol) provider for cache monitoring.

Quick start

dotnet add package Tharga.Cache
builder.Services.AddCache();
public class WeatherService(ITimeToLiveCache cache)
{
    public Task<WeatherForecast[]> GetForecastAsync() =>
        cache.GetAsync<WeatherForecast[]>(
            "weather-forecast",
            () => LoadFromApiAsync(),
            TimeSpan.FromMinutes(5));
}

See Getting started for the full walkthrough.

What's in the box

  • Four cache strategiesIEternalCache (never expires), ITimeToLiveCache (TTL), ITimeToIdleCache (TTI, clock resets on access), and IScopeCache (per-DI-scope). See Cache types.
  • Pluggable backends — in-memory by default, with Redis, MongoDB, and file backends you can assign per type and freely mix. See Persistence backends.
  • Smart loading — stale-while-revalidate, background refresh, eviction policies (FIFO / LRU / random), and size/count limits. See Configuration.
  • Monitoring — inspect cache state via ICacheMonitor, a Blazor dashboard, or over MCP. See Monitoring.

Repo

github.com/Tharga/Cache — source, issues, releases.