HomeLanguagesPHP Memcached get() Function

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

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32548 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6922 POSTS0 COMMENTS
Nicole Veronica
12039 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12146 POSTS0 COMMENTS
Shaida Kate Naidoo
7059 POSTS0 COMMENTS
Ted Musemwa
7301 POSTS0 COMMENTS
Thapelo Manthata
7017 POSTS0 COMMENTS
Umr Jansen
7005 POSTS0 COMMENTS