Skip to content

An extension method that allows to get keys from Microsoft.Extensions.Caching.Memory.MemoryCache and a unit test for it.

License

Notifications You must be signed in to change notification settings

aannenko/MemoryCacheExtensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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.

About

An extension method that allows to get keys from Microsoft.Extensions.Caching.Memory.MemoryCache and a unit test for it.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages