Path: redb/redb.Core/Caching/GlobalPropsCache.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Caching/GlobalPropsCache.cs
----
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
namespace redb.Core.Caching
{
///
/// Domain-isolated cache data for props/objects.
///
internal class PropsCacheDomain
{
public IRedbObjectCache? Cache { get; set; }
}
///
/// Domain-isolated cache for WHOLE RedbObject (not just Props!).
/// Transparent to business code: if disabled (Cache == null), everything goes through DB.
/// Instance is bound to specific domain, static data is shared.
///
///
/// A cached graph is one instance shared by every scope. It carries nothing scope-bound, since a data object owns no
/// connection: its reference stubs hold the scope-free loader of their database and load on the live scope of whoever
/// reads them; only marks the graph shared, so a lazy load inside the reader's transaction
/// keeps nothing. The detached loader swapped in on hand-out, and the dead-scope
/// fallback installed on Set, are gone with the scope-bound loaders they compensated for.
///
public sealed class GlobalPropsCache
{
private static readonly ConcurrentDictionary _domains = new();
private static readonly object _lock = new();
private readonly string _domain;
///
/// Domain identifier for this cache instance.
///
public string Domain => _domain;
///
/// Create cache instance for specific domain.
///
public GlobalPropsCache(string? domain = null)
{
_domain = domain ?? "default";
}
private PropsCacheDomain GetCache() => _domains.GetOrAdd(_domain, _ => new PropsCacheDomain());
///
/// Get underlying cache instance for this domain.
/// If null - cache is disabled, everything goes through DB.
///
public IRedbObjectCache? Instance => GetCache().Cache;
///
/// Initialize cache for this domain (called once at application startup per domain).
///
public void Initialize(IRedbObjectCache cache)
{
CollectibleContextCleanup.EnsureWatching();
lock (_lock)
{
GetCache().Cache = cache;
}
}
///
/// Get WHOLE RedbObject with hash validation.
///
public RedbObject? Get(long objectId, Guid hash) where TProps : class, new()
=> Instance?.Get(objectId, hash);
///
/// Get WHOLE RedbObject WITHOUT hash validation (for monolithic applications).
///
public RedbObject? GetWithoutHashValidation(long objectId) where TProps : class, new()
=> Instance?.GetWithoutHashValidation(objectId);
///
/// Save WHOLE RedbObject to cache. From here on the graph is shared by every scope.
///
public void Set(RedbObject obj) where TProps : class, new()
{
var cache = Instance;
if (cache == null || !obj.hash.HasValue) return;
cache.Set(obj);
Utils.LazyReferenceInstaller.MarkShared(obj);
}
///
/// Save WHOLE RedbObject and every loaded object under it. One walk marks the graph shared and collects the
/// nested entries, where a Set followed by walked the same Props twice.
///
public void SetGraph(RedbObject obj) where TProps : class, new()
{
var cache = Instance;
if (cache == null || !obj.hash.HasValue) return;
cache.Set(obj);
foreach (var nested in Utils.LazyReferenceInstaller.MarkSharedAndCollect(obj))
{
if (ReferenceEquals(nested, obj)) continue;
CacheDelegates.Set(cache, nested);
}
}
///
/// Caches every loaded RedbObject found under by its own id, so a nested object is
/// served directly by id as well; the entries only (the Set of the root marked the graph shared already).
/// Stubs are neither cached nor walked.
///
public void SetNested(object props)
{
var cache = Instance;
if (cache == null || props == null) return;
CacheNestedObjectsInternal(cache, props, new HashSet