Dan Lugg
Dan Lugg

Reputation: 20592

Array lookup speed in PHP; long cache keys

I'm working on a method that, due to the expense of it's operation and frequency which it is called with identical arguments, would benefit from caching the return values.

I'll be serialize()-ing the arguments together for cache keys, but this can result in very long keys, due to the lengthy array arguments.


Minor clarifications:
This is only per-request caching, with no permanent storage. The method in question is that of a view helper, and for each view generation it may be called 500 times or more.

Upvotes: 3

Views: 1654

Answers (2)

fyr
fyr

Reputation: 20859

You should definitely hash the key. You would maybe say "Why should i risk getting collisons when i can concatenate at every point of time a unique key?". The easy answer is if you generate cache keys via string concatenation you have to calculate always with the worst case of space requirements to estimate ram usage.

So if you have a cache with 200 entries .. and 2 fields with 20character max strings. The worst case would be 200*2*20*(size of character). If you load the complete cache on every possible parallel connection this will multiply by the amount of parallel connections.

With hashes you always have the minimum ram requirement = maximum ram requirement for the key field.

If you have many values which are concatenated for the keys this will scale very bad.

Edit:

Even if you use it per request the array consumes memory. Though it is a cache it is present from the beginning to the end of the request. So you need to take into account that it consumes a certain amount of space in your memory and while using a hash its a fixed amount.

The second thing is that keys need to be compared. If you access an associative array with a string key your interpreter needs to compare the key charwise. If you have a hash method for generating keys this will be also a fixed number of steps.

If you use concatenation the amount of steps will range between the best and the worst case.

Upvotes: 4

46bit
46bit

Reputation: 1537

It's certainly not typical to have such long array keys, but you'd probably have to do benchmarking to guess how much slowdown it actually gives. If in doubt though, just md5 it - from what you're saying the speedup will still be so much compared to the md5 time that the latter will be trivial.

Upvotes: 1

Related Questions