Home  >  Article  >  Backend Development  >  When tp5 cache driver Predis php does not have a Redis driver module, use Predis to connect and use redis.

When tp5 cache driver Predis php does not have a Redis driver module, use Predis to connect and use redis.

不言
不言Original
2018-06-02 15:59:172988browse

This article mainly introduces the tp5 cache driver Predis PHP when there is no Redis driver module. Use Predis to connect and use redis. It has a certain reference value. Now I share it with you. Friends in need can refer to it

When there is no Redis driver module, use Predis to connect and use redis

Installation

composer require predis/predis

tp5 cache driver adds Predis.php

 '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
        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
        $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();
    }

}

Related recommendations:

PHP Method of request object in TP5 Request


The above is the detailed content of When tp5 cache driver Predis php does not have a Redis driver module, use Predis to connect and use redis.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn