2012-02-21 06:36
浏览 32


Reading this brief example of using memcached with PHP, I was wondering how memcached knows when a request for data needs to actually come from the database instead of coming from the cache.

图片转代码服务由CSDN问答提供 功能建议


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzhuan0309 2012-02-21 06:42

    It doesn't. It comes down to your caching strategy. That is so with all forms of cache, a tradeoff between getting the latest data and getting some data quickly. If you need to have the data up to date, invalidate (delete) the cache when updating the original. If performance is more important, let the cache expire by itself, at which point it will be renewed. Or something somewhere in-between. It depends on your restrictions and goals.

    点赞 打赏 评论
  • dongyiba8082 2012-02-21 06:38

    I think you need to program that logic.

    e.g. When you update the database then update the memcached value associated with that key, or make that key expire.

    点赞 打赏 评论
  • dtamho6404 2012-02-21 06:41

    It doesn't, you code does this. In most cases you will do something like this:

    key = /* build cache key somehow */
    data = memcache.get(key)
    if data is null:
      data = /* read data from database */
      cached.set(key, data)
    // now you can use the data
    点赞 打赏 评论

相关推荐 更多相似问题