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