I’m wanting to build a cache with an eviction policy in C#. My key is a byte array (fixed at 32 bytes) and value is an instance of a specific class.
I’m debating the best way to do this. I’m thinking that MemoryCache is the way to go, but it uses string for a key. I could turn this into a hex-string but that incurs some overhead. Why isn’t the key an arbitrary object like in a dictionary?
It’s trivial to write a byte array comparer and there’s a suitable Dictionary constructor to supply an IEqualityComparer, but this approach doesn’t give me an eviction strategy for free.
Are there any other options I’m overlooking?
MemoryCacheis actually fairly complex under the hood (grab a copy of Reflector and take a look if you haven’t already). There are several things it does which are non-trivial to replicate; chief among these is approximating memory size used by cached objects.Performance-wise, you will be contending with much more significant impacts than the massaging of a key. Performance is acceptable, but key management is an insignificant part of the process.
You can see this difference by performing 100K+ add operations on a
DictionaryversusMemoryCache.Here’s a little hex algorithm you can use on your byte keys which I have tweaked to be as fast as possible. The BCL also contains base 16 functionality (which I didn’t know when I wrote this code and I’ve kept it around because it is simpler/faster).
As noted in the comments, converting the
byte[]to hex is probably not even needed to meet the stated requirements unless the key will be used elsewhere.