📜  PHP Memcached getByKey()函数

📅  最后修改于: 2022-05-13 01:56:29.577000             🧑  作者: Mango

PHP Memcached getByKey()函数

Memcached::getByKey()函数是PHP中 Memcached 类的内置函数,用于从特定服务器检索项目。这个函数在功能上等价于 Memcached::get()函数,只是可以使用自由格式的 server_key 将密钥映射到特定的服务器。

句法:

public Memcached::getByKey($server_key, 
    $key, $cache_cb = ?, $flags = ?): mixed

必需参数:此函数接受下面讨论的四个参数:

  • server_key:标识服务器的键,用于存储值或从中检索值。
  • key:要获取的项目的键。
  • cache_cb:通读缓存回调或 null
  • flags:控制返回结果的标志。当给定 Memcached::GET_EXTENDED 的值时,它将返回 CAS 令牌。

预期返回值:如果我们使用 Memcached::getByKey() 从特定服务器中查找键的值,我们可以预期两种类型的值,或者存储在缓存中的值,否则为 false。如果服务器中不存在 key,则 Memcached::getResultCode() 将返回 Memcached::RES_NOTFOUND 作为错误。

下面的示例说明了PHP中的 Memcached::getByKey()函数:

示例 1:

PHP
echo "
";
  
// 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);
  
?>


PHP
";
  
// 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);
  
?>


输出:

示例 2:

PHP

";
  
// 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);
  
?>

输出:

参考:https://www. PHP.net/manual/en/memcached.getbykey。 PHP