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
The easiest way is to get the given key and cast it to a boolean value.
(bool) Memcache::get(string $key)
(a little late to the discussion, but) actually you can access all the keys/values stored in the memcache, per:
$allSlabs = $memcache->getExtendedStats('slabs');
$items = $memcache->getExtendedStats('items');
foreach ($allSlabs as $server => $slabs)
foreach ($slabs as $slabId => $slabMeta)
foreach ($memcache->getExtendedStats('cachedump', (int) $slabId) as $entries)
if (!empty($entries))
foreach ($entries as $key => $entry)
This makes no sense from Memcached perspective. If you want to avoid the slow datasource (by a scheduled job, I presume?), save the data to a faster but still stable datasource (e.g. a file) in your scheduled job. When you need the data, try to read from Memcached first, and if that fails read the file and save it into Memcached.
Memcached cannot give you a good answer, as pakore have already answered. The architecture is not meant to be a stable datasource.
I am not sure if this is of any help to you, but you can use
Memcache::add ( string $key , mixed $var)
It will return false if the key already exists.
In case true is returned you may use
Memcache::delete ( string $key)
to remove the key you just set. That way you won't need to fetch the data.
Basically what you want to do is to fill memcached with your data, right?
The thing is that asking if a key is there withouth retrieving the value is not very useful. See this case scenario:
You ask if a key exists and it does. Just after you ask, the data of the key is expelled from the cache. While your response is coming back saying that the date is there, the reality is that the data is not there. So you lost time asking, because the answer is different from reality.
I guess what you want to do is to ask if a key exists, and then if it does not, fill it with your data. Why don't you fill directly all the data? Btw, have you considered that while you are filling memcached with data, you could be expelling keys you just previously inserted?
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.