A thread-safe in-memory key-value store with optimistic concurrency support. Great for testing / mocking and prototyping.
PM> Install-Package AndreasDorfer.InMemoryStore -Version 1.4.0
using AD.InMemoryStore;
KeyValueStore<int, string> store = new();
try
{
var version = store.Add(key: 1, value: "A");
}
catch (DuplicateKeyException<int> ex)
{ }
try
{
var (value, version) = store.Get(key: 1);
}
catch (KeyNotFoundException<int> ex)
{ }
foreach (var (key, value, version) in store.GetAll())
{ }
try
{
var newVersion = store.Update(key: 1, value: "B", match: version);
}
catch (VersionMismatchException<int> ex)
{ }
try
{
var newVersion = store.Update(key: 1, value: "B", match: null);
}
catch (KeyNotFoundException<int> ex)
{ }
try
{
store.Remove(key: 1, match: version);
}
catch (VersionMismatchException<int> ex)
{ }
try
{
store.Remove(key: 1, match: null);
}
catch (KeyNotFoundException<int> ex)
{ }
A functional wrapper around AD.InMemoryStore
. Optimized for F#.
PM> Install-Package AndreasDorfer.InMemoryStore.Functional -Version 1.4.0
open AD.InMemoryStore.Functional
let store = KeyValueStore<int, string> ()
match store.Add(key = 1, value = "A") with
| Ok (key, value, version) -> ()
| Error (AddError.DuplicateKey key) -> ()
Or if you don't care about the specific error type:
match store.Add(key = 1, value = "A") with
| Ok (key, value, version) -> ()
| Error (ErrorKey key) -> ()
match store.Get(key = 1) with
| Ok (key, value, version) -> ()
| Error (GetError.KeyNotFound key) -> ()
Or if you don't care about the specific error type:
match store.Get(key = 1) with
| Ok (key, value, version) -> ()
| Error (ErrorKey key) -> ()
for (key, value, version) in store.GetAll() do
()
match store.Update(key = 1, value = "B", ``match`` = Some version) with
| Ok (key, value, version) -> ()
| Error error ->
match error with
| UpdateError.VersionMismatch key -> ()
| _ -> ()
Or if you don't care about the specific error type:
match store.Update(key = 1, value = "B", ``match`` = Some version) with
| Ok (key, value, version) -> ()
| Error (ErrorKey key) -> ()
match store.Update(key = 1, value = "B", ``match`` = None) with
| Ok (key, value, version) -> ()
| Error error ->
match error with
| UpdateError.KeyNotFound key -> ()
| _ -> ()
Or if you don't care about the specific error type:
match store.Update(key = 1, value = "B", ``match`` = None) with
| Ok (key, value, version) -> ()
| Error (ErrorKey key) -> ()
match store.Remove(key = 1, ``match`` = Some version) with
| Ok key -> ()
| Error error ->
match error with
| RemoveError.VersionMismatch key -> ()
| _ -> ()
Or if you don't care about the specific error type:
match store.Remove(key = 1, ``match`` = Some version) with
| Ok key -> ()
| Error (ErrorKey key) -> ()
match store.Remove(key = 1, ``match`` = None) with
| Ok key -> ()
| Error error ->
match error with
| RemoveError.KeyNotFound key -> ()
| _ -> ()
Or if you don't care about the specific error type:
match store.Remove(key = 1, ``match`` = None) with
| Ok key -> ()
| Error (ErrorKey key) -> ()