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

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.

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

Packages

Packages and versions available at the Nuget Galery.

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

Nuget

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.

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 Initialization

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

Database file location

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.

Encryption

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

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

Platform Support

SQLite Caching is compiled for the following versions of frameworks:

  • DotNet 6
  • DotNet 5
  • DotNet Core 3.1

License

GitHub License

The MIT License (MIT) - Copyright (c) 2022-2023 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