MemoryCache
is actually quite complicated under the hood (grab a copy of Reflector and see if you haven't already). There are several things that are not trivial to replicate; the main one is the approximation of the amount of memory used by cached objects.
In terms of performance, you will deal with significantly more significant impacts than massaging the key. Performance is acceptable, but key management is an insignificant part of the process.
You can see this difference by doing 100K + add operations in Dictionary
compared to MemoryCache
.
Here is a small hexadecimal algorithm that you can use on your byte keys, which I configured as quickly as possible. BCL also contains basic functionality 16 (which I did not know when I wrote this code, and I saved it because it is simpler / faster).
As noted in the comments, converting byte[]
to hex is probably not even required to satisfy stated requirements if the key is not to be used elsewhere.
public unsafe sealed class Hex { private static readonly char[] _hexRange = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
source share