Skip to content

A simple library to use SQLite as a in-disk cache, for failure purposes. Can act as some of a document database, though is not recomended.

License

Notifications You must be signed in to change notification settings

proxfield/Proxfield.Extensions.Caching.SQLite

Repository files navigation

Proxfield.Extensions.Caching.SQLite Logo

Proxfield.Extensions.Caching.SQLite

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.

GitHub License Actions Nuget GitHub branch checks state GitHub code size in bytes

Packages

Packages and versions available at the Nuget Gallery.

Package Version Downloads
Proxfield.Extensions.Caching.SQLite Nuget version Nuget downloads
Proxfield.Extensions.Caching.SQLite.DependencyInjection Nuget version Nuget downloads

Installation

PM> Install-Package Proxfield.Extensions.Caching.SQLite

For applications using Microsoft.Extensions.DependencyInjection, use the DI package:

PM> Install-Package Proxfield.Extensions.Caching.SQLite.DependencyInjection

Visit the Nuget Repository Page to learn more.

Usage

Initialization can be done via standard instantiation or through Dependency Injection.

Common Initialization

var cache = new SQLiteCache(options =>
{
    options.Location = @"c:\cache\database.sqlite";
});

Dependency Injection Initialization

services.AddSQLiteCache(options => {
    options.Location = @"c:\cache\database.sqlite";
});

Configuration

Database File Location

If options.Location is not provided, the database will be stored in the same folder as the running application.

Encryption

To enable data encryption, set UseEncryption to true and provide an EncryptionKey:

services.AddSQLiteCache(options => {
    options.UseEncryption = true;
    options.EncryptionKey = "d5644e8105ad77c3c3324ba693e83d8f";
});

Caching Methods

Data can be stored/retrieved as simple strings or complex objects.

Basic Usage

// 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");

Available Methods

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.

Collections and Indexes

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

Index Management Methods

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.

Platform Support

SQLite Caching is compiled for:

  • .NET 6
  • .NET 5
  • .NET Core 3.1

License

GitHub License

The MIT License (MIT) - Copyright (c) 2022-2026 Proxfield Consulting Group and its affiliates.

About

A simple library to use SQLite as a in-disk cache, for failure purposes. Can act as some of a document database, though is not recomended.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages