Skip to content

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.

License

Notifications You must be signed in to change notification settings

csharpbrasil/CSharpBrasil.Extensions.Caching.LiteDb

Repository files navigation

CSharpBrasil.Extensions.Caching.LiteDb

NuGet Downloads

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.

Features

  • 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 with FileMode.Exclusive
  • Integrates cleanly via IServiceCollection

Installation

Install via NuGet:

dotnet add package CSharpBrasil.Extensions.Caching.LiteDb

Configuration Options

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;
}

✅ Usage in ASP.NET Core (Minimal API or MVC)

builder.Services.AddLiteDbDistributedCache(options =>
{
    options.DatabasePath = "cache.db";
    options.CollectionName = "cache";
    options.EnableAutoCleanup = true;
    options.CleanupInterval = TimeSpan.FromMinutes(5);
    options.Password = "secure123";
});

Controller Example

[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)");
    }
}

✅ Usage in a Console Application (with Host)

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();

License

This project is licensed under the MIT License © C# Brasil.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages