當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。