Skip to content

Commit 462e4a5

Browse files
authored
Merge pull request #983 from 13xforever/vnext
Fix MemoryCacheExtensions and its tests
2 parents 1ba707a + c4aa6a4 commit 462e4a5

File tree

2 files changed

+13
-43
lines changed

2 files changed

+13
-43
lines changed

CompatBot/Utils/Extensions/MemoryCacheExtensions.cs

+10-40
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,15 @@ namespace CompatBot.Utils;
66

77
internal static class MemoryCacheExtensions
88
{
9-
private static readonly object throaway = new object();
9+
private static readonly object throaway = new();
1010

1111
public static List<T> GetCacheKeys<T>(this MemoryCache memoryCache)
12-
{
13-
// idk why it requires access before it populates the internal state
14-
memoryCache.TryGetValue("str", out _);
15-
memoryCache.TryGetValue(throaway, out _);
16-
17-
// get the internal state object
18-
var stateField = memoryCache.GetType()
19-
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
20-
.FirstOrDefault(fi => fi.Name == "_coherentState");
21-
var coherentState = stateField?.GetValue(memoryCache);
22-
if (coherentState is null)
23-
{
24-
Config.Log.Error($"Looks like {nameof(MemoryCache)} internals have changed in {nameof(coherentState)}");
25-
return [];
26-
}
27-
28-
// get the actual underlying key-value object
29-
var stringField = coherentState.GetType()
30-
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
31-
.FirstOrDefault(fi => fi.Name == "_stringEntries");
32-
var nonStringField = coherentState.GetType()
33-
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
34-
.FirstOrDefault(fi => fi.Name == "_nonStringEntries");
35-
if (stringField is null || nonStringField is null)
36-
{
37-
Config.Log.Error($"Looks like {nameof(MemoryCache)} internals have changed in {nameof(stringField)}");
38-
return [];
39-
}
40-
41-
// read the keys
42-
var value = typeof(T) == typeof(string)
43-
? (IDictionary?)stringField.GetValue(coherentState)
44-
: (IDictionary?)nonStringField.GetValue(coherentState);
45-
return value?.Keys.OfType<T>().ToList() ?? [];
46-
}
12+
=> memoryCache.Keys.OfType<T>().ToList();
4713

4814
public static Dictionary<TKey, ICacheEntry?> GetCacheEntries<TKey>(this MemoryCache memoryCache)
4915
where TKey: notnull
5016
{
17+
//memoryCache.TryGetValue();
5118
var stateField = memoryCache.GetType()
5219
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
5320
.FirstOrDefault(fi => fi.Name == "_coherentState");
@@ -58,13 +25,16 @@ public static List<T> GetCacheKeys<T>(this MemoryCache memoryCache)
5825
return new();
5926
}
6027

61-
var field = coherentState.GetType()
28+
var entriesName = "_nonStringEntries";
29+
if (typeof(TKey) == typeof(string))
30+
entriesName = "_stringEntries";
31+
var entriesField = coherentState.GetType() // MemoryCache.CoherentState
6232
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
63-
.FirstOrDefault(fi => fi.Name == "_entries");
64-
var cacheEntries = (IDictionary?)field?.GetValue(coherentState);
33+
.FirstOrDefault(fi => fi.Name == entriesName);
34+
var cacheEntries = (IDictionary?)entriesField?.GetValue(coherentState);
6535
if (cacheEntries is null)
6636
{
67-
Config.Log.Error($"Looks like {nameof(MemoryCache)} internals have changed in {cacheEntries}");
37+
Config.Log.Error($"Looks like {nameof(MemoryCache)} internals have changed in {nameof(coherentState)} cache entries");
6838
return new(0);
6939
}
7040

Tests/MemoryCacheExtensionTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public void GetCacheKeysTest()
3232
public void GetCacheEntriesTest()
3333
{
3434
var cache = new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromHours(1) });
35-
Assert.That(cache.GetCacheKeys<int>(), Is.Empty);
35+
Assert.That(cache.GetCacheEntries<int>(), Is.Empty);
3636

3737
cache.Set(13, "val13");
3838
cache.Set("bob", 69);
39-
Assert.That(cache.GetCacheKeys<int>(), Has.Count.EqualTo(1));
40-
Assert.That(cache.GetCacheKeys<string>(), Has.Count.EqualTo(1));
39+
Assert.That(cache.GetCacheEntries<int>(), Has.Count.EqualTo(1));
40+
Assert.That(cache.GetCacheEntries<string>(), Has.Count.EqualTo(1));
4141
}
4242
}

0 commit comments

Comments
 (0)