Search code examples
c#cachinghashncache

Should cache keys be hashed?


I am working on an existing system that using NCache. it is a distributed system with large caching requirements, so there is no question that caching is the correct answer, but...

For some reason, in the existing code, all cache keys are hashed before storing in the cache.

My argument is that we should NOT hash the key, as the caching library may have some super optimized way of storing it's dictionary and hashing everything means we may actually be slowing down lookups if we do this.

The guy who originally wrote the code has left, and the knowledge of why the keys are cached has been lost.

Can anyone suggest if hashing is the correct thing to do, or should it be removed.


Solution

  • Okay so your question is

    1. Should we hash the keys before storing?
    2. If you yourself do hashing, will it slow down anything

    Well, the cache API works on strings as keys. In the background NCache automatically generates hashes against these keys which help it to identify where the object should be stored. And by where I mean in which node.

    When you say that your application Hashes keys before handing it over to NCahe, then it is simple an unnecessary step. NCache API was meant to take this headache from you.

    BUT if those hashes were generated because of some internal Logic within your application then that's another case. Please check carefully.

    Needless to say, if you're doing something again and again then it will definitely have a performance degradation. The Hash strings that you provide will be used again to generate another hash value (int).