Search code examples
phpzend-frameworkzend-cache

using md5() to generate cache keys in Zend_Cache


I have some issues saving identifiers for cache objects in Zend_Cache. The Zend_Cache identifiers have to be really sanitized (no special chars, no spaces, etc). Some of my internal identifiers have spaces in it, so it's a problem for me to save cache objects.

I was thinking about converting the zend_cache identifier using md5() before saving it, such as:

$cacheId = md5(self::CACHE_PREFIX . $propertyId);

if (($address = $cache->load($cacheId)) === false) {
  .....
    $cache->save($cacheId, $address);
}

(Here for example, $propertyId might by a string with spaces)

My question is how unique are those md5 strings i'm creating? would it be possible that two of my cache objects will have the same identifier? Any suggestions?


Solution

  • My question is how unique are those md5 strings i'm creating?

    They are very unique. However, the more IDs you have the more likely is a collision (You need a very huge number of entries!). Also you can put the generated hashes into different "namespaces" like

    $cacheId = 'addresses_' . md5(self::CACHE_PREFIX . $propertyId);
    

    Instead of hashing you may also think about using the IDs itself. You said, that they contain some special characters. You can sanitize them e.g. using base64_encode()