龚哥哥 - 山里男儿 爱生活、做自己!
PHP Memcached操作类
发表于 2015-8-31 | PHP
<?php

/**
 * 缓存驱动
 * @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);
    }
}
?>

发表评论:

TOP