php memcached get,PHP Memcached操作类
/**
* 緩存驅動
* @author Devil
* @version 1.0.0
*/
class CacheLibrary
{
private $c_obj;
private $c_time;
/**
* [__construct 構造方法]
*/
public function __construct($host, $port = 11211)
{
/* 實例化memcached */
$this->c_obj = new Memcached();
if(!$this->c_obj->addServer($host, $port)) Api_Return(L('code_413'), 413);
/* 基礎參數設置 */
$this->c_time = empty(C('cache')['time']) ? 0 : C('cache')['time'];
}
/**
* [GetTime 獲取緩存時間]
* @return [int] [緩存時間]
*/
private function GetTime($time = 0)
{
return (intval($time) > 0) ? intval($time) : $this->c_time;
}
/**
* [Add 緩存添加]
* @param [string] $key [索引]
* @param [mixed] $val [值]
* @param [integer] $time [過期時間(單位秒), 0永久, 或時間戳]
* @return [boolean] [成功true, 失敗false]
*/
public function Add($key, $val, $time = 0)
{
return $this->c_obj->add($key, $val, $this->GetTime($time));
}
/**
* [Set 緩存替換]
* @param [string] $key [索引]
* @param [mixed] $val [值]
* @param [integer] $time [過期時間(單位秒), 0永久, 或時間戳]
* @return [boolean] [成功true, 失敗false]
*/
public function Set($key, $val, $time = 0)
{
return $this->c_obj->set($key, $val, $this->GetTime($time));
}
/**
* [SetMulti 設置多個索引的緩存數據]
* @param [type] $key_all [索引數組]
* @param [integer] $time [過期時間(單位秒), 0永久, 或時間戳]
* @return [boolean] [成功true, 失敗false]
*/
public function SetMulti($key_all, $time = 0)
{
return $this->c_obj->setMulti($key_all, $time);
}
/**
* [Append 向已存在索引后面追加數據]
* @param [string] $key [索引]
* @param [string] $val [追加的數據(字符串)]
*/
public function Append($key, $val)
{
$this->setOption();
return $this->c_obj->append($key, $val);
}
/**
* [Prepend 向已存在索引前面追加數據]
* @param [string] $key [索引]
* @param [string] $val [追加的數據(字符串)]
*/
public function Prepend($key, $val)
{
$this->setOption();
return $this->c_obj->prepend($key, $val);
}
/**
* [Replace 替換已存在索引下的元素]
* @param [string] $key [索引]
* @param [mixed] $val [值]
* @param [integer] $time [過期時間(單位秒), 0永久, 或時間戳]
* @return [boolean] [成功true, 失敗false]
*/
public function Replace($key, $val, $time = 0)
{
$this->c_obj->replace($key, $val, $time);
}
/**
* [setOption 設置選項]
*/
private function setOption()
{
$this->c_obj->setOption(Memcached::OPT_COMPRESSION, false);
}
/**
* [Fetch 抓取下一個結果]
* @param [string] $key_all [索引名]
* @param [boolean] $cas [是否返回長度, 是true, 否false默認]
*/
public function Fetch($key_all, $cas = false)
{
$this->GetDelayed($key_all, $cas);
return $this->c_obj->fetch();
}
/**
* [FetchAll 抓取所有剩余的結果]
* @param [string] $key_all [索引名]
* @param [boolean] $cas [是否返回長度, 是true, 否false默認]
*/
public function FetchAll($key_all, $cas = false)
{
$this->GetDelayed($key_all, $cas);
return $this->c_obj->fetchAll();
}
/**
* [GetDelayed 請求多個索引]
* @param [string] $key_all [索引名]
* @param [boolean] $cas [是否返回長度, 是true, 否false默認]
*/
private function GetDelayed($key_all, $cas)
{
$this->c_obj->getDelayed($key_all, $cas);
}
/**
* [Get 緩存獲取]
* @param [string] $key [索引]
* @return [mixed] [當前索引對應的數據]
*/
public function Get($key)
{
return $this->c_obj->get($key);
}
/**
* [GetMulti 獲取多個索引的緩存數據]
* @param [type] $key_all [索引數組]
*/
public function GetMulti($key_all)
{
return $this->c_obj->getMulti($key_all);
}
/**
* [Delete 刪除一個索引]
* @param [string] $key [索引]
* @param [integer] $time [等待時間(單位秒), 0立即, 或時間戳]
* @return [boolean] [成功true, 失敗false]
*/
public function Delete($key, $time = 0)
{
return $this->c_obj->delete($key, $time);
}
/**
* [DeleteMulti 刪除多個索引]
* @param [array] $key_all [索引數組]
* @param [integer] $time [過期時間(單位秒), 0立即, 或時間戳]
* @return [boolean或array] [成功true, 失敗false]
*/
public function DeleteMulti($key_all, $time = 0)
{
$s = $this->c_obj->deleteMulti($key_all, $time);
if(empty($s)) return false;
foreach($s as $key=>$val)
{
if($val !== true) if($this->Get($key) == false) $s[$key] = true;
}
return $s;
}
/**
* [Flush 作廢所有元素]
* @param [integer] $time [等待時間(單位秒), 0立即]
* @return [boolean] [成功true, 失敗false]
*/
public function Flush($time = 0)
{
return $this->c_obj->flush($time);
}
}
?>
總結
以上是生活随笔為你收集整理的php memcached get,PHP Memcached操作类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全国铁路列车时刻表(如何查询火车时刻表)
- 下一篇: 十大必吃兰州美食推荐