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.
Syntax:
public Memcached::getByKey($server_key,
$key, $cache_cb = ?, $flags = ?): mixed
Required Parameters: This function accepts four parameters that are discussed below:
- server_key: The key identifying the server to store the value on or retrieve it from.
- key: The key of the item to fetch.
- cache_cb: Read-through caching callback or null
- flags: Flags to control the returned result. When the value of Memcached::GET_EXTENDED is given, it will return the CAS token.
Expected return value: For finding the value of a key from the specific server if we use Memcached::getByKey(), we can expect two type of values either the value stored in the cache or false otherwise. In case if key does not exist in the server Memcached::getResultCode() will return Memcached::RES_NOTFOUND as error.
Below examples illustrate the Memcached::getByKey() function in PHP:
Example 1:
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 = "GEEKSFORGEEKS1"; $value = "computer science portal"; $ttl = 3600; if ($memcacheD->addByKey( "serverkey", $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->getByKey("serverKey", $key); var_dump($valD); ?> |
Output:
* server added **
** added key-value (GEEKSFORGEEKS1:computer science portal)to cache successfully!! **
**** FETCHED VALUE FOR KEY :GEEKSFORGEEKS1 ****
string(23) “computer science portal”
Example2:
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 = "GEEKSFORGEEKS2"; $value = "computer science portal"; $ttl = 3600; if ($memcacheD->addByKey( "serverkey", $key, $value, $ttl)) echo "** added key-value (" . $key . ":" . $value . ")to cache successfully!! **\n"; // Already added, try it once if ($memcacheD->addByKey( "serverkey", $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->getByKey("serverKey", $key); var_dump($valD); ?> |
Output:
** server added **
** added key-value (GEEKSFORGEEKS2:computer science portal)to cache successfully!! **
** error while adding value to cache!! **
**** FETCHED VALUE FOR KEY :GEEKSFORGEEKS2 ****
string(23) “computer science portal”
Reference: https://www.php.net/manual/en/memcached.getbykey.php



