New to the php memcahed library in php. I was just wondering what the major difference between Memcached::add and Memcached::set are? Do both have the same performance over head and What is the advantage of using one over the other?
Another thing do these methods (::set and ::add) feature some sort of safe addition? Meaning, if the key doesnt exist in memcache it creates it, or if the key exist replace it? I do want to minimize duplicate keys. And the only way I can create some sort of safe adding is replacing first then checking if it was success, else create it.
The difference is documented on Memcached::add:
Memcached::add() is similar to Memcached::set(), but the operation fails if the key already exists on the server.
Memcached::add()
will return false if the key is already defined, meaning that's what you should use if you want to report an error for a duplicate key. Additionally, use Memcached::getResultCode() to check if the add was successful.
For performance comparisons, it may depend on the number of memcached servers, library versions and a number of factors specific to your application. Initially I would say it is a premature optimisation, but if you still want to compare, your best bet is to benchmark it with your own set-up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With