Best way to invalidate a number of memcache keys using standard php libraries?

后端 未结 3 905
梦如初夏
梦如初夏 2021-02-04 18:56

I have a database with files which can be searched, browsed and have multiple copies on multiple servers.

I cache searches, browse pages and server locations (urls). Say

相关标签:
3条回答
  • 2021-02-04 19:27

    See Organizing memcache keys

    Unless you can "master key" items, there's no sane way to do this. By this I mean something like "user4231-is_valid". You could check that for anything that used that user's data. Otherwise, unless you're tracking everything that references your file in question, you can't invalidate all of them. If you do that, you still have to iterate all possibilities in order to successfully delete.

    Document your dependencies, limit your dependencies, track your dependencies in your code for deletion activities.

    0 讨论(0)
  • 2021-02-04 19:31

    I have no experience with memcached, but I understand that IO are cheap there.

    I'd go with your tag implementation, make sure the tag list is used frequently and hope that the internal mmcd' logic would "think" that it's something too busy to be dropped :)

    0 讨论(0)
  • 2021-02-04 19:43

    Having come accross the comment here, which explains the logic of evicting existing keys, I believe tags can be implemented reliable by the version flags approach mentioned in: PHP memcache design patterns

    I actually implemented this logic once already, but discarded it as unreliable due to memcache eviction of elements before they expire. You can find my initial implementation here. I do however, believe this is a reliable tags pattern because:

    • On cache object retrieval, the object is moved on top of the LRU stack
    • All tags/flags are retrieved right after the cache object
    • If a flag is evicted, any item containing this flag would have been evicted just before
    • Incrementing a flag returns the new value in the same operation, avoiding write concurrency. If a second thread is incrementing the same flag before the cache object is written, it is by definition invalid already

    Please correct me if I'm wrong! :-)

    0 讨论(0)
提交回复
热议问题