<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think\cache\driver;

use think\cache\Driver;

/**
 * Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好
 * 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
 *
 * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
 * @author    尘缘 <130775@qq.com>
 */
class Redis extends Driver
{
    protected $options = [
        'host'       => '39.105.159.207',
        'port'       => 6379,
        'password'   => 'yuzhou',
        'select'     => 9,
        'timeout'    => 0,
        'expire'     => 0,
        'persistent' => false,
        'prefix'     => '',
        'serialize'  => true,
    ];
    /**
     * 架构函数
     * @access public
     * @param  array $options 缓存参数
     */
    public function __construct($options = [])
    {

        if (!empty($options)) {
            $this->options = array_merge($this->options, $options);
        }

        if (extension_loaded('redis')) {
            $this->handler = new \Redis;

            if ($this->options['persistent']) {
                $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
            } else {
                $this->handler->connect($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']);
            }
        } elseif (class_exists('\Predis\Client')) {
            $params = [];
            foreach ($this->options as $key => $val) {
                if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) {
                    $params[$key] = $val;
                    unset($this->options[$key]);
                }
            }

            if ('' == $this->options['password']) {
                unset($this->options['password']);
            }

            $this->handler = new \Predis\Client($this->options, $params);

            $this->options['prefix'] = '';
        } else {
            throw new \BadFunctionCallException('not support: redis');
        }
    }

    /**
     * 判断缓存
     * @access public
     * @param  string $name 缓存变量名
     * @return bool
     */
    public function has($name)
    {
        return $this->handler->exists($this->getCacheKey($name));
    }

    /**
     * 读取缓存
     * @access public
     * @param  string $name 缓存变量名
     * @param  mixed  $default 默认值
     * @return mixed
     */
    public function get($name, $default = false)
    {
        $this->readTimes++;

        $value = $this->handler->get($this->getCacheKey($name));

        if (is_null($value) || false === $value) {
            return $default;
        }

        return $this->unserialize($value);
    }

