tp5缓存驱动Predis php没有Redis驱动模块的时候使用Predis连接使用redis

不言
发布: 2018-06-02 15:59:17
原创
3256人浏览过

这篇文章主要介绍了关于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();
    }

}
登录后复制

相关推荐:

PHP 中TP5 Request 请求对象的方法

以上就是tp5缓存驱动Predis php没有Redis驱动模块的时候使用Predis连接使用redis 的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号