©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(PECL memcached >= 0.1.0)
Memcached::get — 检索一个元素
$key
[, callback $cache_cb
[, float &$cas_token
]] ) Memcached::get() 返回之前存储在key
下的元素。如果元素被找到,并且提供
了cas_token
参数, 这个参数(译注:这个参数在函数定义中是引用参数,用来传出元素的版本标记,原理
可以查阅乐观锁资料)将会包含该元素的CAS标记值。关于CAS标记值的使用,请查看 Memcached::cas() 的说明。
另外,可以通过cache_cb
参数设置Read-through caching callback。
key
要检索的元素的key。
cache_cb
通读缓存回掉函数或 NULL
.
cas_token
检索的元素的CAS标记值。
返回存储在服务端的元素的值或者在其他情况下返回 FALSE
。
如果key不存在, Memcached::getResultCode() 返回 Memcached::RES_NOTFOUND
。
Example #1 Memcached::get() 示例 #1
<?php
$m = new Memcached ();
$m -> addServer ( 'localhost' , 11211 );
$m -> set ( 'foo' , 100 );
var_dump ( $m -> get ( 'foo' ));
?>
以上例程会输出:
int(100)
Example #2 Memcached::get() 示例 #2
<?php
$m = new Memcached ();
$m -> addServer ( 'localhost' , 11211 );
if (!( $ip = $m -> get ( 'ip_block' ))) {
if ( $m -> getResultCode () == Memcached :: RES_NOTFOUND ) {
$ip = array();
$m -> set ( 'ip_block' , $ip );
} else {
}
}
?>
[#1] denis_truffaut[A-T]hotmail[D-O-T]com [2011-01-17 06:01:36]
Note that this function can return NULL as FALSE, so don't make checks with === FALSE as with the old Memcache class, because it won't work. :O
Use the not (!) operator and check the result code with getResultCode() as mentioned in the documentation :)
[#2] miha at hribar dot info [2009-07-17 01:31:34]
This method also returns false in case you set the value to false, so in order to have a proper fault mechanism in place you need to check the result code to be certain that a key really does not exist in memcached.
<?php
$Memcached = new Memcached();
$Memcached->addServer('localhost', 11211);
$Memcached->set('key', false);
var_dump($Memcached->get('key')); // boolean false
var_dump($Memcached->getResultCode()); // int 0 which is Memcached::RES_SUCCESS
?>
Or just make sure the values are not false :)