Check if a key exists in Memcache

前端 未结 11 2524
无人共我
无人共我 2021-02-12 15:17

How do I check in PHP if a value is stored in Memcache without fetching it? I don\'t like fetching it because the values I have set are all 1MB in size and after I fetch it, I h

11条回答
  •  天命终不由人
    2021-02-12 15:41

    I don't like the add/delete suggestion because it inserts invalid data your application may depend on being valid. If your application does make this assumption it would cause a subtle bug that occurs every so often due to the race condition it introduces.

    One solution is to take a look at the value and if its temp data pretend its not there, but that would require all application code to use the same api or you'd need to update the application itself, neither are fun and somewhat error prone due to the additional complexity.

    If the set of keys is small enough you could store that in memcached and use that determine whether or not to retrieve the data from source again. However, if its as large or larger the the value this method is worse then just getting the entire value from memcached.

    You could then solve the new problem by using a index to separate your keys into smaller sets (of course a good way to shard this data so each bucket is a certain size is easier said then done.)

    The implementation would consist of using memcached append or prepend to maintain your list of keys tied to some master key or a master key which points to a set of sub_keys which point to the keys themselves :)

    Either way you're making the application more and more complex, so I'd only recommend doing this if there is indeed a bottleneck (like there would be if the keys existence needs to be checked often over a network), or if usability in a concern (latency).

    In my case I will not do this because I am only going to be accessing memcached over localhost and am using it as more of an extension to my application to cache resources that take more then a few seconds to load normally.

提交回复
热议问题