@@ -6,48 +6,15 @@ namespace CompatBot.Utils;
6
6
7
7
internal static class MemoryCacheExtensions
8
8
{
9
- private static readonly object throaway = new object ( ) ;
9
+ private static readonly object throaway = new ( ) ;
10
10
11
11
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 ( ) ;
47
13
48
14
public static Dictionary < TKey , ICacheEntry ? > GetCacheEntries < TKey > ( this MemoryCache memoryCache )
49
15
where TKey : notnull
50
16
{
17
+ //memoryCache.TryGetValue();
51
18
var stateField = memoryCache . GetType ( )
52
19
. GetFields ( BindingFlags . NonPublic | BindingFlags . Instance )
53
20
. FirstOrDefault ( fi => fi . Name == "_coherentState" ) ;
@@ -58,13 +25,16 @@ public static List<T> GetCacheKeys<T>(this MemoryCache memoryCache)
58
25
return new ( ) ;
59
26
}
60
27
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
62
32
. 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 ) ;
65
35
if ( cacheEntries is null )
66
36
{
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 ") ;
68
38
return new ( 0 ) ;
69
39
}
70
40
0 commit comments