https://github.com/andreas-dorfer/in-memory-store
A thread-safe in-memory key-value store with optimistic concurrency support.
https://github.com/andreas-dorfer/in-memory-store
csharp dotnet fsharp in-memory-storage key-value-store mocking optimistic-concurrency prototyping testing
Last synced: 8 months ago
JSON representation
A thread-safe in-memory key-value store with optimistic concurrency support.
- Host: GitHub
- URL: https://github.com/andreas-dorfer/in-memory-store
- Owner: Andreas-Dorfer
- License: mit
- Created: 2021-07-08T17:24:48.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-01-07T17:28:10.000Z (over 2 years ago)
- Last Synced: 2025-10-10T04:57:18.250Z (8 months ago)
- Topics: csharp, dotnet, fsharp, in-memory-storage, key-value-store, mocking, optimistic-concurrency, prototyping, testing
- Language: C#
- Homepage:
- Size: 187 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/AndreasDorfer.InMemoryStore/)
# 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
```csharp
using AD.InMemoryStore;
```
### Create a Store
```csharp
KeyValueStore store = new();
```
### Add a Value
```csharp
try
{
var version = store.Add(key: 1, value: "A");
}
catch (DuplicateKeyException ex)
{ }
```
### Get a Value
```csharp
try
{
var (value, version) = store.Get(key: 1);
}
catch (KeyNotFoundException ex)
{ }
```
### Get all Values
```csharp
foreach (var (key, value, version) in store.GetAll())
{ }
```
### Update a Value
```csharp
try
{
var newVersion = store.Update(key: 1, value: "B", match: version);
}
catch (VersionMismatchException ex)
{ }
```
### Update a Value with No Version Check
```csharp
try
{
var newVersion = store.Update(key: 1, value: "B", match: null);
}
catch (KeyNotFoundException ex)
{ }
```
### Remove a Value
```csharp
try
{
store.Remove(key: 1, match: version);
}
catch (VersionMismatchException ex)
{ }
```
### Remove a Value with No Version Check
```csharp
try
{
store.Remove(key: 1, match: null);
}
catch (KeyNotFoundException ex)
{ }
```
---
[](https://www.nuget.org/packages/AndreasDorfer.InMemoryStore.Functional/)
# 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
```fsharp
open AD.InMemoryStore.Functional
```
### Create a Store
```fsharp
let store = KeyValueStore ()
```
### Add a Value
```fsharp
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:
```fsharp
match store.Add(key = 1, value = "A") with
| Ok (key, value, version) -> ()
| Error (ErrorKey key) -> ()
```
### Get a Value
```fsharp
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:
```fsharp
match store.Get(key = 1) with
| Ok (key, value, version) -> ()
| Error (ErrorKey key) -> ()
```
### Get all Values
```fsharp
for (key, value, version) in store.GetAll() do
()
```
### Update a Value
```fsharp
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:
```fsharp
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
```fsharp
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:
```fsharp
match store.Update(key = 1, value = "B", ``match`` = None) with
| Ok (key, value, version) -> ()
| Error (ErrorKey key) -> ()
```
### Remove a Value
```fsharp
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:
```fsharp
match store.Remove(key = 1, ``match`` = Some version) with
| Ok key -> ()
| Error (ErrorKey key) -> ()
```
### Remove a Value with No Version Check
```fsharp
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:
```fsharp
match store.Remove(key = 1, ``match`` = None) with
| Ok key -> ()
| Error (ErrorKey key) -> ()
```