1
我在我的程序中使用了phpredis,在redis服务器中存储了一些内容,当同一请求出现时(在同一天)获取它们,但我总是得到空的结果。任何人都可以给我一些启示吗?这里是Cache类的代码我使用:set在redis中成功执行但没有任何作用
<?php
class Cache
{
public static function getInstance()
{
static $instance = null;
null == $instance && $instance = new self();
return $instance;
}
protected function __construct()
{
}
protected function getR()
{
static $r = NULL;
if (NULL == $r) {
$r = new Redis();
try {
$r->pconnect(HOST, PORT, 5);
} catch(Exception $ex) {
//log
try {
$api->connect(HOST, PORT, 5);
} catch (Exception $ex) {
//log
}
}
}
return $r;
}
public function getValue($key)
{
$result = array();
$r = $this->getR();
if(!empty($r)) {
try{
$result = $r->hKeys($key);
$r->setTimeout($keys, 86400);
} catch (Exception $ex){
//log
}
}
return $result; // return true
}
public function setValue($key, $value)
{
$result = false;
$r = $this->getR();
if(!empty($r)) {
try{
$result = $r->hMset($key, $value);
} catch (Exception $ex){
//log
}
}
}
}
?>
编辑:
我检查与Redis的-CLI的键值,找到了有线:该键值的数据存储在数据库5,而我认为它应该在DB 0默认没有选择语句,但程序检索数据库0,当然没有返回。现在我想知道为什么数据到了DB 5,因为我没有选择DB。
问题已被接受,但在发布完整的源代码之前,我们希望您可以进行一些测试,比如使用rediscli进行测试。另外,在没有任何警告的情况下捕捉异常时要小心,这里可能有些问题。 – tomsoft 2014-09-30 09:45:39