Index CultCache records by document type
This commit is contained in:
parent
ba680020b9
commit
be846415d4
2 changed files with 58 additions and 3 deletions
|
|
@ -1271,6 +1271,7 @@ namespace GameCult.Caching
|
|||
private readonly CultDocumentRegistry _registry;
|
||||
private readonly List<CacheBackingStore> _backingStores = new();
|
||||
private readonly ConcurrentDictionary<string, CultStoredDocument> _entries = new(StringComparer.Ordinal);
|
||||
private readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, CultStoredDocument>> _typeMaps = new();
|
||||
private readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, string>> _nameMaps = new();
|
||||
private readonly ConcurrentDictionary<(Type Type, string Alias), ConcurrentDictionary<string, string>> _indexMaps = new();
|
||||
private readonly ConcurrentDictionary<Type, string> _globalKeys = new();
|
||||
|
|
@ -1597,11 +1598,26 @@ namespace GameCult.Caching
|
|||
/// Gets all cached documents assignable to the requested type.
|
||||
/// </summary>
|
||||
public IEnumerable<T> GetAll<T>() where T : class
|
||||
{
|
||||
return GetStoredDocuments<T>().Select(entry => (T)entry.Document);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets typed cached records with their stable CultCache identities without scanning unrelated documents.
|
||||
/// </summary>
|
||||
public IEnumerable<CultStoredDocument> GetStoredDocuments<T>() 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<CultStoredDocument>();
|
||||
}
|
||||
|
||||
return _typeMaps
|
||||
.Where(pair => type.IsAssignableFrom(pair.Key))
|
||||
.SelectMany(pair => pair.Value.Values);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -1867,6 +1883,11 @@ namespace GameCult.Caching
|
|||
|
||||
private void AddIndexes(CultStoredDocument stored)
|
||||
{
|
||||
var typeMap = _typeMaps.GetOrAdd(
|
||||
stored.Descriptor.DocumentType,
|
||||
_ => new ConcurrentDictionary<string, CultStoredDocument>(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 _);
|
||||
|
|
|
|||
|
|
@ -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<NamedTestEntry>().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<NamedTestEntry>().Single();
|
||||
Assert.That(((NamedTestEntry)replaced.Document).Value, Is.EqualTo("second"));
|
||||
|
||||
Assert.That(cache.Remove(key), Is.True);
|
||||
Assert.That(cache.GetStoredDocuments<NamedTestEntry>(), Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CultCache_FlushOnDispose_Persists_When_Enabled()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue