tp5緩衝驅動Predis php沒有Redis驅動模組的時候使用Predis串連使用redis

來源:互聯網
上載者:User
這篇文章主要介紹了關於tp5緩衝驅動Predis php沒有Redis驅動模組的時候使用Predis串連使用redis ,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

沒有Redis驅動模組的時候使用Predis串連使用redis

安裝

composer require predis/predis

tp5 緩衝驅動增加Predis.php

<?phpnamespace think\cache\driver;use think\cache\Driver;/** * Predis緩衝驅動,就是php沒有redis擴充的時候使用,適合單機部署、有前端代理實現高可用的情境,效能最好 * 有需要在業務層實現讀寫分離、或者使用RedisCluster的需求,請使用Redisd驅動 * * 要求安裝Predis擴充:https://github.com/nrk/predis *  */class Predis extends Driver{    protected $options = [        'scheme'     => 'tcp',        'host'       => '127.0.0.1',        'port'       => 6379,        'password'   => '',        'select'     => 0,        'timeout'    => 0,        'expire'     => 0,        'persistent' => false,        'prefix'     => '',        'database'   => 15,    ];        /**     * 建構函式     * @param array $options 緩衝參數     * @access public     */    public function __construct($options = [])    {        if (!empty($options)) {                    $this->options = array_merge($this->options, $options);        }        $func          = $this->options['persistent'] ? 'pconnect' : 'connect';                $this->handler = new \Predis\Client;                $this->handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);                if ('' != $this->options['password']) {                    $this->handler->auth($this->options['password']);        }                if (0 != $this->options['select']) {                    $this->handler->select($this->options['select']);        }    }    /**     * 判斷緩衝     * @access public     * @param string $name 緩衝變數名     * @return bool     */    public function has($name)    {        return $this->handler->get($this->getCacheKey($name)) ? true : false;    }    /**     * 讀取緩衝     * @access public     * @param string $name 緩衝變數名     * @param mixed  $default 預設值     * @return mixed     */    public function get($name, $default = false)    {        $value = $this->handler->get($this->getCacheKey($name));                if (is_null($value)) {                    return $default;        }                $jsonData = json_decode($value, true);        // 檢測是否為JSON資料 true 返回JSON解析數組, false返回來源資料 byron sampson<xiaobo.sun@qq.com>        return (null === $jsonData) ? $value : $jsonData;    }    /**     * 寫入緩衝     * @access public     * @param string    $name 緩衝變數名     * @param mixed     $value  儲存資料     * @param integer   $expire  有效時間(秒)     * @return boolean     */    public function set($name, $value, $expire = null)    {        if (is_null($expire)) {                    $expire = $this->options['expire'];        }                if ($this->tag && !$this->has($name)) {                    $first = true;        }                $key = $this->getCacheKey($name);        //對數組/對象資料進行緩衝處理,保證資料完整性  byron sampson<xiaobo.sun@qq.com>        $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;                if (is_int($expire) && $expire) {                    $result = $this->handler->setex($key, $expire, $value);        } else {                    $result = $this->handler->set($key, $value);        }                isset($first) && $this->setTagItem($key);                return $result;    }    /**     * 自增緩衝(針對數值緩衝)     * @access public     * @param string    $name 緩衝變數名     * @param int       $step 步長     * @return false|int     */    public function inc($name, $step = 1)    {        $key = $this->getCacheKey($name);                return $this->handler->incrby($key, $step);    }    /**     * 自減緩衝(針對數值緩衝)     * @access public     * @param string    $name 緩衝變數名     * @param int       $step 步長     * @return false|int     */    public function dec($name, $step = 1)    {        $key = $this->getCacheKey($name);                return $this->handler->decrby($key, $step);    }    /**     * 刪除緩衝     * @access public     * @param string $name 緩衝變數名     * @return boolean     */    public function rm($name)    {        return $this->handler->delete($this->getCacheKey($name));    }    /**     * 清除緩衝     * @access public     * @param string $tag 標籤名     * @return boolean     */    public function clear($tag = null)    {        if ($tag) {            // 指定標籤清除            $keys = $this->getTagItem($tag);                        foreach ($keys as $key) {                            $this->handler->delete($key);            }                        $this->rm('tag_' . md5($tag));                        return true;        }                return $this->handler->flushDB();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.