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.
Packages and versions available at the Nuget Galery.
Package | Version | Downloads |
---|---|---|
Proxfield.Extensions.Caching.SQLite | ||
Proxfield.Extensions.Caching.SQLite.DependencyInjection |
PM> Install-Package Proxfield.Extensions.Caching.SQLite
For application who uses Microsoft.Extensions.DependencyInjection there is a package available for using the library with DI:
PM> Install-Package Proxfield.Extensions.Caching.SQLite.DependencyInjection
Visit out project at the Nuget Repository Page to know more.
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.
The initialization can be either by using Microsoft's dependency injection or a common initialization.
var cache = new SQLiteCache(options =>
{
options.Location = @"c:\cache\database.sqlite";
});
services.AddSQLiteCache(options => {
options.Location = @"c:\cache\database.sqlite";
});
If the options.Location
is not provided the database will be stored on the
same folder as the projet which implement the library is running.
To enable the encryption of all the data on the caching database one's just need to set the UseEncryption
to true
and set the EncryptionKey
to any string, as shown bellow:
services.AddSQLiteCache(options => {
options.UseEncryption = true;
options.EncryptionKey = "d5644e8105ad77c3c3324ba693e83d8fffd54950"
});
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 |
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 |
SQLite Caching is compiled for the following versions of frameworks:
- DotNet 6
- DotNet 5
- DotNet Core 3.1
The MIT License (MIT) - Copyright (c) 2022-2023 Proxfield Consulting Group and its affiliates