A lightweight and embeddable implementation of IDistributedCache
using LiteDB, designed for .NET applications that require local distributed caching without external dependencies such as Redis or SQL Server.
- Local persistent storage using a single
.db
file - Supports absolute and sliding expiration
- Fully managed and dependency-free in .NET
- Compatible with ASP.NET Core, Console Apps and .NET 6/7/8+
- Background cleanup of expired cache entries
- Uses a single
LiteDatabase
instance withFileMode.Exclusive
- Integrates cleanly via
IServiceCollection
Install via NuGet:
dotnet add package CSharpBrasil.Extensions.Caching.LiteDb
public class LiteDbDistributedCacheOptions
{
public string DatabasePath { get; set; } = "cache.db";
public string CollectionName { get; set; } = "cache";
public bool EnableAutoCleanup { get; set; } = true;
public TimeSpan CleanupInterval { get; set; } = TimeSpan.FromMinutes(10);
public bool ReadOnly { get; set; } = false;
public string? Password { get; set; }
public bool Upgrade { get; set; } = false;
public bool AutoRebuild { get; set; } = false;
public long InitialSize { get; set; } = 0;
public Collation Collation { get; set; } = Collation.Default;
}
builder.Services.AddLiteDbDistributedCache(options =>
{
options.DatabasePath = "cache.db";
options.CollectionName = "cache";
options.EnableAutoCleanup = true;
options.CleanupInterval = TimeSpan.FromMinutes(5);
options.Password = "secure123";
});
[ApiController]
[Route("api/cache")]
public class CacheController : ControllerBase
{
private readonly IDistributedCache _cache;
public CacheController(IDistributedCache cache)
{
_cache = cache;
}
[HttpPost("{key}")]
public async Task<IActionResult> Set(string key, [FromBody] string value)
{
await _cache.SetStringAsync(key, value, new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)
});
return Ok();
}
[HttpGet("{key}")]
public async Task<IActionResult> Get(string key)
{
var value = await _cache.GetStringAsync(key);
return Ok(value ?? "(not found)");
}
}
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Caching.Distributed;
using CSharpBrasil.Extensions.Caching.LiteDb;
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddLiteDbDistributedCache(options =>
{
options.DatabasePath = "console-cache.db";
options.CollectionName = "cache";
options.Password = "secure123";
});
})
.Build();
var cache = host.Services.GetRequiredService<IDistributedCache>();
await cache.SetStringAsync("message", "Olá mundo!", new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(30)
});
var result = await cache.GetStringAsync("message");
Console.WriteLine($"Mensagem em cache: {result}");
await host.StopAsync();
This project is licensed under the MIT License © C# Brasil.