Skip to content

Repository files navigation

KeyValueLite

A netstandard key value store, backed by sqlite, alternative to Akavache

(c)2018-2019 Benjamin Mayrargue
MIT License

NuGet

Features

  • 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.

Setup

Add the nuget to your netstandard project
NuGet

Initialization code to get the cacheService singleton instance:

//Init SQLiteSQLitePCL.Batteries_V2.Init();//Use an already existing logger factory, or create a new one with this code:varloggerFactory=newMicrosoft.Extensions.Logging.LoggerFactory();varlogger=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.vardsFactory=newDataStoreFactory(newGenericPlatformService());cacheService=newKeyValueLite(dsFactory,newKeyValueItemNewtonsoftJsonSerializer(),logger);

Usage

Get a value, or create it if it does not exist

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>(stringkey,Func<T>create)
Task<T>GetOrFetchObject<T>(stringkey,Func<Task<T>>create,DateTimeOffset?expiresOn=null)

Samples:

varvalue=awaitGetOrCreateObject("sample key",()=>"sample string value")
var value=awaitGetOrCreateObject("sample key",()=>newSomeObject{SomeProperty=12})
var value =await GetOrFetchObject("sample key",async()=>awaithttpClient.GetAync("https://happy/api/method"));

Add values

Add a list of value
Persists the specified key, value and expiration, updating it if the key already exists.

TaskInsertObjects<T>(Dictionary<string,T>keyValuePairs,DateTimeOffset?expiresOn=null)TaskSet(stringkey,objectvalue,DateTimeOffset?expiresOn=null)

Samples:

awaitInsertObjects(newDictionary<string,IPAddress>(){{"someKey",someIp},{"someKey2",someIp2}},DateTimeOffset.Now.AddDays(1));awaitSet("someKey",someObject,DateTimeOffset.Now.AddMinutes(60));

Retrieve a value

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>(stringkey)
Task<List<T>>GetAll<T>()

Samples:

varvalue=awaitGet<string>("sample key");varvalues=awaitGetAll<string>();

Delete a value

Usage scenario
Delete an object from the cache Delete all objects of this type from the cache

TaskRemove(stringkey)TaskRemoveAll<T>()

Samples:

awaitRemove("the key");awaitRemoveAll<string>();

If the key does not exist, it does nothing.

Usage (advanced)

Get the internal keyValueItem matching this key, or null

varkeyValueItem=awaitkeyValueStore.Get("sample key");

Persists the specified KeyValueItem, updating it if the key already exists.

TaskSet(KeyValueItemkeyValueItem);

Removes the specified key value item.

TaskRemove(KeyValueItemkeyValueItem)

About Newtonsoft.Json versus System.Text.Json

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.

About

A netstandard key value store, backed by sqlite, alternative to Akavache

Topics

Resources

Stars

6 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages