PHP Memcached add() Function Last Updated : 06 Dec, 2021 Comments Improve Suggest changes Like Article Like Report The Memcached::add() function is an inbuilt function of memcached class in PHP which is used to set/add a given value under a given value with an expiration time(TTL) on memcache server. This function is similar to Memcached::set() function, but the operation fails if the key already exists on the server. Syntax: public Memcached::add( $key, $value, $expiration = ?): bool Parameters: This function accepts three parameters that are: key: The key under which to store the value.value: The value to store.expiration: The expiration time, defaults to 0. See Expiration Times for more info. Return Values: This function returns true in case key-value pair stored successfully and false in case of any failure. The Memcached::getResultCode() function will return Memcached::RES_NOTSTORED if the key already exists. Below examples illustrate the Memcached::add() function in PHP: Example 1: PHP <?php echo "<pre>"; // Server & port details $server = '127.0.0.1'; $port = 11211; // Initiate a new object of memcache $memcacheD = new Memcached(); // Add server if ($memcacheD->addServer($server, $port)) { echo "** server added ** \n"; } else { echo "** issue while creating a server **\n"; } // Set key & value with TTL $key = "GEEKSFORGEEKS"; $value = "computer science portal"; $ttl = 3600; if ($memcacheD->add($key, $value, $ttl)) echo "** added key-value (" . $key . ":" . $value . ")to cache successfully!! **\n"; else echo "** error while adding value to cache!! **\n"; // Get value of key echo "**** FETCHED VALUE FOR KEY :" . $key . " ****\n"; $valD = $memcacheD->get($key); var_dump($valD); ?> Output: ** server added ** ** added key-value (GEEKSFORGEEKS:computer science portal)to cache successfully!! ** **** FETCHED VALUE FOR KEY :GEEKSFORGEEKS **** string(23) "computer science portal" Example 2 (already existing key-pair): PHP <?php echo "<pre>"; // Server & port details $server = '127.0.0.1'; $port = 11211; // Initiate a new object of memcache $memcacheD = new Memcached(); // Add server if ($memcacheD->addServer($server, $port)) { echo "** server added ** \n"; } else { echo "** issue while creating a server **\n"; } // Set key & value with TTL $key = "GEEKSFORGEEKS"; $value = "computer science portal"; $ttl = 3600; if ($memcacheD->add($key, $value, $ttl)) echo "** added key-value (" . $key . ":" . $value . ")to cache successfully!! **\n"; else echo "** error while adding value to cache!! **\n"; // Get value of key echo "**** FETCHED VALUE FOR KEY :" . $key . " ****\n"; $valD = $memcacheD->get($key); var_dump($valD); ?> Output: ** server added ** ** error while adding value to cache!! :: MSG:: NOT STORED ** **** FETCHED VALUE FOR KEY :geeksforgeeks **** string(23) "computer science portal" Reference: https://www.php.net/manual/en/memcached.add.php Comment More infoAdvertise with us Next Article PHP Memcached add() Function S Shivam.Pradhan Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-Memcached Similar Reads PHP Memcached addServer() Function The Memcached::add() function is an inbuilt function of memcached class in PHP which is used to add a server to the server pool. It adds the specified server to the server pool. No connection is established to the server at this time, but if you are using consistent key distribution option (via Memc 2 min read PHP Memcached get() Function The Memcached::get() function is an inbuilt function of memcached class in PHP which is used to get the value under a given key stored on memcache server. Syntax: public Memcached::get( $key, callable $cache_cb = ?, $$flags = ?): mixed Parameters: This function accepts three parameters that are: key 3 min read PHP Memcached getByKey() Function The Memcached::getByKey() function is an inbuilt function of Memcached class in PHP which is used to retrieve an item from a specific server. Functionally this function is equivalent to Memcached::get() function, except that the free-form server_key can be used to map the key to a specific server. S 3 min read PHP | IntlCalendar add() Function The IntlCalendar::add() function is an inbuilt function in PHP which is used to add a signed amount of time to a field. Syntax: Object oriented style: bool IntlCalendar::add( int $field, int $amount ) Procedural style: bool intlcal_add( IntlCalendar $cal, int $field, int $amount ) Parameters: $cal: 2 min read PHP | Imagick addImage() Function The Imagick::addImage() function is an inbuilt function in PHP which is used to adds new image to Imagick object image list. After the operation iterator position is moved at the end of the list. This function adds new image to Imagick object from the current position of the source object. The Imagi 1 min read PHP | Gmagick addImage() Function The Gmagick::addImage() function is an inbuilt function in PHP which is used to adds new image to Gmagick object image list. This function adds a new image to Gmagick object from the current position of the source object. The Gmagick class have the ability to hold and operate on multiple images simu 2 min read PHP Memcached::getServerList() Function The Memcached::getServerList() function is an inbuilt function of memcached class in PHP which is used to get the list of the servers in the pool of memcache server. Syntax: public Memcached::getServerList(): array Parameters: This function has no parameters. Return Value: This function returns an a 2 min read PHP | array_map() Function The array_map() is an inbuilt function in PHP and it helps to modify all elements one or more arrays according to some user-defined condition in an easy manner. It basically, sends each of the elements of an array to a user-defined function and returns an array with new values as modified by that fu 2 min read PHP array_pad() Function The array_pad() is a builtin function in PHP and is used to pad a value fixed number of time onto an array. This function inserts an element specified number of times into an array either at front or back. Syntax: array array_pad($input_array, $input_size, $values) Parameters: This function accepts 3 min read PHP memcached Cache has an important role in system design whenever it comes to the key-value store with a very less fetch time. So to remove database latency we all use cache memory which is a bit volatile but is highly available and it's easy to use as a session store and similar use case. The Memcached is a ty 2 min read Like