Remove alternate CultCache backing stores
This commit is contained in:
parent
ef1f4c16ee
commit
08208b9722
11 changed files with 32 additions and 467 deletions
|
|
@ -904,11 +904,6 @@ namespace GameCult.Caching
|
|||
}
|
||||
}
|
||||
|
||||
public interface IRealtimeBackingStore
|
||||
{
|
||||
void ObserveChanges();
|
||||
}
|
||||
|
||||
public abstract class CacheBackingStore : IDisposable
|
||||
{
|
||||
private CultDocumentRegistry? _registry;
|
||||
|
|
@ -972,135 +967,6 @@ namespace GameCult.Caching
|
|||
}
|
||||
}
|
||||
|
||||
public abstract class MultiFileBackingStore : CacheBackingStore, IRealtimeBackingStore
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, string> _pathsByKey = new(StringComparer.Ordinal);
|
||||
|
||||
protected MultiFileBackingStore(string rootPath)
|
||||
{
|
||||
RootDirectory = new DirectoryInfo(rootPath);
|
||||
}
|
||||
|
||||
protected DirectoryInfo RootDirectory { get; }
|
||||
protected abstract byte[] SerializeRecord(CultPersistedRecord record);
|
||||
protected abstract CultPersistedRecord DeserializeRecord(byte[] data);
|
||||
protected abstract byte[] SerializeCatalog(CultSchemaCatalogEntry[] catalog);
|
||||
protected abstract CultSchemaCatalogEntry[] DeserializeCatalog(byte[] data);
|
||||
protected abstract byte[] SerializePayload(object document);
|
||||
protected abstract object DeserializePayload(Type documentType, byte[] payload);
|
||||
public abstract string Extension { get; }
|
||||
protected virtual string CatalogFileName => $"__cultcache.catalog.{Extension}";
|
||||
|
||||
public override void PullAll()
|
||||
{
|
||||
if (!RootDirectory.Exists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var catalog = LoadCatalog();
|
||||
foreach (var file in RootDirectory.EnumerateFiles($"*.{Extension}", SearchOption.AllDirectories)
|
||||
.Where(file => !string.Equals(file.Name, CatalogFileName, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
var stored = ToStoredDocument(
|
||||
DeserializeRecord(File.ReadAllBytes(file.FullName)),
|
||||
catalog,
|
||||
DeserializePayload);
|
||||
Entries[stored.Key.Value] = stored;
|
||||
_pathsByKey[stored.Key.Value] = file.FullName;
|
||||
EntryAdded.OnNext(stored);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Push(CultStoredDocument entry)
|
||||
{
|
||||
Entries[entry.Key.Value] = entry;
|
||||
var filePath = GetFilePath(entry);
|
||||
if (_pathsByKey.TryGetValue(entry.Key.Value, out var existingPath) &&
|
||||
!string.Equals(existingPath, filePath, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
TryDeleteFile(existingPath);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
|
||||
File.WriteAllBytes(filePath, SerializeRecord(ToPersistedRecord(entry, SerializePayload)));
|
||||
_pathsByKey[entry.Key.Value] = filePath;
|
||||
PersistCatalog();
|
||||
}
|
||||
|
||||
public override void Delete(CultStoredDocument entry)
|
||||
{
|
||||
Entries.TryRemove(entry.Key.Value, out _);
|
||||
if (_pathsByKey.TryRemove(entry.Key.Value, out var existingPath))
|
||||
{
|
||||
TryDeleteFile(existingPath);
|
||||
}
|
||||
|
||||
PersistCatalog();
|
||||
}
|
||||
|
||||
public override void PushAll(bool soft = false)
|
||||
{
|
||||
foreach (var entry in Entries.Values.ToArray())
|
||||
{
|
||||
Push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ObserveChanges()
|
||||
{
|
||||
}
|
||||
|
||||
private CultSchemaCatalogEntry[] LoadCatalog()
|
||||
{
|
||||
var catalogFile = Path.Combine(RootDirectory.FullName, CatalogFileName);
|
||||
if (!File.Exists(catalogFile))
|
||||
{
|
||||
return Array.Empty<CultSchemaCatalogEntry>();
|
||||
}
|
||||
|
||||
return DeserializeCatalog(File.ReadAllBytes(catalogFile));
|
||||
}
|
||||
|
||||
private void PersistCatalog()
|
||||
{
|
||||
Directory.CreateDirectory(RootDirectory.FullName);
|
||||
var catalogFile = Path.Combine(RootDirectory.FullName, CatalogFileName);
|
||||
var catalog = Entries.Values
|
||||
.Select(entry => entry.Descriptor.ToCatalogEntry())
|
||||
.GroupBy(entry => entry.SchemaId, StringComparer.Ordinal)
|
||||
.Select(group => group.First())
|
||||
.OrderBy(entry => entry.SchemaName, StringComparer.Ordinal)
|
||||
.ToArray();
|
||||
File.WriteAllBytes(catalogFile, SerializeCatalog(catalog));
|
||||
}
|
||||
|
||||
private string GetFilePath(CultStoredDocument entry)
|
||||
{
|
||||
var directory = Path.Combine(RootDirectory.FullName, entry.Descriptor.SchemaName);
|
||||
var fileName = SanitizeFileName(entry.Descriptor.GetPreferredFileStem(entry.Document));
|
||||
return Path.Combine(directory, $"{fileName}.{Extension}");
|
||||
}
|
||||
|
||||
private static string SanitizeFileName(string value)
|
||||
{
|
||||
foreach (var invalid in Path.GetInvalidFileNameChars())
|
||||
{
|
||||
value = value.Replace(invalid, '_');
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private static void TryDeleteFile(string path)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class SingleFileBackingStore : CacheBackingStore
|
||||
{
|
||||
protected SingleFileBackingStore(string filePath)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
# GameCult.Caching
|
||||
|
||||
`GameCult.Caching` is an attribute-driven typed cache with pluggable stores.
|
||||
Domain models stay clean. The cache owns record keys, schema ids, timestamps,
|
||||
and persistence metadata instead of smearing that sludge across every class.
|
||||
`GameCult.Caching` is an attribute-driven typed cache with a canonical
|
||||
single-file MessagePack store. Domain models stay clean. The cache owns record
|
||||
keys, schema ids, timestamps, and persistence metadata instead of smearing
|
||||
that sludge across every class.
|
||||
|
||||
## Authoring Model
|
||||
|
||||
|
|
@ -13,7 +14,6 @@ using GameCult.Caching;
|
|||
using MessagePack;
|
||||
|
||||
[CultDocument("gamecult.item_data", "gamecult.item_data.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed class ItemData
|
||||
{
|
||||
[Key(0)]
|
||||
|
|
@ -78,7 +78,6 @@ but it will not quietly build an entire haunted object graph behind your back.
|
|||
|
||||
```csharp
|
||||
[CultDocument("gamecult.quest_data", "gamecult.quest_data.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed class QuestData
|
||||
{
|
||||
[Key(0)] public string Title = string.Empty;
|
||||
|
|
@ -103,24 +102,24 @@ Each persisted record carries:
|
|||
|
||||
The domain payload stays free of storage metadata.
|
||||
|
||||
## Backing Stores
|
||||
## Backing Store
|
||||
|
||||
Base abstractions:
|
||||
|
||||
- `CacheBackingStore`
|
||||
- `SingleFileBackingStore`
|
||||
- `MultiFileBackingStore`
|
||||
|
||||
Included implementations in sibling packages:
|
||||
CultCache now has one sanctioned persistence format:
|
||||
|
||||
- `SingleFileMessagePackBackingStore`
|
||||
- `MultiFileMessagePackBackingStore`
|
||||
- `SingleFileNewtonsoftJsonBackingStore`
|
||||
- `MultiFileNewtonsoftJsonBackingStore`
|
||||
|
||||
`MultiFileBackingStore` derives filenames from the `CultName` member when one
|
||||
exists, so renames replace stale files instead of leaving little corpses
|
||||
behind.
|
||||
It persists the whole store as:
|
||||
|
||||
1. store format version
|
||||
2. embedded schema catalog
|
||||
3. persisted records
|
||||
|
||||
Each persisted record carries:
|
||||
|
||||
- `key`
|
||||
- `schemaId`
|
||||
- `storedAt`
|
||||
- `payload`
|
||||
|
||||
## Typical Startup
|
||||
|
||||
|
|
@ -129,7 +128,7 @@ using GameCult.Caching;
|
|||
using GameCult.Caching.MessagePack;
|
||||
|
||||
var cache = new CultCache();
|
||||
var store = new MultiFileMessagePackBackingStore("Data");
|
||||
var store = new SingleFileMessagePackBackingStore("Data.msgpack");
|
||||
|
||||
cache.AddBackingStore(store);
|
||||
await cache.PullAllBackingStoresAsync();
|
||||
|
|
|
|||
Loading…
Reference in a new issue