I have a caching method that generates long cache keys by concatenating a string list of price codes.
var cacheKey = "SalesPrice_" + string.Join("_", priceCodeList);
There can be 50+ priceCodes which leads to the cache key being too long. I am looking for a way to efficiently shorten this cache key by keeping the uniqueness.
Is there a way to solve this?
Edit: PriceCodeList is a string query parameter list for a web api i use. So I cannot cache each price code separately. This long string must be used.
8
EDIT: Per @MarcGravell ‘s findings, hashing cache keys slows the performance and might lead to hash collisions. Therefore my answer below does not address the question perfectly – even more – introduces new pitfalls (please read comments for more context).
Therefore, I would try to pass this long string as cache key directly to cache and monitor it regularly in case of any error. Hope that it works as expected.
I am keeping the question and answer for future reference.
PS: Please do not downvote the answer as – even though it is not relevant – it includes valuable take-aways. 🙂
I have come across hashing. I could pass a long string and it could shorten it for me by keeping uniqueness.
Here is the code that does that:
private static string GetHashedCacheKey(string key)
{
using var md5 = MD5.Create();
var inputAsBytes = Encoding.UTF8.GetBytes(key);
var hashBytes = md5.ComputeHash(inputAsBytes);
var sb = new StringBuilder();
foreach (var byt in hashBytes)
{
sb.Append(byt.ToString("X2")); // X2 ensures upper case hexadecimal characters
}
return sb.ToString();
}
How to use it:
var cacheKey = "SalesPrice_" + string.Join("_", priceCodeList);
var hashedKey = GetHashedCacheKey(cacheKey);
await _memoryCache.GetOrCreateAsync(hashedKey, async entry =>
{
entry.SetOptions(cacheEntryOptions);
return await value();
});
One thing that you should be super careful about is that since you register your cache with a hashed key, you must also do other operations with this hashed key.
In other words, all CRUD operations must be with the hashed key if you decide to implement this. So remember to hash your key before a cache operation takes place.
EDIT: OBS! Hash collisions can occur in this setting. So, if you want to implement this solution, you’d need to be super careful; for example, you might choose to include the original key in the payload, so that you can at least detect collisions and act as though it was a “miss”, rather than using the wrong data. [Credits: @Marc Gravell]
17