当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP Memcached getByKey()用法及代码示例


Memcached::getByKey() 函数是 PHP 中 Memcached 类的内置函数,用于从特定服务器检索项目。该函数在函数上与 Memcached::get() 函数等效,不同之处在于 free-form server_key 可用于将 key 映射到特定服务器。

用法:

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

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

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

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

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

范例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);
  
?>

输出:

*  server added **
** added key-value (GEEKSFORGEEKS1:computer science portal)to cache successfully!! **
****   FETCHED VALUE   FOR KEY:GEEKSFORGEEKS1 ****
string(23) “computer science portal”

范例2:

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

输出:

**  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”

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


相关用法


注:本文由纯净天空筛选整理自Shivam.Pradhan大神的英文原创作品 PHP Memcached getByKey() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。