mirror of
https://github.com/RPCS3/discord-bot.git
synced 2024-11-26 19:50:36 +00:00
update MemoryCache hack extension to support the new implementation
This commit is contained in:
parent
bc07b7700a
commit
2d47dccc12
@ -8,8 +8,15 @@ namespace CompatBot.Utils;
|
||||
|
||||
internal static class MemoryCacheExtensions
|
||||
{
|
||||
private static readonly object throaway = new object();
|
||||
|
||||
public static List<T> GetCacheKeys<T>(this MemoryCache memoryCache)
|
||||
{
|
||||
// idk why it requires access before it populates the internal state
|
||||
memoryCache.TryGetValue("str", out _);
|
||||
memoryCache.TryGetValue(throaway, out _);
|
||||
|
||||
// get the internal state object
|
||||
var stateField = memoryCache.GetType()
|
||||
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
|
||||
.FirstOrDefault(fi => fi.Name == "_coherentState");
|
||||
@ -17,20 +24,27 @@ internal static class MemoryCacheExtensions
|
||||
if (coherentState is null)
|
||||
{
|
||||
Config.Log.Error($"Looks like {nameof(MemoryCache)} internals have changed");
|
||||
return new();
|
||||
return [];
|
||||
}
|
||||
|
||||
var field = coherentState.GetType()
|
||||
// get the actual underlying key-value object
|
||||
var stringField = coherentState.GetType()
|
||||
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
|
||||
.FirstOrDefault(fi => fi.Name == "_entries");
|
||||
if (field is null)
|
||||
.FirstOrDefault(fi => fi.Name == "_stringEntries");
|
||||
var nonStringField = coherentState.GetType()
|
||||
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
|
||||
.FirstOrDefault(fi => fi.Name == "_nonStringEntries");
|
||||
if (stringField is null || nonStringField is null)
|
||||
{
|
||||
Config.Log.Error($"Looks like {nameof(MemoryCache)} internals have changed");
|
||||
return new();
|
||||
return [];
|
||||
}
|
||||
|
||||
var value = (IDictionary?)field.GetValue(coherentState);
|
||||
return value?.Keys.OfType<T>().ToList() ?? new List<T>();
|
||||
// read the keys
|
||||
var value = typeof(T) == typeof(string)
|
||||
? (IDictionary?)stringField.GetValue(coherentState)
|
||||
: (IDictionary?)nonStringField.GetValue(coherentState);
|
||||
return value?.Keys.OfType<T>().ToList() ?? [];
|
||||
}
|
||||
|
||||
public static Dictionary<TKey, ICacheEntry?> GetCacheEntries<TKey>(this MemoryCache memoryCache)
|
||||
|
@ -14,7 +14,14 @@ public class MemoryCacheExtensionTests
|
||||
var cache = new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromHours(1) });
|
||||
Assert.That(cache.GetCacheKeys<int>(), Is.Empty);
|
||||
|
||||
cache.Set(13, "val13");
|
||||
const string testVal = "vale13";
|
||||
cache.Set(13, testVal);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(cache.TryGetValue(13, out string expectedVal), Is.True);
|
||||
Assert.That(expectedVal, Is.EqualTo(testVal));
|
||||
}
|
||||
);
|
||||
Assert.That(cache.GetCacheKeys<int>(), Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user