Skip to content

Latest commit

 

History

History
26 lines (20 loc) · 1.56 KB

File metadata and controls

26 lines (20 loc) · 1.56 KB

Build and Test
This Build and Test workflow runs weekly. If its status is green, it indicates that the extension method MemoryCache.GetKeys() remains compatible with the latest 8.x version of the Microsoft.Extensions.Caching.Memory package.

Important

Consider adding this unit test for GetKeys() to your solution to catch problems early.

MemoryCacheExtensions

Get keys from MemoryCache (specifically, Microsoft.Extensions.Caching.Memory.MemoryCache): add the class MemoryCacheExtensions from this repository to your code and call memoryCache.GetKeys().

var cache = new MemoryCache(new MemoryCacheOptions());
cache.GetOrCreate(1, ce => "one");
cache.GetOrCreate("two", ce => "two");

foreach (var key in cache.GetKeys())
    Console.WriteLine($"Key: '{key}', Key type: '{key.GetType()}'");

foreach (var key in cache.GetKeys<string>())
    Console.WriteLine($"Key: '{key}', Key type: '{key.GetType()}'");

// Output:
// Key: '1', Key type: 'System.Int32'
// Key: 'two', Key type: 'System.String'
// Key: 'two', Key type: 'System.String'

MemoryCacheExtensions uses reflection to build a few delegates which, when built, allow you to tap into MemoryCache internals and efficiently retrieve current keys.