Open In App

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


Next Article

Similar Reads