Skip to main content

SQLite Cache Extension for Dotnet

The SQLite Caching Library is layer for caching data on SQLite to be used as a secondary database in case of failures and network inconsistences.

Example of architecture



How

This caching library is based on the "Microsoft.Extensions.Caching.Redis" for memory caching, but instead of using Redis it uses the SQLite as a data layer. The ideia is to be a library for persistence cache in case of failures. This library uses the "Microsoft.Data" and acts as a layer above the SQLite.

Usage

The initialization can be either by using Microsoft's dependency injection or a common initialization.

Common Initialization

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

Dependency Injection

The commom initialization requires to always declare a new SQLiteCache instance and it can be consuming, depending on how many time do you need to cache data. There is why it is available the dependency injection library:

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

Usage on a class

class AppService
{
    private readonly ISQLiteCache _cache;
    
    public AppService(ISQLiteCache cache)
    {
    	cache = _cache;
    }
}

Cache Methods available

The caching can be recorded/retrieved as a simple string

this.cache.SetAsString("users/1", "jose");
var user = this.cache.GetAsString("users/1");

Or either as a complex object:

this.cache.SetAsObject<User>("users/1", new User() { Name = "Jose" });
var user = this.cache.GetAsObject<User>("users/1");

Cache Methods available

The caching can be recorded/retrieved as a simple string

this.cache.SetAsString("users/1", "jose");
var user = this.cache.GetAsString("users/1");

Or either as a complex object:

this.cache.SetAsObject<User>("users/1", new User() { Name = "Jose" });
var user = this.cache.GetAsObject<User>("users/1");

The following list constains all caching methods avaliable currently on the library.

Method Description
byte[] Get(string key); Retrieves a cached resource from the database
Task<byte[]> GetAsync(string key); Retrieves a cached resource from the database as async
void Set(string key, byte[] value); Sets a cached resource to the database
Task SetAsync(string key, byte[] value); Sets a cached resource to the database async
void Remove(string key); Removes a cached resource to the database
Task RemoveAsync(string key); Removes a cached resource to the database as async
void SetAsString(string key, string value); Sets an string into the the database
void SetAsObject(string key, T value); Sets an object into the the database
string GetAsString(string key); Retrieves a string from the database
T? GetAsObject(string key); Retrieves an object from the database
List<T> GetAsObjectStartsWith(this ISQLiteCache cache, string key) Get a list of objects when the key starts with something
List<string> GetAsStringStartsWith(this ISQLiteCache cache, string key) Get a list of strings when the key starts with something

Collections and Indexes

It is now possible to cache objects/strings by using an index, for example, the following code on a newly created database would save the object with the key as being vehicles/1.

cache.SetAsObject("vehicles|", new { Name = "bycicle" }) ;

Making possible to query more than one object at once, every document on a collection.

cache.GetAsObjectStartsWith<Vehicle>("vehicles");

The following list constains all indexing methods avaliable currently on the library. They can be acessed by the Maintenance property of cache (cache.Maintenance.)

Method Description
List<SQLiteCacheIndex> GetAllIndexes() Returns all indexes on the database
SQLiteCacheIndex? GetIndex(string name Returns an index from the database
void ClearAllIndexers() Purge all indexes from the database
void ResetIndex(string name, long? value = null) Reset an index to an specific value

Links

GitHub

Nuget

Translate