Memcached::get() 函數是 PHP 中 memcached 類的內置函數,用於獲取存儲在 memcache 服務器上的給定鍵下的值。
用法:
public Memcached::get( $key, callable $cache_cb = ?, $$flags = ?):mixed
參數:這個函數接受三個參數:
- key:要檢索的項目的鍵。
- cache_cb:Read-through 緩存回調或空。
- flags:用於控製返回結果的標誌。當給定 Memcached::GET_EXTENDED 時,該函數還將返回 CAS 令牌。
返回值:返回存儲在緩存中的值,否則返回 false。如果標誌設置為 Memcached::GET_EXTENDED,則返回一個包含值和 CAS 令牌的數組,而不僅僅是返回值。如果鍵不存在,Memcached::getResultCode() 將返回 Memcached::RES_NOTFOUND。
以下示例程序旨在說明 PHP 中的 Memcached::set() 函數:
範例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);
?>
輸出:
** server added **
** added key-value (GEEKSFORGEEKS:computer science portal)to cache successfully!! **
**** FETCHED VALUE FOR KEY:GEEKSFORGEEKS ****
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 = "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);
?>
輸出:
** server added **
** error while adding value to cache!!::MSG::NOT STORED **
**** FETCHED VALUE FOR KEY:geeksforgeeks ****
string(23) “computer science portal”
參考: https://www.php.net/manual/en/memcached.get.php
相關用法
- PHP Memcached::getServerList()用法及代碼示例
- PHP Memcached add()用法及代碼示例
- PHP Memcached addServer()用法及代碼示例
- PHP Ds\Deque get()用法及代碼示例
- PHP Ds\Sequence get()用法及代碼示例
- PHP Ds\Vector get()用法及代碼示例
- PHP Ds\Set get()用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- PHP IntlCalendar get()用法及代碼示例
- PHP dir()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam.Pradhan大神的英文原創作品 PHP Memcached get() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。