dsg24156 2013-12-11 11:11
浏览 67
已采纳

PHP memcached更新结果

I'm about to implement a memcached class which can be extended by our database class. However i have looked at many different ways of doing this.

My First question was, what is the point in the Memcached::set() as it seems to replace the value of the key. Does this not defeat the object of caching your results?

My Second question was, technically speaking what is the fastest/best way to update the value of a key without checking the results every time the query is executed. If this happens, the results been retreived would have to be checked constantly so there would be no point in caching as the results would constantly be connecting to the mysql database.

Lastly, What is the best way of creating a key? most people recommend using MD5 however using MD5 on a PDO query would be the same regardless.

I.E.

$key = MD5("SELECT * FROM test WHERE category=?");

the category could produce many different results however the key would be constantly replaced. Is there a best practice for this?

  • 写回答

1条回答 默认 最新

  • dongmi5015 2013-12-11 12:48
    关注

    You set a cache entry when you had to read the database, so that next time, you don't have to read the database first. You'd check the cache, and if it was not there, or otherwise out of date, then you fall back to the database read, and reset the key.

    As for a key name, it depends very much on the expected values of the category. If if was a simple integer, or string, I'd use a key like test.category:99 or test.category:car. If it was likely to be more, it may be useful to encode it, so there were no spaces in it (say, urlencode).

    Finally, if it were any more complex than that - test:category:{MD5(category)}.

    Since the key is only a reference to the data and you'll never be using it in any kind of SQL query, putting the value in there is not generally going to be a security issue.

    Since you control when the cache is set, if the underlying database entry is changed, it's simple to also update the cache with the new data at the same time - you just have to use the same key.

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

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决