JSON backing store, I/O fixes, Networking Security
Introduced Newtonsoft.Json-backed persistence. Improved backing-store reliability and serialization across the caching stack. Improved networking security posture. Key changes: - Add new GameCult.Caching.NewtonsoftJson project with SingleFile/MultiFile backing stores and polymorphic known-type envelopes. - Add MessagePack helper serialization (DatabaseEntrySerialization) and update MessagePack backing stores to use the shared options/resolver; add DatabaseLink formatter and generator/analyzer docs. - Update CultLib.sln to include the new projects and new caching tests project. - Improve CultCache persistence behavior: track pending store operations, wait for operations on PullAll, and centralize async push handling with error logging. - Harden file I/O in multi-file/single-file stores: atomic temp-file writes, safe Replace/Move semantics, rename-safe deletes, and improved file-watcher subscription and debounce disposal. - Add README files and documentation for several subprojects and packaging projects. These changes add JSON persistence support, improve serialization consistency, and make on-disk operations more robust and observable.
This commit is contained in:
parent
d88eecb4ba
commit
0bfc85a4f2
50 changed files with 3957 additions and 325 deletions
|
|
@ -12,12 +12,20 @@ namespace GameCult.Caching.MessagePack
|
|||
/// </summary>
|
||||
public sealed class DatabaseEntryResolver : IFormatterResolver
|
||||
{
|
||||
/// <summary>
|
||||
/// Shared singleton instance of the resolver.
|
||||
/// </summary>
|
||||
public static readonly DatabaseEntryResolver Instance = new DatabaseEntryResolver();
|
||||
|
||||
private readonly ConcurrentDictionary<Type, object> _formatterCache = new();
|
||||
|
||||
private DatabaseEntryResolver() { }
|
||||
|
||||
/// <summary>
|
||||
/// Resolves a formatter for a cache entry type or a <see cref="DatabaseLink{T}"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The requested serialized type.</typeparam>
|
||||
/// <returns>The formatter if one is available; otherwise, <c>null</c>.</returns>
|
||||
public IMessagePackFormatter<T>? GetFormatter<T>()
|
||||
{
|
||||
// 1. Check generated resolver mapping
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
using MessagePack;
|
||||
using MessagePack.Resolvers;
|
||||
|
||||
namespace GameCult.Caching.MessagePack;
|
||||
|
||||
internal static class DatabaseEntrySerialization
|
||||
{
|
||||
public static readonly MessagePackSerializerOptions Options =
|
||||
MessagePackSerializerOptions.Standard
|
||||
.WithResolver(CompositeResolver.Create(
|
||||
DatabaseEntryResolver.Instance,
|
||||
StandardResolver.Instance))
|
||||
.WithSecurity(MessagePackSecurity.UntrustedData);
|
||||
|
||||
public static byte[] Serialize<T>(T value)
|
||||
{
|
||||
return MessagePackSerializer.Serialize(value, Options);
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(byte[] payload)
|
||||
{
|
||||
return MessagePackSerializer.Deserialize<T>(payload, Options);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,8 +4,13 @@ using MessagePack.Formatters;
|
|||
|
||||
namespace GameCult.Caching.MessagePack
|
||||
{
|
||||
public sealed class DatabaseLinkFormatter<T> : IMessagePackFormatter<DatabaseLink<T>> where T : DatabaseEntry
|
||||
/// <summary>
|
||||
/// Serializes <see cref="DatabaseLink{T}"/> values as their linked identifier only.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The target entry type.</typeparam>
|
||||
public sealed class DatabaseLinkFormatter<T> : IMessagePackFormatter<DatabaseLink<T>?> where T : DatabaseEntry
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Serialize(ref MessagePackWriter writer, DatabaseLink<T>? value, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (value == null) { writer.WriteNil(); return; }
|
||||
|
|
@ -14,6 +19,7 @@ namespace GameCult.Caching.MessagePack
|
|||
fmt.Serialize(ref writer, value.LinkID, options);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public DatabaseLink<T>? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.TryReadNil()) return null;
|
||||
|
|
|
|||
|
|
@ -2,34 +2,53 @@ using MessagePack;
|
|||
|
||||
namespace GameCult.Caching.MessagePack;
|
||||
|
||||
/// <summary>
|
||||
/// Persists all cache entries to a single MessagePack file.
|
||||
/// </summary>
|
||||
public class SingleFileMessagePackBackingStore : SingleFileBackingStore
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a single-file MessagePack backing store.
|
||||
/// </summary>
|
||||
/// <param name="filePath">The path to the backing file.</param>
|
||||
public SingleFileMessagePackBackingStore(string filePath) : base(filePath) { }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override byte[] Serialize(DatabaseEntry[] entries)
|
||||
{
|
||||
return MessagePackSerializer.Serialize(entries);
|
||||
return DatabaseEntrySerialization.Serialize(entries);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override DatabaseEntry[] Deserialize(byte[] data)
|
||||
{
|
||||
return MessagePackSerializer.Deserialize<DatabaseEntry[]>(data);
|
||||
return DatabaseEntrySerialization.Deserialize<DatabaseEntry[]>(data);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Persists each cache entry to its own MessagePack file.
|
||||
/// </summary>
|
||||
public class MultiFileMessagePackBackingStore : MultiFileBackingStore
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a multi-file MessagePack backing store rooted at the supplied directory.
|
||||
/// </summary>
|
||||
/// <param name="path">The root directory for stored entry files.</param>
|
||||
public MultiFileMessagePackBackingStore(string path) : base(path) { }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override byte[] Serialize(DatabaseEntry entry)
|
||||
{
|
||||
return MessagePackSerializer.Serialize(entry);
|
||||
return DatabaseEntrySerialization.Serialize(entry);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override DatabaseEntry Deserialize(byte[] data)
|
||||
{
|
||||
return MessagePackSerializer.Deserialize<DatabaseEntry>(data);
|
||||
return DatabaseEntrySerialization.Deserialize<DatabaseEntry>(data);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Extension => "msgpack";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
115
src/GameCult.Caching.MessagePack/README.md
Normal file
115
src/GameCult.Caching.MessagePack/README.md
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
# GameCult.Caching.MessagePack
|
||||
|
||||
`GameCult.Caching.MessagePack` provides MessagePack-backed persistence for `GameCult.Caching`.
|
||||
|
||||
## Scope
|
||||
|
||||
The package focuses on:
|
||||
|
||||
- MessagePack storage for `DatabaseEntry` models
|
||||
- single-file and multi-file store implementations
|
||||
- support for generated formatters for concrete cache-entry types
|
||||
- formatter support for `DatabaseLink<T>`
|
||||
|
||||
## Included Types
|
||||
|
||||
- `SingleFileMessagePackBackingStore`
|
||||
- `MultiFileMessagePackBackingStore`
|
||||
- `DatabaseEntryResolver`
|
||||
- `DatabaseLinkFormatter<T>`
|
||||
|
||||
## Why The Generator Matters
|
||||
|
||||
`DatabaseEntry` is abstract. Concrete subclasses need MessagePack formatters to be serializable without hand-writing formatters for each model.
|
||||
|
||||
That is the role of:
|
||||
|
||||
- `GameCult.Caching.MessagePack.Generator`
|
||||
- `GameCult.Caching.MessagePack.Analyzers`
|
||||
|
||||
In practice:
|
||||
|
||||
- this package gives you the stores and resolver support
|
||||
- the generator package produces the actual concrete formatters
|
||||
|
||||
## Typical Usage
|
||||
|
||||
```csharp
|
||||
using GameCult.Caching;
|
||||
using GameCult.Caching.MessagePack;
|
||||
|
||||
var cache = new CultCache();
|
||||
var store = new MultiFileMessagePackBackingStore("Data");
|
||||
|
||||
cache.AddBackingStore(store);
|
||||
await cache.PullAllBackingStoresAsync();
|
||||
```
|
||||
|
||||
## Single-File vs Multi-File
|
||||
|
||||
### `SingleFileMessagePackBackingStore`
|
||||
|
||||
Use this when you want one snapshot file containing all entries.
|
||||
|
||||
Use cases:
|
||||
|
||||
- compact settings data
|
||||
- simple local persistence
|
||||
- write-all-at-once workflows
|
||||
|
||||
```csharp
|
||||
var store = new SingleFileMessagePackBackingStore("cache.msgpack");
|
||||
cache.AddBackingStore(store);
|
||||
```
|
||||
|
||||
### `MultiFileMessagePackBackingStore`
|
||||
|
||||
Use this when you want one file per entry.
|
||||
|
||||
Use cases:
|
||||
|
||||
- large datasets
|
||||
- debugging persisted entries individually
|
||||
- change observation through the multi-file base store
|
||||
|
||||
```csharp
|
||||
var store = new MultiFileMessagePackBackingStore("Data");
|
||||
cache.AddBackingStore(store, typeof(PlayerData));
|
||||
```
|
||||
|
||||
## `DatabaseLink<T>`
|
||||
|
||||
`DatabaseLink<T>` is a lightweight reference type used by the cache layer. The MessagePack formatter serializes only the linked `Guid`, not the fully loaded target object.
|
||||
|
||||
That means:
|
||||
|
||||
- links stay compact on disk
|
||||
- linked objects are resolved through the active `CultCache`
|
||||
- deserialized links depend on `DatabaseLinkBase.Cache` pointing at the active cache
|
||||
|
||||
## Example Model
|
||||
|
||||
```csharp
|
||||
using GameCult.Caching;
|
||||
using MessagePack;
|
||||
|
||||
[MessagePackObject]
|
||||
public class ItemData : DatabaseEntry, INamedEntry
|
||||
{
|
||||
[Key(1)] public string Name = string.Empty;
|
||||
[Key(2)] public int Value;
|
||||
|
||||
[IgnoreMember]
|
||||
public string EntryName
|
||||
{
|
||||
get => Name;
|
||||
set => Name = value;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Practical Notes
|
||||
|
||||
- For polymorphic `DatabaseEntry` serialization, use this package with the generator/analyzer package.
|
||||
- The backing stores build on the semantics of `CultCache`, including primary-store routing and type-specific store routing.
|
||||
- `MultiFileMessagePackBackingStore` inherits the rename-safe and atomic-write behavior of the hardened `MultiFileBackingStore` base class.
|
||||
Loading…
Reference in a new issue