[backend/core] Warmup meta store cache using reflection (ISH-133)

This commit is contained in:
Laura Hausmann 2024-03-16 13:48:39 +01:00
parent be27893c32
commit 14419b0907
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -1,3 +1,4 @@
using System.Reflection;
using EntityFramework.Exceptions.Common;
using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables;
@ -46,10 +47,32 @@ public class MetaService(IServiceScopeFactory scopeFactory, IDistributedCache ca
public async Task WarmupCache()
{
await Get(MetaEntity.VapidPrivateKey);
await Get(MetaEntity.VapidPublicKey);
await Get(MetaEntity.InstanceName);
await Get(MetaEntity.InstanceDescription);
var entities = typeof(MetaEntity).GetMembers(BindingFlags.Static | BindingFlags.Public)
.OfType<FieldInfo>();
foreach (var entity in entities)
{
var value = entity.GetValue(this);
var type = entity.FieldType;
while (type?.GenericTypeArguments == null ||
type.GenericTypeArguments.Length == 0 ||
type.GetGenericTypeDefinition() != typeof(Meta<>))
{
if (type == typeof(object) || type == null)
continue;
type = type.BaseType;
}
var genericType = type.GenericTypeArguments.First();
var task = typeof(MetaService)
.GetMethod(nameof(Get))!
.MakeGenericMethod(genericType)
.Invoke(this, [value]);
await (Task)task!;
}
}
private async Task<T> Fetch<T>(Meta<T> meta) => meta.GetConverter(await Fetch(meta.Key));