A netstandard key value store, backed by sqlite, alternative to Akavache
(c)2018-2019 Benjamin Mayrargue
MIT License
- Just works
- Async operations
- Thread safe
- Fast enough
- Direct write on underlying sqlite database. No need to flush.
- Stores DateTime/DateTimeOffset using ticks, not strings, preserving nanoseconds
- Akavache interface for easy and fast migration: use the
KeyValueLiteLikeAkavache
class.
Add the nuget to your netstandard project
Initialization code to get the cacheService
singleton instance:
//Init SQLite
SQLitePCL.Batteries_V2.Init();
//Use an already existing logger factory, or create a new one with this code:
var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory();
var logger = loggerFactory.CreateLogger(nameof(KeyValueLite));
//Create the cacheService using the sqlite database path provided by the DataStoreFactory class
//Note: you can provide your own IDataStoreFactory implementation.
var dsFactory = new DataStoreFactory(new GenericPlatformService());
cacheService = new KeyValueLite(dsFactory, new KeyValueItemNewtonsoftJsonSerializer(), logger);
Usage scenario
Get an object from the cache, or create it synchronously
Get an object from the cache, or fetch it from a webservice. Optionaly set an expiration time.
Task<T> GetOrCreateObject<T>(string key, Func<T> create)
Task<T> GetOrFetchObject<T>(string key, Func<Task<T>> create, DateTimeOffset? expiresOn = null)
Samples:
var value = await GetOrCreateObject("sample key", () => "sample string value")
var value = await GetOrCreateObject("sample key", () => new SomeObject { SomeProperty = 12 })
var value = await GetOrFetchObject("sample key", async () => await httpClient.GetAync("https://happy/api/method"));
Add a list of value
Persists the specified key, value and expiration, updating it if the key already exists.
Task InsertObjects<T>(Dictionary<string, T> keyValuePairs, DateTimeOffset? expiresOn = null)
Task Set(string key, object value, DateTimeOffset? expiresOn = null)
Samples:
await InsertObjects(new Dictionary<string,IPAddress>() { {"someKey", someIp}, {"someKey2", someIp2} }, DateTimeOffset.Now.AddDays(1));
await Set("someKey", someObject, DateTimeOffset.Now.AddMinutes(60));
Usage scenario
Get an object from the cache, or null if it has expired or is not in the cache
Get all objects of this type from the cache
Task<T> Get<T>(string key)
Task<List<T>> GetAll<T>()
Samples:
var value = await Get<string>("sample key");
var values = await GetAll<string>();
Usage scenario
Delete an object from the cache
Delete all objects of this type from the cache
Task Remove(string key)
Task RemoveAll<T>()
Samples:
await Remove("the key");
await RemoveAll<string>();
If the key does not exist, it does nothing.
Get the internal keyValueItem matching this key, or null
var keyValueItem = await keyValueStore.Get("sample key");
Persists the specified KeyValueItem
, updating it if the key already exists.
Task Set(KeyValueItem keyValueItem);
Removes the specified key value item.
Task Remove(KeyValueItem keyValueItem)
Starting from version 3.0.0, the dependency on Newtonsoft.Json is removed and replaced by System.Text.Json in Vapolia.KeyValueLite.
If you prefer not to use these Json libraries, implement your own IKeyValueItemSerializer and use the Vapolia.KeyValueLite.Core nuget.
Check the KeyValueItemSytemTextJsonSerializer
and KeyValueItemNewtonsoftJsonSerializer
classes for a sample implementation of the IKeyValueItemSerializer interface.