CSharpBrasil.Extensions.Caching.LiteDb 0.1.1

dotnet add package CSharpBrasil.Extensions.Caching.LiteDb --version 0.1.1
                    
NuGet\Install-Package CSharpBrasil.Extensions.Caching.LiteDb -Version 0.1.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="CSharpBrasil.Extensions.Caching.LiteDb" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpBrasil.Extensions.Caching.LiteDb" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="CSharpBrasil.Extensions.Caching.LiteDb" />
                    
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 CSharpBrasil.Extensions.Caching.LiteDb --version 0.1.1
                    
#r "nuget: CSharpBrasil.Extensions.Caching.LiteDb, 0.1.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.
#addin nuget:?package=CSharpBrasil.Extensions.Caching.LiteDb&version=0.1.1
                    
Install CSharpBrasil.Extensions.Caching.LiteDb as a Cake Addin
#tool nuget:?package=CSharpBrasil.Extensions.Caching.LiteDb&version=0.1.1
                    
Install CSharpBrasil.Extensions.Caching.LiteDb as a Cake Tool

CSharpBrasil.Extensions.Caching.LiteDb

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.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  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.  net10.0 was computed.  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

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
0.1.1 138 5/1/2025
0.1.0 135 5/1/2025