Cachey.Core 1.0.3

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

Cachey

Cachey is a lightweight, flexible caching library for .NET applications. It supports both in-memory and persistent caching with easy integration and customization.

Features

  • In-memory caching for fast and lightweight operations.
  • Persistent caching for durable data storage.
  • Automatic cleanup of expired items with customizable intervals.
  • Seamless integration with dependency injection (DI) and configuration.

Installation

Add Cachey to your project using your package manager:

dotnet add package Cachey

Quick Start

1. Register Cachey in DI

You can register Cachey in your application's DI container using the UseCachey method. Here's an example:

builder.Services.UseCachey(builder.Configuration)
    .AddCacheCleanupService(TimeSpan.FromHours(1));
  • UseCachey integrates Cachey into your application, dynamically deciding whether to use in-memory or persistent cache based on configuration.
  • AddCacheCleanupService registers a background service to periodically clean up expired cache items.

2. Configuring Persistence

The caching behavior is determined by the Cache section in your application's configuration file (e.g., appsettings.json).

Example Configuration:
{
  "Cache": {
    "UsePersistentCache": true
  }
}
  • Set UsePersistentCache to true to enable persistent caching.
  • When persistent caching is enabled, Cachey will use a SQLite database to store cache entries.
Using a Custom Connection String:

To specify a custom SQLite connection string, add the following to your configuration:

{
  "Cache": {
    "UsePersistentCache": true,
    "ConnectionString": "DataSource=mycustomcache.db"
  }
}

Cachey automatically configures a default SQLite database (mycache.db) if a custom connection string is not provided.

3. Accessing Cachey in Your Code

Inject the ICache interface into your services or controllers to start using Cachey:

public class MyService
{
    private readonly ICache _cache;

    public MyService(ICache cache)
    {
        _cache = cache;
    }

    public async Task DoWorkAsync()
    {
        // Store a value in the cache
        await _cache.SetAsync("key", "value", TimeSpan.FromMinutes(10));

        // Retrieve a value from the cache
        var cachedValue = await _cache.GetAsync<string>("key");

        // Remove a value from the cache
        await _cache.RemoveAsync("key");
    }
}

Adding Cleanup Service

The cleanup service automatically removes expired cache items at regular intervals. You can customize the interval like this:

builder.Services.UseCachey(builder.Configuration)
    .AddCacheCleanupService(TimeSpan.FromMinutes(30));

Cleanup Behavior

The cleanup service runs periodically, based on the specified interval, to ensure expired items are removed from the cache.

Full Example

Here’s a complete example of setting up Cachey in an ASP.NET Core application:

Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add Cachey
builder.Services.UseCachey(builder.Configuration)
    .AddCacheCleanupService(TimeSpan.FromHours(1));

var app = builder.Build();

app.MapGet("/", async (ICache cache) =>
{
    // Set a cache entry
    await cache.SetAsync("example", "Hello, Cachey!", TimeSpan.FromMinutes(5));

    // Retrieve the cached value
    var value = await cache.GetAsync<string>("example");
    return value ?? "Cache entry not found!";
});

app.Run();

appsettings.json

{
  "Cache": {
    "UsePersistentCache": true,
    "ConnectionString": "DataSource=mycustomcache.db"
  }
}

License

Cachey is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET 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
1.0.3 178 12/6/2024
1.0.2 140 12/5/2024
1.0.1 168 12/5/2024
1.0.0 154 12/2/2024