Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP Memcached get() Function

  • Last Updated : 06 Dec, 2021

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: The key of the item to retrieve.
  • cache_cb: Read-through caching callback or null.
  • flags: Flags to control the returned result. When Memcached::GET_EXTENDED is given, the function will also return the CAS token.

Return Values: Returns the value stored in the cache or false otherwise. If the flags is set to Memcached::GET_EXTENDED, an array containing the value and the CAS token is returned instead of only the value. The Memcached::getResultCode() will return Memcached::RES_NOTFOUND if the key does not exist.

Below program illustrate the Memcached::set() 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 (Key-value already stored):

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.get.php


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!