views:

214

answers:

1

We are using HttpRuntime.Cache API in an ASP.NET to cache data retrieved from a database.

For this particular application, our database queries feature a LOT of parameters, so our cache keys look something like this:

table=table1;param1=somevalue1;param2=somevalue2;param3=somevalue3;param4=somevalue4;param5=somevalue5;param6=somevalue6... etc...

For some queries, we have so many parameters that the cache key is several hundred characters long.

My question: is there a limit to the length of these cache keys? Internally, it is using a dictionary, so theoretically the lookup time should be constant. However, I wonder if we have potential to run into some performance/memory problem.

+2  A: 

Internally, Dictionary uses the hash code of the key you give it. Effectively every key is stored as an integer.

You have nothing to worry about.

BC
As an extreme example... assume that my cache key is 4K in size. This is more information than can be stored in an integer. How then, can this be stored as an integer and be unique?
frankadelic
.NET uses a hash algorithm to turn any string into a single integer (kinda like MD5 except the hash fits into 4 bytes). This algorithm was probably selected for a balance of speed and uniqueness, but hash collisions occur. The dictionary handles these collisions automatically. For specific details, check out the hash table entry in wikipedia: http://en.wikipedia.org/wiki/Hash_table
BC