donglao9606 2011-07-25 16:02
浏览 52
已采纳

使用md5()在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?

  • 写回答

1条回答 默认 最新

  • donglan8256 2011-07-25 16:20
    关注

    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()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)