Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcached php add vs set performance

Tags:

php

memcached

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.

like image 489
theintersect Avatar asked Sep 06 '25 03:09

theintersect


1 Answers

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.

like image 90
cmbuckley Avatar answered Sep 08 '25 00:09

cmbuckley