From 14419b0907fd3132e9d5f6e3d24c45741917349a Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 16 Mar 2024 13:48:39 +0100 Subject: [PATCH] [backend/core] Warmup meta store cache using reflection (ISH-133) --- .../Core/Services/MetaService.cs | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Iceshrimp.Backend/Core/Services/MetaService.cs b/Iceshrimp.Backend/Core/Services/MetaService.cs index 0279abdc..d6110151 100644 --- a/Iceshrimp.Backend/Core/Services/MetaService.cs +++ b/Iceshrimp.Backend/Core/Services/MetaService.cs @@ -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(); + + 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 Fetch(Meta meta) => meta.GetConverter(await Fetch(meta.Key));