From be846415d487e0c10b71235f4e4c60a41940bb53 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 18 Aug 2026 01:07:25 +0200 Subject: [PATCH] Index CultCache records by document type --- src/GameCult.Caching/CultCache.cs | 30 ++++++++++++++++-- .../BackingStoreTests.cs | 31 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/GameCult.Caching/CultCache.cs b/src/GameCult.Caching/CultCache.cs index d3da3096..21b437af 100644 --- a/src/GameCult.Caching/CultCache.cs +++ b/src/GameCult.Caching/CultCache.cs @@ -1271,6 +1271,7 @@ namespace GameCult.Caching private readonly CultDocumentRegistry _registry; private readonly List _backingStores = new(); private readonly ConcurrentDictionary _entries = new(StringComparer.Ordinal); + private readonly ConcurrentDictionary> _typeMaps = new(); private readonly ConcurrentDictionary> _nameMaps = new(); private readonly ConcurrentDictionary<(Type Type, string Alias), ConcurrentDictionary> _indexMaps = new(); private readonly ConcurrentDictionary _globalKeys = new(); @@ -1597,11 +1598,26 @@ namespace GameCult.Caching /// Gets all cached documents assignable to the requested type. /// public IEnumerable GetAll() where T : class + { + return GetStoredDocuments().Select(entry => (T)entry.Document); + } + + /// + /// Gets typed cached records with their stable CultCache identities without scanning unrelated documents. + /// + public IEnumerable GetStoredDocuments() where T : class { var type = typeof(T); - return _entries.Values - .Where(entry => type.IsAssignableFrom(entry.Descriptor.DocumentType)) - .Select(entry => (T)entry.Document); + if (type.IsSealed) + { + return _typeMaps.TryGetValue(type, out var exact) + ? exact.Values + : Enumerable.Empty(); + } + + return _typeMaps + .Where(pair => type.IsAssignableFrom(pair.Key)) + .SelectMany(pair => pair.Value.Values); } /// @@ -1867,6 +1883,11 @@ namespace GameCult.Caching private void AddIndexes(CultStoredDocument stored) { + var typeMap = _typeMaps.GetOrAdd( + stored.Descriptor.DocumentType, + _ => new ConcurrentDictionary(StringComparer.Ordinal)); + typeMap[stored.Key.Value] = stored; + if (stored.Descriptor.IsGlobal) { _globalKeys[stored.Descriptor.DocumentType] = stored.Key.Value; @@ -1897,6 +1918,9 @@ namespace GameCult.Caching private void RemoveIndexes(CultStoredDocument stored) { + if (_typeMaps.TryGetValue(stored.Descriptor.DocumentType, out var typeMap)) + typeMap.TryRemove(stored.Key.Value, out _); + if (stored.Descriptor.IsGlobal) { _globalKeys.TryRemove(stored.Descriptor.DocumentType, out _); diff --git a/tests/GameCult.Caching.Tests/BackingStoreTests.cs b/tests/GameCult.Caching.Tests/BackingStoreTests.cs index 15d6d718..3910946a 100644 --- a/tests/GameCult.Caching.Tests/BackingStoreTests.cs +++ b/tests/GameCult.Caching.Tests/BackingStoreTests.cs @@ -806,6 +806,37 @@ namespace GameCult.Caching.Tests Assert.That(cache.Remove(key), Is.False); } + [Test] + public async Task CultCache_TypedStoredDocumentIndex_Tracks_Upsert_Replacement_And_Removal() + { + var cache = new CultCache(); + var key = new CultRecordKey("typed-index:named"); + await cache.UpsertAsync(typeof(NamedTestEntry), new NamedTestEntry + { + Name = "indexed", + Value = "first" + }, key); + await cache.UpsertAsync(typeof(AlternateNamedTestEntry), new AlternateNamedTestEntry + { + Name = "unrelated" + }, new CultRecordKey("typed-index:unrelated")); + + var initial = cache.GetStoredDocuments().Single(); + Assert.That(initial.Key, Is.EqualTo(key)); + Assert.That(((NamedTestEntry)initial.Document).Value, Is.EqualTo("first")); + + await cache.UpsertAsync(typeof(NamedTestEntry), new NamedTestEntry + { + Name = "indexed", + Value = "second" + }, key); + var replaced = cache.GetStoredDocuments().Single(); + Assert.That(((NamedTestEntry)replaced.Document).Value, Is.EqualTo("second")); + + Assert.That(cache.Remove(key), Is.True); + Assert.That(cache.GetStoredDocuments(), Is.Empty); + } + [Test] public async Task CultCache_FlushOnDispose_Persists_When_Enabled() {