The SQLite Caching Library is a layer for caching data on SQLite to be used as a secondary database in case of failures and network inconsistencies.
This library is based on Microsoft.Extensions.Caching.StackExchangeRedis for memory caching, but uses SQLite as the data store. It serves as a persistent cache layer, leveraging Microsoft.Data.Sqlite.
Packages and versions available at the Nuget Gallery.
| Package | Version | Downloads |
|---|---|---|
| Proxfield.Extensions.Caching.SQLite | ||
| Proxfield.Extensions.Caching.SQLite.DependencyInjection |
PM> Install-Package Proxfield.Extensions.Caching.SQLiteFor applications using Microsoft.Extensions.DependencyInjection, use the DI package:
PM> Install-Package Proxfield.Extensions.Caching.SQLite.DependencyInjectionVisit the Nuget Repository Page to learn more.
Initialization can be done via standard instantiation or through Dependency Injection.
var cache = new SQLiteCache(options =>
{
options.Location = @"c:\cache\database.sqlite";
});services.AddSQLiteCache(options => {
options.Location = @"c:\cache\database.sqlite";
});If options.Location is not provided, the database will be stored in the same folder as the running application.
To enable data encryption, set UseEncryption to true and provide an EncryptionKey:
services.AddSQLiteCache(options => {
options.UseEncryption = true;
options.EncryptionKey = "d5644e8105ad77c3c3324ba693e83d8f";
});Data can be stored/retrieved as simple strings or complex objects.
// Store as string
this.cache.SetAsString("users/1", "jose");
var userName = this.cache.GetAsString("users/1");
// Store as object
this.cache.SetAsObject<User>("users/1", new User() { Name = "Jose" });
var user = this.cache.GetAsObject<User>("users/1");| Method | Description |
|---|---|
byte[] Get(string key) |
Retrieves a cached resource from the database. |
Task<byte[]> GetAsync(string key) |
Retrieves a cached resource asynchronously. |
void Set(string key, byte[] value) |
Sets a cached resource in the database. |
Task SetAsync(string key, byte[] value) |
Sets a cached resource asynchronously. |
void Remove(string key) |
Removes a cached resource from the database. |
Task RemoveAsync(string key) |
Removes a cached resource asynchronously. |
void SetAsString(string key, string value) |
Sets a string in the database. |
Task SetAsStringAsync(string key, string value, CancellationToken token = default) |
Sets a string in the database asynchronously. |
void SetAsObject<T>(string key, T value) |
Sets an object in the database. |
Task SetAsObjectAsync<T>(string key, T value, CancellationToken token = default) |
Sets an object in the database asynchronously. |
string GetAsString(string key) |
Retrieves a string from the database. |
Task<string> GetAsStringAsync(string key, CancellationToken token = default) |
Retrieves a string from the database asynchronously. |
T? GetAsObject<T>(string key) |
Retrieves an object from the database. |
Task<T?> GetAsObjectAsync<T>(string key, CancellationToken token = default) |
Retrieves an object from the database asynchronously. |
List<T> GetAsObjectStartsWith<T>(string key, int start = 0, int pageSize = int.MaxValue) |
Gets a list of objects where the key starts with the specified string. |
Task<List<T>?> GetAsObjectStartsWithAsync<T>(string key, int start = 0, int pageSize = int.MaxValue, CancellationToken token = default) |
Gets a list of objects where the key starts with the specified string asynchronously. |
List<string> GetAsStringStartsWith(string key, int start = 0, int pageSize = int.MaxValue) |
Gets a list of strings where the key starts with the specified string. |
Task<List<string>?> GetAsStringStartsWithAsync(string key, int start = 0, int pageSize = int.MaxValue, CancellationToken token = default) |
Gets a list of strings where the key starts with the specified string asynchronously. |
void ClearCache() |
Clears all items from the cache (excluding indexes). |
Task ClearCacheAsync(CancellationToken token = default) |
Clears all items from the cache asynchronously. |
You can index cached objects/strings. For example, saving an object with key vehicles/1:
cache.SetAsObject("vehicles/1", new { Name = "bicycle" });This makes it possible to query multiple objects at once (e.g., every document in a collection):
cache.GetAsObjectStartsWith<Vehicle>("vehicles");Accessed via cache.Maintenance.
| Method | Description |
|---|---|
List<SQLiteCacheIndex> GetAllIndexes() |
Returns all indexes in the database. |
SQLiteCacheIndex? GetIndex(string name) |
Returns a specific index. |
void ClearAllIndexers() |
Purges all indexes from the database. |
void ResetIndex(string name, long? value = null) |
Resets an index to a specific value. |
SQLite Caching is compiled for:
- .NET 6
- .NET 5
- .NET Core 3.1
The MIT License (MIT) - Copyright (c) 2022-2026 Proxfield Consulting Group and its affiliates.
