NeoSmart.Caching.Sqlite.AspNetCore 8.0.0

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package NeoSmart.Caching.Sqlite.AspNetCore --version 8.0.0                
NuGet\Install-Package NeoSmart.Caching.Sqlite.AspNetCore -Version 8.0.0                
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="NeoSmart.Caching.Sqlite.AspNetCore" Version="8.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NeoSmart.Caching.Sqlite.AspNetCore --version 8.0.0                
#r "nuget: NeoSmart.Caching.Sqlite.AspNetCore, 8.0.0"                
#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.
// Install NeoSmart.Caching.Sqlite.AspNetCore as a Cake Addin
#addin nuget:?package=NeoSmart.Caching.Sqlite.AspNetCore&version=8.0.0

// Install NeoSmart.Caching.Sqlite.AspNetCore as a Cake Tool
#tool nuget:?package=NeoSmart.Caching.Sqlite.AspNetCore&version=8.0.0                

SqliteCache for ASP.NET Core

SqliteCache is a persistent cache implementing IDistributedCache for .NET and ASP.NET Core projects.

SqliteCache uses a locally stored SQLite database file (taking advantage of SQLite's battle-tested safe multi-threaded access features) to replicate persistent caching, allowing developers to mimic the behavior of staging or production targets without all the overhead or hassle of a traditional IDistributedCache implementation. You can read more about its design and inspiration in the official release post on the NeoSmart blog.

Why NeoSmart.Caching.Sqlite?

The currently available options for caching in ASP.NET Core projects are either all ephemeral in-memory cache offerings (IMemoryCache and co.) -- aka non-persistent -- or else have a whole slew of dependencies and requirements that require at the very least administrator privileges and background services hogging up system resources and needing updates and maintenance to requiring multiple machines and a persistent network configuration.

  • NeoSmart.Caching.Sqlite has no dependencies on background services that hog system resources and need to be updated or maintained (cough cough NCache cough cough)
  • NeoSmart.Caching.Sqlite is fully cross-platform and runs the same on your Windows PC or your colleagues' Linux, FreeBSD, and macOS workstations (unlike, say, Redis)
  • NeoSmart.Caching.Sqlite doesn't need administrator privileges to install - or even any installation for that matter (SQL Express LocalDB, this one is aimed at you)
  • NeoSmart.Caching.Sqlite is a fully contained IDistributedCache offering that is installed and updated alongside the rest of your packages via NuGet, Paket, or whatever other option you're already using to manage your dependencies.

Installation

SqliteCache is available via the NuGet, and can be installed in the Package Manager Console as follows:

Install-Package NeoSmart.Caching.Sqlite

If using this in an ASP.NET Core project, you can install NeoSmart.Caching.Sqlite.AspNetCore (also or instead) to get a convenient helper method for dependency injection (used below):

Install-Package NeoSmart.Caching.Sqlite.AspNetCore

If you install NeoSmart.Caching.Sqlite.AspNetCore you do not need to manually install NeoSmart.Caching.Sqlite as it it will be installed automatically/transitively.

Usage

Using SqliteCache is straight-forward, and should be extremely familiar for anyone that's configured an ASP.NET Core application before. Starting by adding a namespace import using NeoSmart.Caching.Sqlite makes things easier as the editor will pull in the correct extension methods.

If using SqliteCache in an ASP.NET Core project, the SQLite-backed cache should be added as an IDistributedCache type by adding the following to your ConfigureServices method, by default located in Startup.cs, after using the correct namespace NeoSmart.Caching.Sqlite.AspNetCore:

// using NeoSmart.Caching.Sqlite.AspNetCore;

public void ConfigureServices(IServiceCollection services)
{
    ...

    // Note: this *must* come before services.AddMvc()!
    services.AddSqliteCache(options => {
        options.CachePath = @"C:\data\bazaar\cache.db";
    });

    services.AddMvc();

    ...
}

Afterwards, the SqliteCache instance will be made available to both the framework and the application via dependency injection, and can be imported and used via either the IDistributedCache abstract type or the concrete SqliteCache type:

// using Microsoft.Extensions.Caching.Distributed;
public class FooModel(DbContext db, IDistributedCache cache)
{
    _db = db;
    _cache = cache;

    cache.SetString("foo", "bar");
    Assert.AreEqual(cache.GetString("foo"), "bar");

    Assert.AreEqual(typeof(NeoSmart.Caching.Sqlite.SqliteCache),
                    cache.GetType());
}

To take advantage of SqliteCache-specific features or functionality that aren't exposed via the IDistributedCache façade, you'll need to inject SqliteCache into your classes/methods rather than IDistributedCache. For example, to globally clear the cache after performing some operation:

// using NeoSmart.Caching.Sqlite;
public class BarModel(DbContext db, SqliteCache cache)
{
    _db = db;
    _cache = cache;
}

public ActionResult OnPostAsync()
{
    ...
    await _db.SomethingDestructiveAsync();

    // We need to invalidate all the cache, since it's too hard to
    // account for the changes this operation caused for legacy reasons.
    await _cache.ClearAsync();

    ...
}

License

SqliteCache is developed and maintained by Mahmoud Al-Qudsi of NeoSmart Technologies. The project is provided free to the community under the terms of the MIT open source license.

Contributing

We are open to pull requests and contributions aimed at the code, documentation, unit tests, or anything else. If you're mulling an extensive contribution, file an issue first to make sure we're all on the same page, otherwise, PR away!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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 (1)

Showing the top 1 popular GitHub repositories that depend on NeoSmart.Caching.Sqlite.AspNetCore:

Repository Stars
simpleidserver/SimpleIdServer
OpenID, OAuth 2.0, SCIM2.0, UMA2.0, FAPI, CIBA & OPENBANKING Framework for ASP.NET Core
Version Downloads Last updated
9.0.0-preview1 76 10/26/2024
8.0.0 32,053 3/2/2024
7.0.2 12,417 10/27/2023
7.0.1 470 9/19/2023