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(ReferenceEqualityComparer.Instance)); } /// /// Internal method for recursive caching with tracking of visited objects /// private static void CacheNestedObjectsInternal(IRedbObjectCache cache, object obj, HashSet visited) { if (obj == null) return; var objType = obj.GetType(); // CRITICAL: Check TYPE IMMEDIATELY before visited.Add()! // This prevents infinite boxing for value types (DateTime, int, etc.) if (objType.IsPrimitive || objType.IsValueType || objType.Namespace?.StartsWith("System") == true) { return; } // A list item is a leaf: reading its lazy Object property IS a database load, so the // reflective property walk below must never descend into one (stand, 2026-09-09). if (redb.Core.Utils.LazyReferenceInstaller.IsWalkLeaf(obj)) { return; } // Only AFTER type check add to visited (protection against circular references) if (!visited.Add(obj)) { return; } // If this is a RedbObject (or a TreeRedbObject) → cache it var genericObjType = Utils.RedbObjectTypes.GenericOf(objType); if (genericObjType != null) { var redbObj = obj as IRedbObject; // V4 (L.3): a reference stub is neither cached nor walked - reading its Props here // would BE the lazy load: synchronous, and for the whole reachable graph (review). if (redbObj != null && redbObj.Hash.HasValue && obj is RedbObject { IsPropsLoaded: true }) { // The entry only: the Set of the root marked the whole graph shared already, and a nested Set // through GlobalPropsCache walked the subtree again for every nested object (review after 4.0.0). var propsType = genericObjType.GetGenericArguments()[0]; var setMethod = typeof(IRedbObjectCache).GetMethod("Set")?.MakeGenericMethod(propsType); setMethod?.Invoke(cache, new[] { obj }); // Raw Props, never the getter var propsValue = objType.GetMethod("GetPropsDirectly")?.Invoke(obj, null); if (propsValue != null) { CacheNestedObjectsInternal(cache, propsValue, visited); } } return; } // Process arrays if (obj is Array array) { foreach (var item in array) { if (item != null) { CacheNestedObjectsInternal(cache, item, visited); } } return; } // Process collections (IEnumerable, except string) if (obj is System.Collections.IEnumerable enumerable && obj is not string) { foreach (var item in enumerable) { if (item != null) { CacheNestedObjectsInternal(cache, item, visited); } } return; } // Process business class properties (Address, Contact, Details, etc.) var properties = objType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var prop in properties) { // The save's property set: not an indexer, not [RedbIgnore]. if (!Utils.LazyReferenceInstaller.IsWalkedProperty(prop)) continue; var propValue = prop.GetValue(obj); if (propValue != null) { CacheNestedObjectsInternal(cache, propValue, visited); } } } /// /// BULK: determine which objects need to be loaded from DB (set difference). /// Returns cached WHOLE RedbObject instances. /// public HashSet FilterNeedToLoad( List<(long objectId, Guid hash)> objects, out Dictionary> fromCache) where TProps : class, new() { if (Instance != null) return Instance.FilterNeedToLoad(objects, out fromCache); // Cache is disabled - load everything from DB fromCache = new Dictionary>(); return objects.Select(o => o.objectId).ToHashSet(); } /// /// Remove from cache. /// /// /// Drops, in every domain, the cached objects whose type answers true for: the /// types of a collectible AssemblyLoadContext that unloads (). /// internal static void RemoveObjectsOfTypes(Func unloading) { foreach (var domain in _domains.Values) domain.Cache?.RemoveObjectsOfTypes(unloading); } public void Remove(long objectId) { Instance?.Remove(objectId); } /// /// Clear cache for this domain. /// public void Clear() { Instance?.Clear(); } /// /// Get cache statistics for this domain. /// public PropsCacheStatistics GetStats() { return Instance?.GetStats() ?? new PropsCacheStatistics(); } } }