What is the point of replacing a function in Memcache?

What is the point of replacing a function in PHP memcache if you can just use set? Even if there is a variable, it automatically replaces it, right?

Can you give me an example where it is better to use a replacement instead of a set?

Thanks.

+4
source share
4 answers

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); } 
+4
source

Generally, a replacement should be used if the key is likely to be used frequently. Set / add / etc creates a new entry and can lead to fragmentation and a lot of cleanup. Replace the reuse of already allocated memory (if possible) and can be more stable and efficient. If this fails, using add / set will still work.

+1
source

No, which is completely wrong.

If you want to check and set the value, you must use GETS + CAS.

0
source

All Articles