Path: redb/redb.Core/Caching/IRedbObjectCache.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/IRedbObjectCache.cs
----
using System;
using System.Collections.Generic;
using redb.Core.Models.Entities;
namespace redb.Core.Caching
{
///
/// Interface for caching WHOLE RedbObject objects (not just Props!)
/// Cache key = objectId, Hash is used for validity validation
/// Cache entire object with base fields + Props for:
/// - 0 DB queries on Cache HIT (without SELECT base fields)
/// - Reuse of nested RedbObject through references (memory savings)
///
public interface IRedbObjectCache
{
///
/// Get WHOLE RedbObject from cache by objectId with hash validation
///
/// Object ID
/// Current object hash for validation
/// RedbObject if found and hash matches, otherwise null
RedbObject? Get(long objectId, Guid currentHash) where TProps : class, new();
///
/// Get WHOLE RedbObject from cache by objectId WITHOUT hash validation
/// Used when SkipHashValidationOnCacheCheck = true (monolithic applications)
///
/// Object ID
/// RedbObject if found in cache, otherwise null (without actuality check)
RedbObject? GetWithoutHashValidation(long objectId) where TProps : class, new();
///
/// Save WHOLE RedbObject to cache
///
/// RedbObject for caching (with base fields + Props)
void Set(RedbObject obj) where TProps : class, new();
///
/// BULK: determine which objects need to be loaded from DB (set difference)
/// Returns cached WHOLE objects (not just Props)
///
/// List of (objectId, hash) from DB
/// OUT: RedbObject instances taken from cache
/// HashSet of objectId that need to be loaded from DB
HashSet FilterNeedToLoad(
List<(long objectId, Guid hash)> objects,
out Dictionary> fromCache) where TProps : class, new();
///
/// Remove object from cache
///
/// Object ID
void Remove(long objectId);
///
/// Clear entire cache
///
void Clear();
///
/// Get cache statistics
///
PropsCacheStatistics GetStats();
///
/// Removes every cached object whose type answers true for. redb calls it when a
/// collectible AssemblyLoadContext unloads (a module of Tsak, for instance), with a predicate that
/// recognizes the types of that context: an object of such a type kept here would keep the context alive.
/// The default implementation clears the whole cache, which is correct at the price of a cold cache.
///
void RemoveObjectsOfTypes(Func unloading) => Clear();
// === LEGACY METHODS (COMMENTED OUT - use Get/Set for full RedbObject) ===
// ///
// /// [LEGACY] Get only Props (for backward compatibility)
// ///
// [Obsolete("Use Get(objectId, hash) to get full RedbObject instead of only Props")]
// TProps? GetProps(long objectId, Guid currentHash) where TProps : class, new();
// ///
// /// [LEGACY] Save only Props (for backward compatibility)
// ///
// [Obsolete("Use Set(redbObject) to save full RedbObject instead of only Props")]
// void SetProps(long objectId, Guid hash, TProps props) where TProps : class, new();
}
}