    /**
     * 写入缓存
     * @access public
     * @param  string            $name 缓存变量名
     * @param  mixed             $value  存储数据
     * @param  integer|\DateTime $expire  有效时间(秒)
     * @return boolean
     */
    public function set($name, $value, $expire = null)
    {
        $this->writeTimes++;

        if (is_null($expire)) {
            $expire = $this->options['expire'];
        }

        if ($this->tag && !$this->has($name)) {
            $first = true;
        }

        $key    = $this->getCacheKey($name);
        $expire = $this->getExpireTime($expire);

        $value = $this->serialize($value);

        if ($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)
    {
        $this->writeTimes++;

        $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)
    {
        $this->writeTimes++;

        $key = $this->getCacheKey($name);

        return $this->handler->decrby($key, $step);
    }

    /**
     * 删除缓存
     * @access public
     * @param  string $name 缓存变量名
     * @return boolean
     */
    public function rm($name)
    {
        $this->writeTimes++;

        return $this->handler->del($this->getCacheKey($name));
    }

    /**
     * 清除缓存
     * @access public
     * @param  string $tag 标签名
     * @return boolean
     */
    public function clear($tag = null)
    {
        if ($tag) {
            // 指定标签清除
            $keys = $this->getTagItem($tag);

            $this->handler->del($keys);

            $tagName = $this->getTagKey($tag);
            $this->handler->del($tagName);
            return true;
        }

        $this->writeTimes++;

        return $this->handler->flushDB();
    }

    /**
     * redis 哈希表(hash)类型
     * 批量填充HASH表。不是字符串类型的VALUE,自动转换成字符串类型。使用标准的值。NULL值将被储存为一个空的字符串。
     *
     * 可以批量添加更新 value,key 不存在将创建,存在则更新值
     *
     * @param  $key
     * @param  $fieldArr
     * @return
     * 如果命令执行成功,返回OK。
     * 当key不是哈希表(hash)类型时,返回一个错误。
     */
    public function hMSet($key,$fieldArr){

        return $this->handler->hmset($key,$fieldArr);
    }


    public function hGet( $key, $hashKeys ) {
        return $this->handler->hget($key,$hashKeys);
    }


    public function hMGet( $key, $hashKeys ) {
        return $this->handler->hMGet($key,$hashKeys);
    }

    public function hdel( $key, $hashKeys ) {
        return $this->handler->hdel($key,$hashKeys);
    }

    public function hkeys( $key ) {
        return $this->handler->hkeys($key);
    }

    /**
     * redis 哈希表(hash)类型
     * 返回哈希表 $key 中,所有的域和值。
     * @param $key
     *
     */
    public function hGetAll($key){

        return $this->handler->hGetAll($key);
    }

    /**
     * redis 获取哈希表中所有值。
     * 返回哈希表 $key 中,所有的域和值。
     * @param $key
     *
     */
    public function hGetvals($key){

        return $this->handler->hvals($key);
    }

    /**
     * redis 获取哈希表中字段的数量
     * 返回哈希表 $key 中,所有的域和值。
     * @param $key
     *
     */
    public function hGetLen($key){

        return $this->handler->hlen($key);
    }



    /**
     * 添加一个字符串值到LIST容器的顶部(左侧),如果KEY不存在,曾创建一个LIST容器,如果KEY存在并且不是一个LIST容器,那么返回FLASE。
     *
     * @param unknown $key
     * @param unknown $val
     */
    public function lPush($key,$val){


        return	$this->handler->lPush($key,$val);
    }

    /*
	 *将 key 中储存的数字值增一。
	 *如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。
	 */
    public function Incr($key){

        $this->handler->incr($key);
    }

    /*
    *将 key 中储存的数字值增加数量。
    *如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 incrby 操作。
    */
    public function Incrby($key,$number){

        $this->handler->incrby($key,$number);
    }
    /*
	 *将 key 中储存的数字值减一。
	 *如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。
	 */
    public function Decr($key){

        $this->handler->decr($key);
    }

    /*
    *将 key 中储存的数字值减少数量。
    *如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 Decrby 操作。
    */
    public function Decrby($key,$number){

        $this->handler->decrby($key,$number);
    }


    public function rPush($key,$field_arr){
        //	$keys =array($key,$vals);
        if(is_array($field_arr)){
            array_unshift($field_arr,$key);
            $keys = $field_arr;
        }else{
            $keys = array($key,$field_arr);
        }
        //	return	$this->redis->rPush($key,$val);
        return call_user_func_array(array($this->handler, "rPush"), $keys);
    }
    /**
     * 返回LIST顶部(左侧)的VALUE,并且从LIST中把该VALUE弹出。
     * @param unknown $key
     */
    public function lPop($key){

        return $this->handler->lPop($key);
    }

    public function rPop($key){
        return $this->handler->rPop($key);
    }

    public function lGetRange($key,$start,$end){
        return	$this->handler->lGetRange($key,$start,$end);
    }
    public function lRange($key,$start,$end){
        return	$this->handler->lRange($key,$start,$end);
    }

    public function Llen($key){
        return	$this->handler->llen($key);
    }

    public function Ltrim($key,$start,$end){
        return	$this->handler->ltrim($key,$start,$end);
    }


    /**
     * 加锁
     */
    public function Setnx($key,$exptime){
        return $this->handler->setnx($key,$exptime);
    }

    public function del($key){
        return $this->handler->del($key);
    }

    /**
     * 缓存标签
     * @access public
     * @param  string        $name 标签名
     * @param  string|array  $keys 缓存标识
     * @param  bool          $overlay 是否覆盖
     * @return $this
     */
    public function tag($name, $keys = null, $overlay = false)
    {
        if (is_null($keys)) {
            $this->tag = $name;
        } else {
            $tagName = $this->getTagKey($name);
            if ($overlay) {
                $this->handler->del($tagName);
            }

            foreach ($keys as $key) {
                $this->handler->sAdd($tagName, $key);
            }
        }

        return $this;
    }

    /**
     * 更新标签
     * @access protected
     * @param  string $name 缓存标识
     * @return void
     */
    protected function setTagItem($name)
    {
        if ($this->tag) {
            $tagName = $this->getTagKey($this->tag);
            $this->handler->sAdd($tagName, $name);
        }
    }

    /**
     * 获取标签包含的缓存标识
     * @access protected
     * @param  string $tag 缓存标签
     * @return array
     */
    protected function getTagItem($tag)
    {
        $tagName = $this->getTagKey($tag);
        return $this->handler->sMembers($tagName);
    }
}