Andreas-Dorfer / in-memory-store

A thread-safe in-memory key-value store with optimistic concurrency support.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NuGet Package

AD.InMemoryStore

A thread-safe in-memory key-value store with optimistic concurrency support. Great for testing / mocking and prototyping.

NuGet Package

PM> Install-Package AndreasDorfer.InMemoryStore -Version 1.4.0

Namespace

using AD.InMemoryStore;

Create a Store

KeyValueStore<int, string> store = new();

Add a Value

try
{
    var version = store.Add(key: 1, value: "A");
}
catch (DuplicateKeyException<int> ex)
{ }

Get a Value

try
{
    var (value, version) = store.Get(key: 1);
}
catch (KeyNotFoundException<int> ex)
{ }

Get all Values

foreach (var (key, value, version) in store.GetAll())
{ }

Update a Value

try
{
    var newVersion = store.Update(key: 1, value: "B", match: version);
}
catch (VersionMismatchException<int> ex)
{ }

Update a Value with No Version Check

try
{
    var newVersion = store.Update(key: 1, value: "B", match: null);
}
catch (KeyNotFoundException<int> ex)
{ }

Remove a Value

try
{
    store.Remove(key: 1, match: version);
}
catch (VersionMismatchException<int> ex)
{ }

Remove a Value with No Version Check

try
{
    store.Remove(key: 1, match: null);
}
catch (KeyNotFoundException<int> ex)
{ }

NuGet Package

AD.InMemoryStore.Functional

A functional wrapper around AD.InMemoryStore. Optimized for F#.

NuGet Package

PM> Install-Package AndreasDorfer.InMemoryStore.Functional -Version 1.4.0

Namespace

open AD.InMemoryStore.Functional

Create a Store

let store = KeyValueStore<int, string> ()

Add a Value

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) -> ()

Get a Value

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) -> ()

Get all Values

for (key, value, version) in store.GetAll() do
    ()

Update a Value

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) -> ()

Update a Value with No Version Check

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) -> ()

Remove a Value

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) -> ()

Remove a Value with No Version Check

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) -> ()

About

A thread-safe in-memory key-value store with optimistic concurrency support.

License:MIT License


Languages

Language:C# 55.0%Language:F# 45.0%