Thursday, December 11, 2025
HomeLanguagesPHP Memcached add() Function

PHP Memcached add() Function

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 :neveropen ****
string(23) “computer science portal”

Reference: https://www.php.net/manual/en/memcached.add.php

RELATED ARTICLES

Most Popular

Dominic
32443 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12025 POSTS0 COMMENTS
Shaida Kate Naidoo
6943 POSTS0 COMMENTS
Ted Musemwa
7196 POSTS0 COMMENTS
Thapelo Manthata
6889 POSTS0 COMMENTS
Umr Jansen
6880 POSTS0 COMMENTS