![]() |
VOOZH | about |
dotnet add package Hexalith.KeyValueStorages.Files --version 2.1.2
NuGet\Install-Package Hexalith.KeyValueStorages.Files -Version 2.1.2
<PackageReference Include="Hexalith.KeyValueStorages.Files" Version="2.1.2" />
<PackageVersion Include="Hexalith.KeyValueStorages.Files" Version="2.1.2" />Directory.Packages.props
<PackageReference Include="Hexalith.KeyValueStorages.Files" />Project file
paket add Hexalith.KeyValueStorages.Files --version 2.1.2
#r "nuget: Hexalith.KeyValueStorages.Files, 2.1.2"
#:package Hexalith.KeyValueStorages.Files@2.1.2
#addin nuget:?package=Hexalith.KeyValueStorages.Files&version=2.1.2Install as a Cake Addin
#tool nuget:?package=Hexalith.KeyValueStorages.Files&version=2.1.2Install as a Cake Tool
Hexalith.KeyValueStorages.Files provides file-based implementations of the key-value storage interfaces defined in Hexalith.KeyValueStorages.Abstractions. It offers a robust solution for persisting key-value pairs to the file system using JSON serialization, with built-in support for optimistic concurrency control through Etags.
This library serves several key purposes:
Add the NuGet package to your project:
dotnet add package Hexalith.KeyValueStorages.Files
// Create a JSON file store with string keys and custom value type
var store = new JsonFileKeyValueStorage<string, MyData>("data-directory");
// Add a new value
string etag = await store.AddAsync("key1", new MyData { Name = "Test" }, CancellationToken.None);
// Retrieve a value
var result = await store.GetAsync("key1", CancellationToken.None);
MyData value = result.Value;
string currentEtag = result.Etag;
// Update a value with concurrency check
string newEtag = await store.SetAsync("key1", updatedData, currentEtag, CancellationToken.None);
// Check if a key exists
bool exists = await store.ContainsKeyAsync("key1", CancellationToken.None);
// Remove a value
bool removed = await store.RemoveAsync("key1", currentEtag, CancellationToken.None);
FileKeyValueStorage<TKey, TValue, TEtag, TKeySerializer, TValueSerializer>: Abstract base class for file-based storage
JsonFileKeyValueStorage<TKey, TValue, TEtag>: Implementation using JSON serialization with custom Etag type
JsonFileKeyValueStorage<TKey, TValue>: Simplified implementation using string EtagsJsonFileValue<TValue, TEtag>: Record type for storing value and Etag togetherJsonFileSerializer<TValue, TEtag>: Serializer for JSON file valuesKeyToStringSerializer<TKey>: Converts keys to file-safe string representations// Create a store for string keys and values
var stringStore = new JsonFileKeyValueStorage<string, string>("string-store");
// Add a string value
string etag = await stringStore.AddAsync("greeting", "Hello, World!", CancellationToken.None);
// Define a custom data type
public record UserProfile
{
public string Username { get; init; } = string.Empty;
public string Email { get; init; } = string.Empty;
public DateTimeOffset LastLogin { get; init; }
public int LoginCount { get; init; }
}
// Create a store for user profiles
var userStore = new JsonFileKeyValueStorage<string, UserProfile>("user-profiles");
// Add a user profile
var profile = new UserProfile
{
Username = "johndoe",
Email = "john@example.com",
LastLogin = DateTimeOffset.UtcNow,
LoginCount = 1
};
string etag = await userStore.AddAsync("user123", profile, CancellationToken.None);
The JsonFileKeyValueStorage<TKey, TValue> class uses string Etags by default, generated using UniqueIdHelper.GenerateUniqueStringId(). You can create a custom implementation with different Etag types:
public class CustomEtagStorage<TKey, TValue> :
JsonFileKeyValueStorage<TKey, TValue, Guid>
where TKey : notnull, IEquatable<TKey>
where TValue : notnull
{
public CustomEtagStorage(string rootPath) : base(rootPath) { }
protected override Guid GenerateEtag() => Guid.NewGuid();
}
You can customize the JSON serialization options:
public class CustomJsonStorage<TKey, TValue> :
FileKeyValueStorage<TKey, TValue, string, KeyToStringSerializer<TKey>, CustomJsonSerializer<TValue, string>>
where TKey : notnull, IEquatable<TKey>
where TValue : notnull
{
public CustomJsonStorage(string rootPath) : base(rootPath) { }
protected override string GenerateEtag() => UniqueIdHelper.GenerateUniqueStringId();
}
public class CustomJsonSerializer<TValue, TEtag> : JsonFileSerializer<TValue, TEtag>
where TValue : notnull
where TEtag : notnull
{
public CustomJsonSerializer() : base(new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
})
{ }
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.1.2 | 236 | 4/12/2025 |
| 2.1.1 | 225 | 4/11/2025 |
| 2.1.0 | 223 | 4/11/2025 |
| 2.0.2 | 251 | 4/10/2025 |
| 2.0.1 | 224 | 4/10/2025 |
| 2.0.0 | 235 | 4/10/2025 |
| 1.6.0 | 246 | 4/9/2025 |
| 1.5.0 | 240 | 4/8/2025 |
| 1.4.2 | 263 | 4/8/2025 |
| 1.4.1 | 240 | 4/7/2025 |
| 1.4.0 | 243 | 4/7/2025 |
| 1.3.2 | 248 | 4/6/2025 |
| 1.3.1 | 240 | 4/6/2025 |
| 1.3.0 | 246 | 4/6/2025 |
| 1.2.0 | 236 | 4/6/2025 |
| 1.0.0 | 242 | 4/6/2025 |