Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MemoryCache case insensitive key lookup

Tags:

.net

I'm trying to find a way to make the .net 4.0 MemoryCache.Default instance use a case insensitive comparer.

Is that possible?

var bob = new object();

MemoryCache.Default["Bob"] = bob;

bob == MemoryCache.Default["bob"]; --> true
like image 883
BillRob Avatar asked Dec 24 '12 00:12

BillRob


1 Answers

From looking at the code through ILSpy, it doesn't appear to be possible. Because behind the scenes it is ultimately using the GetHashCode() of your key string.

I think the easiest work-around for this, is to implement a custom cache, extending MemoryCache that overrides all methods that interact with the key, and calls ToUpperInvariant() when passing it as parameter to the base call.

like image 94
Pablo Romeo Avatar answered Oct 10 '22 03:10

Pablo Romeo