Following the answers of ZoFreX, if you look at the comments here: http://www.php.net/manual/en/memcache.set.php
You will see the following:
Using the set more than once for the same key leads to unexpected results - it does not behave like a βreplacementβ, but instead seems to βsetβ more than one value for the same key. "get" can return any of the values.
This was tested when setting up multiple servers - the behavior may be different if you have only one server.
So, really and really replace()
first looks for the existing key and then replaces it (if it exists), while set()
just adds the key. I believe that it is always better to use replace()
, assuming first that it returns FALSE
if the key is not found, in which case you would use add()
rather than set()
, since you know for sure that the key is not exists. This ensures that you will not have any unforeseen failures. So you can use the code:
$replace = Memcached::replace($key, $var); if ( ! $replace) { $set = Memcached::add($key, $var); }
source share