Path: redb/redb.Core/Caching/CacheDelegates.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/CacheDelegates.cs
----
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using redb.Core.Models.Contracts;
using redb.Core.Utils;
namespace redb.Core.Caching
{
///
/// Typed cache calls for an object type met at run time. A batch load and a graph write used to run
/// GetMethod + MakeGenericMethod + Invoke on every object; here the call is built once per type
/// and kept in a : a type unloaded with its collectible load context
/// takes its delegate with it, so Tsak hot reload releases the assembly.
///
internal static class CacheDelegates
{
private static readonly ConditionalWeakTable> _set = new();
private static readonly ConditionalWeakTable> _setGraph = new();
private static readonly ConditionalWeakTable> _get = new();
/// Write the object by its own id (the Set of the cache interface). A non-generic RedbObject carries
/// no Props and was never an entry of its own.
public static void Set(IRedbObjectCache cache, IRedbObject obj)
{
if (RedbObjectTypes.GenericOf(obj.GetType()) == null) return;
_set.GetValue(obj.GetType(), CreateSet)(cache, obj);
}
/// Write the object and every loaded object under it in one walk.
public static void SetGraph(GlobalPropsCache cache, IRedbObject obj)
{
if (RedbObjectTypes.GenericOf(obj.GetType()) == null) return;
_setGraph.GetValue(obj.GetType(), CreateSetGraph)(cache, obj);
}
/// Read the object by id and hash; null when the cache has no current entry.
public static IRedbObject? Get(GlobalPropsCache cache, Type propsType, long objectId, Guid hash)
=> _get.GetValue(propsType, CreateGet)(cache, objectId, hash);
private static Action CreateSet(Type objectType)
{
var redbType = RedbObjectTypes.GenericOf(objectType)!;
var set = typeof(IRedbObjectCache).GetMethod(nameof(IRedbObjectCache.Set))!
.MakeGenericMethod(redbType.GetGenericArguments()[0]);
var cache = Expression.Parameter(typeof(IRedbObjectCache), "cache");
var obj = Expression.Parameter(typeof(IRedbObject), "obj");
var call = Expression.Call(cache, set, Expression.Convert(obj, redbType));
return Expression.Lambda>(call, cache, obj).Compile();
}
private static Action CreateSetGraph(Type objectType)
{
var redbType = RedbObjectTypes.GenericOf(objectType)!;
var setGraph = typeof(GlobalPropsCache).GetMethod(nameof(GlobalPropsCache.SetGraph))!
.MakeGenericMethod(redbType.GetGenericArguments()[0]);
var cache = Expression.Parameter(typeof(GlobalPropsCache), "cache");
var obj = Expression.Parameter(typeof(IRedbObject), "obj");
var call = Expression.Call(cache, setGraph, Expression.Convert(obj, redbType));
return Expression.Lambda>(call, cache, obj).Compile();
}
private static Func CreateGet(Type propsType)
{
var get = typeof(GlobalPropsCache).GetMethod(nameof(GlobalPropsCache.Get))!
.MakeGenericMethod(propsType);
var cache = Expression.Parameter(typeof(GlobalPropsCache), "cache");
var objectId = Expression.Parameter(typeof(long), "objectId");
var hash = Expression.Parameter(typeof(Guid), "hash");
var call = Expression.Call(cache, get, objectId, hash);
var body = Expression.Convert(call, typeof(IRedbObject));
return Expression.Lambda>(body, cache, objectId, hash)
.Compile();
}
}
}