redis php redis可视化客户端工具TreeNMS redis 命令参考 php-redis 操作类 封装 php-redis 操作类 封装:

redis  php
redis可视化客户端工具TreeNMS
redis 命令参考
php-redis 操作类 封装
php-redis 操作类 封装:

http://www.treesoft.cn/dms.html

redis界面管理工具phpRedisAdmin

https://github.com/erikdubbelboer/phpRedisAdmin

redis 命令参考

中文手册:http://redisdoc.com/

php扩展的检测:

$modules=get_loaded_extensions(); //返回所有编译并加载模块名
get_extension_funcs ( $modules['module_name'])// 返回模块函数名称的数组
extension_loaded($module_name)//检查一个扩展是否已经加载
function_exists() //检查某个函数是否存在

php-redis 操作类 封装

copy:https://www.cnblogs.com/YangJieCheng/p/7272054.html

<?php

/**
 * redis操作类
 * 说明,任何为false的串,存在redis中都是空串。
 * 只有在key不存在时,才会返回false。
 * 这点可用于防止缓存穿透
 *
 */
class Redis
{
    private $redis;
     
    //当前数据库ID号
    protected $dbId=0;
     
    //当前权限认证码
    protected $auth;
     
    /**
     * 实例化的对象,单例模式.
     * @var iphpdbRedis
     */
    static private $_instance=array();
     
    private  $k;
     
    //连接属性数组
    protected $attr=array(
        //连接超时时间,redis配置文件中默认为300秒
        'timeout'=>30,
        //选择的数据库。
        'db_id'=>0,
    );
     
    //什么时候重新建立连接
    protected $expireTime;
     
    protected $host;
     
    protected $port;
     
         
    private function __construct($config,$attr=array())
    {
        $this->attr        =    array_merge($this->attr,$attr);
        $this->redis    =    new Redis();
        $this->port        =    $config['port'] ? $config['port'] : 6379;
        $this->host        =    $config['host'];
        $this->redis->connect($this->host, $this->port, $this->attr['timeout']);
         
        if($config['auth'])
        {
            $this->auth($config['auth']);
            $this->auth    =    $config['auth'];
        }
         
        $this->expireTime    =    time() + $this->attr['timeout'];
    }
     
    /**
     * 得到实例化的对象.
     * 为每个数据库建立一个连接
     * 如果连接超时,将会重新建立一个连接
     * @param array $config
     * @param int $dbId
     * @return iphpdbRedis
     */
    public static function getInstance($config, $attr = array())
    {
        //如果是一个字符串,将其认为是数据库的ID号。以简化写法。
        if(!is_array($attr))
        {
            $dbId    =    $attr;
            $attr    =    array();
            $attr['db_id']    =    $dbId;
        }
         
        $attr['db_id']    =    $attr['db_id'] ? $attr['db_id'] : 0;
        
         
        $k    =    md5(implode('', $config).$attr['db_id']);
        if(! (static::$_instance[$k] instanceof self))
        {
           
            static::$_instance[$k] = new self($config,$attr);
            static::$_instance[$k]->k        =    $k;
            static::$_instance[$k]->dbId    =    $attr['db_id'];
             
            //如果不是0号库,选择一下数据库。
            if($attr['db_id'] != 0){
                static::$_instance[$k]->select($attr['db_id']);
            }
        }
        elseif( time() > static::$_instance[$k]->expireTime)
        {
            static::$_instance[$k]->close();
            static::$_instance[$k]         =     new self($config,$attr);
            static::$_instance[$k]->k    =    $k;
            static::$_instance[$k]->dbId=    $attr['db_id'];
              
            //如果不是0号库,选择一下数据库。
            if($attr['db_id']!=0){
                static::$_instance[$k]->select($attr['db_id']);
            }
        }
        return static::$_instance[$k];
    }
     
    private function __clone(){}
     
    /**
     * 执行原生的redis操作
     * @return Redis
     */
    public function getRedis()
    {
        return $this->redis;
    }
     
    /*****************hash表操作函数*******************/
     
    /**
     * 得到hash表中一个字段的值
     * @param string $key 缓存key
     * @param string  $field 字段
     * @return string|false
     */
    public function hGet($key,$field)
    {
        return $this->redis->hGet($key,$field);
    }
     
    /**
     * 为hash表设定一个字段的值
     * @param string $key 缓存key
     * @param string  $field 字段
     * @param string $value 值。
     * @return bool 
     */
    public function hSet($key,$field,$value)
    {
        return $this->redis->hSet($key,$field,$value);
    }
     
    /**
     * 判断hash表中,指定field是不是存在
     * @param string $key 缓存key
     * @param string  $field 字段
     * @return bool
     */
    public function hExists($key,$field)
    {
        return $this->redis->hExists($key,$field);
    }
     
    /**
     * 删除hash表中指定字段 ,支持批量删除
     * @param string $key 缓存key
     * @param string  $field 字段
     * @return int
     */
    public function hdel($key,$field)
    {
        $fieldArr=explode(',',$field);
        $delNum=0;
 
        foreach($fieldArr as $row)
        {
            $row=trim($row);
            $delNum+=$this->redis->hDel($key,$row);
        }
 
        return $delNum;
    }
     
    /**
     * 返回hash表元素个数
     * @param string $key 缓存key
     * @return int|bool
     */
    public function hLen($key)
    {
        return $this->redis->hLen($key);
    }
     
    /**
     * 为hash表设定一个字段的值,如果字段存在,返回false
     * @param string $key 缓存key
     * @param string  $field 字段
     * @param string $value 值。
     * @return bool
     */
    public function hSetNx($key,$field,$value)
    {
        return $this->redis->hSetNx($key,$field,$value);
    }
     
    /**
     * 为hash表多个字段设定值。
     * @param string $key
     * @param array $value
     * @return array|bool
     */
    public function hMset($key,$value)
    {
        if(!is_array($value))
            return false;
        return $this->redis->hMset($key,$value); 
    }
     
    /**
     * 为hash表多个字段设定值。
     * @param string $key
     * @param array|string $value string以','号分隔字段
     * @return array|bool
     */
    public function hMget($key,$field)
    {
        if(!is_array($field))
            $field=explode(',', $field);
        return $this->redis->hMget($key,$field);
    }
     
    /**
     * 为hash表设这累加,可以负数
     * @param string $key
     * @param int $field
     * @param string $value
     * @return bool
     */
    public function hIncrBy($key,$field,$value)
    {
        $value=intval($value);
        return $this->redis->hIncrBy($key,$field,$value);
    }
     
    /**
     * 返回所有hash表的所有字段
     * @param string $key
     * @return array|bool
     */
    public function hKeys($key)
    {
        return $this->redis->hKeys($key);
    }
     
    /**
     * 返回所有hash表的字段值,为一个索引数组
     * @param string $key
     * @return array|bool
     */
    public function hVals($key)
    {
        return $this->redis->hVals($key);
    }
     
    /**
     * 返回所有hash表的字段值,为一个关联数组
     * @param string $key
     * @return array|bool
     */
    public function hGetAll($key)
    {
        return $this->redis->hGetAll($key);
    }
     
    /*********************有序集合操作*********************/
     
    /**
     * 给当前集合添加一个元素
     * 如果value已经存在,会更新order的值。
     * @param string $key
     * @param string $order 序号
     * @param string $value 值
     * @return bool
     */
    public function zAdd($key,$order,$value)
    {
        return $this->redis->zAdd($key,$order,$value);   
    }
     
    /**
     * 给$value成员的order值,增加$num,可以为负数
     * @param string $key
     * @param string $num 序号
     * @param string $value 值
     * @return 返回新的order
     */
    public function zinCry($key,$num,$value)
    {
        return $this->redis->zinCry($key,$num,$value);
    }
     
    /**
     * 删除值为value的元素
     * @param string $key
     * @param stirng $value
     * @return bool
     */
    public function zRem($key,$value)
    {
        return $this->redis->zRem($key,$value);
    }
     
    /**
     * 集合以order递增排列后,0表示第一个元素,-1表示最后一个元素
     * @param string $key
     * @param int $start
     * @param int $end
     * @return array|bool
     */
    public function zRange($key,$start,$end)
    {
        return $this->redis->zRange($key,$start,$end);
    }
     
    /**
     * 集合以order递减排列后,0表示第一个元素,-1表示最后一个元素
     * @param string $key
     * @param int $start
     * @param int $end
     * @return array|bool
     */
    public function zRevRange($key,$start,$end)
    {
        return $this->redis->zRevRange($key,$start,$end);
    }
     
    /**
     * 集合以order递增排列后,返回指定order之间的元素。
     * min和max可以是-inf和+inf 表示最大值,最小值
     * @param string $key
     * @param int $start
     * @param int $end
     * @package array $option 参数
     *     withscores=>true,表示数组下标为Order值,默认返回索引数组
     *     limit=>array(0,1) 表示从0开始,取一条记录。
     * @return array|bool
     */
    public function zRangeByScore($key,$start='-inf',$end="+inf",$option=array())
    {
        return $this->redis->zRangeByScore($key,$start,$end,$option);
    }
     
    /**
     * 集合以order递减排列后,返回指定order之间的元素。
     * min和max可以是-inf和+inf 表示最大值,最小值
     * @param string $key
     * @param int $start
     * @param int $end
     * @package array $option 参数
     *     withscores=>true,表示数组下标为Order值,默认返回索引数组
     *     limit=>array(0,1) 表示从0开始,取一条记录。
     * @return array|bool
     */
    public function zRevRangeByScore($key,$start='-inf',$end="+inf",$option=array())
    {
        return $this->redis->zRevRangeByScore($key,$start,$end,$option);
    }
     
    /**
     * 返回order值在start end之间的数量
     * @param unknown $key
     * @param unknown $start
     * @param unknown $end
     */
    public function zCount($key,$start,$end)
    {
        return $this->redis->zCount($key,$start,$end);
    }
     
    /**
     * 返回值为value的order值
     * @param unknown $key
     * @param unknown $value
     */
    public function zScore($key,$value)
    {
        return $this->redis->zScore($key,$value);
    }
     
    /**
     * 返回集合以score递增加排序后,指定成员的排序号,从0开始。
     * @param unknown $key
     * @param unknown $value
     */
    public function zRank($key,$value)
    {
        return $this->redis->zRank($key,$value);
    }
     
    /**
     * 返回集合以score递增加排序后,指定成员的排序号,从0开始。
     * @param unknown $key
     * @param unknown $value
     */
    public function zRevRank($key,$value)
    {
        return $this->redis->zRevRank($key,$value);
    }
     
    /**
     * 删除集合中,score值在start end之间的元素 包括start end
     * min和max可以是-inf和+inf 表示最大值,最小值
     * @param unknown $key
     * @param unknown $start
     * @param unknown $end
     * @return 删除成员的数量。
     */
    public function zRemRangeByScore($key,$start,$end)
    {
        return $this->redis->zRemRangeByScore($key,$start,$end);
    }
     
    /**
     * 返回集合元素个数。
     * @param unknown $key
     */
    public function zCard($key)
    {
        return $this->redis->zCard($key);
    }
    /*********************队列操作命令************************/
     
    /**
     * 在队列尾部插入一个元素
     * @param unknown $key
     * @param unknown $value
     * 返回队列长度
     */
    public function rPush($key,$value)
    {
        return $this->redis->rPush($key,$value); 
    }
     
    /**
     * 在队列尾部插入一个元素 如果key不存在,什么也不做
     * @param unknown $key
     * @param unknown $value
     * 返回队列长度
     */
    public function rPushx($key,$value)
    {
        return $this->redis->rPushx($key,$value);
    }
     
    /**
     * 在队列头部插入一个元素
     * @param unknown $key
     * @param unknown $value
     * 返回队列长度
     */
    public function lPush($key,$value)
    {
        return $this->redis->lPush($key,$value);
    }
     
    /**
     * 在队列头插入一个元素 如果key不存在,什么也不做
     * @param unknown $key
     * @param unknown $value
     * 返回队列长度
     */
    public function lPushx($key,$value)
    {
        return $this->redis->lPushx($key,$value);
    }
     
    /**
     * 返回队列长度
     * @param unknown $key
     */
    public function lLen($key)
    {
        return $this->redis->lLen($key); 
    }
     
    /**
     * 返回队列指定区间的元素
     * @param unknown $key
     * @param unknown $start
     * @param unknown $end
     */
    public function lRange($key,$start,$end)
    {
        return $this->redis->lrange($key,$start,$end);
    }
     
    /**
     * 返回队列中指定索引的元素
     * @param unknown $key
     * @param unknown $index
     */
    public function lIndex($key,$index)
    {
        return $this->redis->lIndex($key,$index);
    }
     
    /**
     * 设定队列中指定index的值。
     * @param unknown $key
     * @param unknown $index
     * @param unknown $value
     */
    public function lSet($key,$index,$value)
    {
        return $this->redis->lSet($key,$index,$value);
    }
     
    /**
     * 删除值为vaule的count个元素
     * PHP-REDIS扩展的数据顺序与命令的顺序不太一样,不知道是不是bug
     * count>0 从尾部开始
     *  >0 从头部开始
     *  =0 删除全部
     * @param unknown $key
     * @param unknown $count
     * @param unknown $value
     */
    public function lRem($key,$count,$value)
    {
        return $this->redis->lRem($key,$value,$count);
    }
     
    /**
     * 删除并返回队列中的头元素。
     * @param unknown $key
     */
    public function lPop($key)
    {
        return $this->redis->lPop($key);
    }
     
    /**
     * 删除并返回队列中的尾元素
     * @param unknown $key
     */
    public function rPop($key)
    {
        return $this->redis->rPop($key);
    }
     
    /*************redis字符串操作命令*****************/
     
    /**
     * 设置一个key
     * @param unknown $key
     * @param unknown $value
     */
    public function set($key,$value)
    {
        return $this->redis->set($key,$value);
    }
     
    /**
     * 得到一个key
     * @param unknown $key
     */
    public function get($key)
    {
        return $this->redis->get($key);
    }
     
    /**
     * 设置一个有过期时间的key
     * @param unknown $key
     * @param unknown $expire
     * @param unknown $value
     */
    public function setex($key,$expire,$value)
    {
        return $this->redis->setex($key,$expire,$value);
    }
     
     
    /**
     * 设置一个key,如果key存在,不做任何操作.
     * @param unknown $key
     * @param unknown $value
     */
    public function setnx($key,$value)
    {
        return $this->redis->setnx($key,$value);
    }
     
    /**
     * 批量设置key
     * @param unknown $arr
     */
    public function mset($arr)
    {
        return $this->redis->mset($arr);
    }
     
    /*************redis 无序集合操作命令*****************/
     
    /**
     * 返回集合中所有元素
     * @param unknown $key
     */
    public function sMembers($key)
    {
        return $this->redis->sMembers($key);
    }
     
    /**
     * 求2个集合的差集
     * @param unknown $key1
     * @param unknown $key2
     */
    public function sDiff($key1,$key2)
    {
        return $this->redis->sDiff($key1,$key2);
    }
     
    /**
     * 添加集合。由于版本问题,扩展不支持批量添加。这里做了封装
     * @param unknown $key
     * @param string|array $value
     */
    public function sAdd($key,$value)
    {
        if(!is_array($value))
            $arr=array($value);
        else
            $arr=$value;
        foreach($arr as $row)
            $this->redis->sAdd($key,$row);
    }
     
    /**
     * 返回无序集合的元素个数
     * @param unknown $key
     */
    public function scard($key)
    {
        return $this->redis->scard($key);
    }
     
    /**
     * 从集合中删除一个元素
     * @param unknown $key
     * @param unknown $value
     */
    public function srem($key,$value)
    {
        return $this->redis->srem($key,$value);
    }
     
    /*************redis管理操作命令*****************/
     
    /**
     * 选择数据库
     * @param int $dbId 数据库ID号
     * @return bool
     */
    public function select($dbId)
    {
        $this->dbId=$dbId;
        return $this->redis->select($dbId);
    }
     
    /**
     * 清空当前数据库
     * @return bool
     */
    public function flushDB()
    {
        return $this->redis->flushDB();
    }
     
    /**
     * 返回当前库状态
     * @return array
     */
    public function info()
    {
        return $this->redis->info();
    }
     
    /**
     * 同步保存数据到磁盘
     */
    public function save()
    {
        return $this->redis->save();
    }
     
    /**
     * 异步保存数据到磁盘
     */
    public function bgSave()
    {
        return $this->redis->bgSave();
    }
     
    /**
     * 返回最后保存到磁盘的时间
     */
    public function lastSave()
    {
        return $this->redis->lastSave();
    }
     
    /**
     * 返回key,支持*多个字符,?一个字符
     * 只有* 表示全部
     * @param string $key
     * @return array
     */
    public function keys($key)
    {
        return $this->redis->keys($key);
    }
     
    /**
     * 删除指定key
     * @param unknown $key
     */
    public function del($key)
    {
        return $this->redis->del($key);
    }
     
    /**
     * 判断一个key值是不是存在
     * @param unknown $key
     */
    public function exists($key)
    {
        return $this->redis->exists($key);
    }
     
    /**
     * 为一个key设定过期时间 单位为秒
     * @param unknown $key
     * @param unknown $expire
     */
    public function expire($key,$expire)
    {
        return $this->redis->expire($key,$expire);
    }
     
    /**
     * 返回一个key还有多久过期,单位秒
     * @param unknown $key
     */
    public function ttl($key)
    {
        return $this->redis->ttl($key);
    }
     
    /**
     * 设定一个key什么时候过期,time为一个时间戳
     * @param unknown $key
     * @param unknown $time
     */
    public function exprieAt($key,$time)
    {
        return $this->redis->expireAt($key,$time);
    }
     
    /**
     * 关闭服务器链接
     */
    public function close()
    {
        return $this->redis->close();
    }
     
    /**
     * 关闭所有连接
     */
    public static function closeAll()
    {
        foreach(static::$_instance as $o)
        {
            if($o instanceof self)
                $o->close();
        }
    }
     
    /** 这里不关闭连接,因为session写入会在所有对象销毁之后。
    public function __destruct()
    {
        return $this->redis->close();
    }
    **/
    /**
     * 返回当前数据库key数量
     */
    public function dbSize()
    {
        return $this->redis->dbSize();
    }
     
    /**
     * 返回一个随机key
     */
    public function randomKey()
    {
        return $this->redis->randomKey();
    }
     
    /**
     * 得到当前数据库ID
     * @return int
     */
    public function getDbId()
    {
        return $this->dbId;
    }
     
    /**
     * 返回当前密码
     */
    public function getAuth()
    {
        return $this->auth;
    }
     
    public function getHost()
    {
        return $this->host;
    }
     
    public function getPort()
    {
        return $this->port;
    }
     
    public function getConnInfo()
    {
        return array(
            'host'=>$this->host,
            'port'=>$this->port,
            'auth'=>$this->auth
        );
    }
    /*********************事务的相关方法************************/
     
    /**
     * 监控key,就是一个或多个key添加一个乐观锁
     * 在此期间如果key的值如果发生的改变,刚不能为key设定值
     * 可以重新取得Key的值。
     * @param unknown $key
     */
    public function watch($key)
    {
        return $this->redis->watch($key);
    }
     
    /**
     * 取消当前链接对所有key的watch
     *  EXEC 命令或 DISCARD 命令先被执行了的话,那么就不需要再执行 UNWATCH 了
     */
    public function unwatch()
    {
        return $this->redis->unwatch();
    }
     
    /**
     * 开启一个事务
     * 事务的调用有两种模式Redis::MULTI和Redis::PIPELINE,
     * 默认是Redis::MULTI模式,
     * Redis::PIPELINE管道模式速度更快,但没有任何保证原子性有可能造成数据的丢失
     */
    public function multi($type=Redis::MULTI)
    {
        return $this->redis->multi($type);
    }
     
    /**
     * 执行一个事务
     * 收到 EXEC 命令后进入事务执行,事务中任意命令执行失败,其余的命令依然被执行
     */
    public function exec()
    {
        return $this->redis->exec();
    }
     
    /**
     * 回滚一个事务
     */
    public function discard()
    {
        return $this->redis->discard();
    }
     
    /**
     * 测试当前链接是不是已经失效
     * 没有失效返回+PONG
     * 失效返回false
     */
    public function ping()
    {
        return $this->redis->ping();
    }
     
    public function auth($auth)
    {
        return $this->redis->auth($auth);
    }
    /*********************自定义的方法,用于简化操作************************/
     
    /**
     * 得到一组的ID号
     * @param unknown $prefix
     * @param unknown $ids
     */
    public function hashAll($prefix,$ids)
    {
        if($ids==false)
            return false;
        if(is_string($ids))
            $ids=explode(',', $ids);
        $arr=array();
        foreach($ids as $id)
        {
            $key=$prefix.'.'.$id;
            $res=$this->hGetAll($key);
            if($res!=false)
                $arr[]=$res;
        }
         
        return $arr;
    }
     
    /**
     * 生成一条消息,放在redis数据库中。使用0号库。
     * @param string|array $msg
     */
    public function pushMessage($lkey,$msg)
    {
        if(is_array($msg)){
            $msg    =    json_encode($msg);
        }
        $key    =    md5($msg);
         
        //如果消息已经存在,删除旧消息,已当前消息为准
        //echo $n=$this->lRem($lkey, 0, $key)."
";
        //重新设置新消息
        $this->lPush($lkey, $key);
        $this->setex($key, 3600, $msg);
        return $key;
    }
     
     
    /**
     * 得到条批量删除key的命令
     * @param unknown $keys
     * @param unknown $dbId
     */
    public function delKeys($keys,$dbId)
    {
        $redisInfo=$this->getConnInfo();
        $cmdArr=array(
            'redis-cli',
            '-a',
            $redisInfo['auth'],
            '-h',
            $redisInfo['host'],
            '-p',
            $redisInfo['port'],
            '-n',
            $dbId,
        );
        $redisStr=implode(' ', $cmdArr);
        $cmd="{$redisStr} KEYS "{$keys}" | xargs {$redisStr} del";
        return $cmd;
    }
}

php-redis 操作类 封装

redis 操作类,包括单台或多台、多组redis服务器操作,适用于业务复杂、高性能要求的 php web 应用。

redis.php

 

<?php
 /*
   redis 操作类,适用于单台或多台、多组redis服务器操作

   使用方法:
   1、$rs=new mz_redis();$rs->load_config_file('redis_config1.php');$www=$rs->connect(1,true,0)==单台读连接,连接read_array第一个元素对应的redis服务器中的随机一台;$rs->get($www[0],'abc'),获取$www连接对象里的'abc'key的值。
   2、$rs=new mz_redis();$rs->load_config_file('redis_config2.php');$www=$rs->connect(1,true,1)==单台读连接,连接read_array第二个元素对应的redis服务器中的随机一台
   3、$rs=new mz_redis();$rs->load_config_file('redis_config3.php');$www=$rs->connect(1,false,0)==多台读连接,连接read_array每一个元素对应的redis服务器中的随机一台;数组形式的连接对象$www,需要循环去操作,与第一种方式有所区别
   4、$rs=new mz_redis();$rs->load_config_file('redis_config4.php');$www=$rs->connect(2,false,0)==多台写连接,连接write_array每一个元素对应的redis服务器
   5、$rs=new mz_redis();$rs->load_config_file('redis_config5.php');$www=$rs->connect(2,true,0)==单台写连接,连接write_array第一个元素对应的redis服务器
   注意:$www是数组,redis有很多操作方法,本类并未完全包括,简单的都可以自己扩展,这个类主要“单台或多台、多组redis服务器连接操作”
   有问题联系 QQ 8704953 。
 */

class pub_redis{

    private $read_link=array();   // 一维数组读资源
    private $write_link=array();  // 一维数组写资源

    private $read_array=array();  // 二维数组
    private $write_array=array(); // 二维数组

    /*
     * 构造函数
    */
    public function __construct(){

        if (!extension_loaded('redis')) {
            exit('服务器不支持redis扩展');
        }

    }

    /*
     * 初始化 redis 读写配置数组,都是二维数组
     * 不能业务类型的redis应用,配置到不同的文件中
     * 切换不同业务类型redis的连接,只需要执行本方法导入不同的redis配置文件,然后connect()
    */
    public function load_config_file($redis_config_file='redis_config1.php'){

        require_once($redis_config_file);
        $this->read_array=$read_array;
        $this->write_array=$write_array;
        $read_array=$write_array=null;

    }

    /*
     * 连接函数,redis链接入口
     * $single==true,单台操作 ; false就是多台操作
     * type==1:read ; type==2:write
     * $index,单台操作,指定操作某一台,数组的索引
     * 返回redis链接资源,一维数组形式,下标为从0开始的数字
    */
    public function connect($type=1,$single=true,$index=0){ 

        if($type==1){
            if($single){
                $idx=array_rand($this->read_array[$index]);
                $data=array(array($this->read_array[$index][$idx]));
            }
            else{
                $data=array();
                foreach($this->read_array as $key=>$val){
                    $idx=array_rand($val);
                    $data[]=array($this->read_array[$key][$idx]);
                }
            }
            $this->mz_connect($data,$this->read_link,$single,$index);
            $rs=$this->read_link;
        }
        else if($type==2){
            $this->mz_connect($this->write_array,$this->write_link,$single,$index);
            $rs=$this->write_link;
        }
        else{
            exit('参数错误');
        }

        sort($rs);
        return $rs;

    }

    /*
     * 连接资源数组化
    */
    public function mz_connect($array,&$link,$single,$index){ 
        if($single){
            if(!isset($link[$array[$index]['ip']]) || $link[$array[$index]['ip']]===false){
                $link[$array[$index]['ip']]=$this->do_connect($array[$index]['ip'],$array[$index]['pwd'],$array[$index]['port'],$array[$index]['time_out'],$array[$index]['db']);
            }
        }
        else{
            $num=count($array);
            for($i=0;$i<$num;++$i){
                $index=array_rand($array);
                if(!isset($link[$array[$index]['ip']]) || $link[$array[$index]['ip']]===false){
                    $link[$array[$index]['ip']]=$this->do_connect($array[$index]['ip'],$array[$index]['pwd'],$array[$index]['port'],$array[$index]['time_out'],$array[$index]['db']);
                }
                unset($array[$index]);
            }
        }
    }

    /*
     * 连接函数,执行连接
     * 连接redis与选择数据库,并确认是否可以正常连接,连接不上就返回false
    */
    public function do_connect($ip,$pwd='',$port=6379,$time_out=0.3,$db=1){

        $redis = new Redis();
        try {
            $redis->connect($ip,$port,$time_out); 
            if($pwd!=''){
                $redis->auth($pwd);
            }
            $redis->select($db);

        } catch (Exception $e) {
            $redis=false;
        }
        return $redis;
    }

    /*
     * 判断key是否存在
     * $obj redis连接对象
    */
    public function key_exists($obj,$key){
        return $obj->exists($key);
    }

    /*
     * 判断key剩余有效时间,单位秒
     * $obj redis连接对象
    */
    public function get_ttl($obj,$key){
        return $obj->ttl($key);
    }

    /*
     * 获取字符串对象
     * $obj redis连接对象
    */
    public function get($obj,$key){
        return json_decode($obj->get($key));
    }

    /*
     * 设置字符串,带生存时间
     * $obj redis连接对象
    */
    public function set($obj,$key,$time,$value){
        $str=json_encode($value);
        return $obj->setex($key,$time,$str);
    }

    /*
     * 设置锁
     * $obj redis连接对象
     * $str, 字符串
    */
    public function set_lock($obj,$key,$value){
        return $obj->setnx($key,$value);
    }

    /*
     * 删除key
     * $obj redis连接对象
    */
    public function delete_key($obj,$key){
        return $obj->delete($key);
    }

    /*
     * 链表增加多个元素
     * $obj redis连接对象
    */
    public function list_add_element($obj,$key,$array,$direction='left'){
        if(!is_array($array)){
            $array=array($array);
        }
        foreach($array as $val){
            ($direction == 'left') ? $obj->lPush($key, json_encode($val)) : $obj->rPush($key, json_encode($val));
        }
    }

    /*
     * 链表弹出多个元素
     * $obj redis连接对象
     * 返回数组
    */
    public function list_pop_element($obj,$key,$num=1,$direction='right') {
        for($i=0;$i<$num;$i++){
           $value = ($direction == 'right') ? $obj->rPop($key) : $obj->lPop($key);
           $data[]=json_decode($value);
        }
        return $data;
    }

    /*
     * 哈希表新增或修改元素
     * $obj redis连接对象
     * $array 关联数组
    */
    public function hash_set($obj,$key,$array){
        if(!$is_array($array)){
            exit('设置哈希表参数异常');
        }
        $obj->hmset($key,$array);
    }

    /*
     * 哈希表读取元素
     * $obj redis连接对象
     * $array 关联数组
    */
    public function hash_get($obj,$key,$array){
        if(!$is_array($array)){
            return $obj->hget($key,$array);
        }
        return $obj->hmget($key,$array);
    }

}

?>

redis_config1.php

<?php
/*
 * 读写redis配置
 * 读写数组下标相同,为主从关系
 */
$write_array=array(
    0=>array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1),
    1=>array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1)
);

$read_array=array(
    0=>array(
        array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'654321','db'=>1),
        array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1)
    ),
    1=>array(
        array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'5678765','db'=>1),
        array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'345678','db'=>1)
    )
);

?>

redis_config2.php

<?php
/*
 * 读写redis配置
 * 读写数组下标相同,为主从关系
 */
$write_array=array(
    0=>array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1),
    1=>array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1)
);

$read_array=array(
    0=>array(
        array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'654321','db'=>1),
        array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'123456','db'=>1)
    ),
    1=>array(
        array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'5678765','db'=>1),
        array('ip'=>'172.16.10.23','port'=>6379,'time_out'=>0.3,'pwd'=>'345678','db'=>1)
    )
);

?>

这篇主要讲如何将数据保存回Mysql,但是里面还会涉及到如何将错误信息以及提示信息保存到文件里,方便以后的运维,再有就是如何使用PHP写进程BAT。

Redis数据刷回数据库前的知识准备

  首先针对上篇提到的关于redis刷回数据库的安全性的设计模式,因为我们使用的是list来做数据索引,所以在我们将list数据提取出来的时候,一旦redis在这时候出现异常,就会导致刚提取出来的数据丢失!有些小伙伴就说,丢失就丢失呗,才一点数据。但是我们做程序,就应该以严谨为基础,所以下面就来说下Redis List这位大佬给我们提供了什么帮助。

  •   Redis List -》RpopLpush()函数
  •        使用方法:RPOPLPUSH source destination
  •        说明:命令RPOPLPUSH在一个原子时间内,执行以下两个动作:①命令RPOPLPUSH在一个原子时间内,执行以下两个动作;②将source弹出的元素插入到列表destination,作为destination列表的的头元素。
  •        设计模式:

      Redis的列表经常被用作队列(queue),用于在不同程序之间有序地交换消息(message)。一个程序(称之为生产者,producer)通过LPUSH命令将消息放入队列中,而另一个程序(称之为消费者,consumer)通过RPOP命令取出队列中等待时间最长的消息。

    不幸的是,在这个过程中,一个消费者可能在获得一个消息之后崩溃,而未执行完成的消息也因此丢失。

    使用RPOPLPUSH命令可以解决这个问题,因为它在返回一个消息之余,还将该消息添加到另一个列表当中,另外的这个列表可以用作消息的备份表:假如一切正常,当消费者完成该消息的处理之后,可以用LREM命令将该消息从备份表删除。

Redis数据刷回数据库

方面文字太多?没关系。下面先来一段代码!我们的主体部分:

index.php:

 1 <?php
 2 require_once(__DIR__."/Mysql.class.php");
 3 require_once(__DIR__."/Redis.class.php");
 4 require_once(__DIR__."/Output_Log.class.php");
 5 
 6 
 7 $rel = true;        //无限循环的变量
 8 $num = 0;            //用来没数据时的判断依据
 9 date_default_timezone_set("Asia/Shanghai"); 
10 $now = date("Y-m-d H:i:s");        //当前时间
11 //file log
12 $txt = dirname(__DIR__)."/Script_Log/clickgood_log.txt";
13 $output = new OutputLog();
14 $test = $output->open($txt,"a+");
15 
16 while($rel)
17 {
18     $redis = new RedisCtrl();
19 
20     //开始干活
21     if($num==0){
22         //这里就是将信息输出到文件里记录,下面很多地方都是一样的。
23         $text = "start ".$name."
";
24         echo $text;
25         $output->write($test,$text);
26     }
27 
28     //获取备份队列的长度
29     $copylistlength = $redis->llen("comment:uploadcopylist"); 
30 
31     //我这里展示的是第一数据回滚到mysql,小伙伴想批量回滚的,自己改装下就可以用了。
32     //自己动手丰衣足食!
33     if($copylistlength>1)
34     {
35         //由于是单一数据回滚,所以我要判断它是否超过我设定的值,小伙伴们最好也自己定一个阈值。
36         //report error
37         echo $now." ->false
";
38         $rel = false;
39         return;
40     }
41     else if($copylistlength==1)
42     {
43         //这里判断防止上次redis出现错误,导致数据没有及时回到mysql
44         $data = $redis->rpop("comment:uploadcopylist");
45         $rel = $redis->UpdateClickGoodDataToMysql($data);
46         $text = $rel."
";
47         echo $text;
48         $output->write($test,$text);
49     }
50     else
51     {
52         //获取主要队列的长度
53         $listlength = $redis->llen("comment:uploadlist");
54         if ($listlength>0) {
55             //使用之前说到的设计模式
56             $data = $redis->rpoplpush("comment:uploadlist","comment:uploadcopylist");
57 
58             $rel = $redis->UpdateClickGoodDataToMysql($data);
59             $text = $rel."
";
60             echo $text;
61             $output->write($test,$text);
62         }else{
63             // 队列为空
64             // 打印关闭信息,这里的写法算是维持进程窗口不关闭,需要手动关闭
65             // 如果想让它执行完自动关闭的,
66             // 把下面改写成$rel = false;
67             if($num<=3){
68                 $text = $now." -> please close .
";
69                 echo $text;
70                 $output->write($test,$text);
71                 $num++;
72             }
73             else
74             {
75                 $output->close($test);
76             }
77         }
78     }
79 
80 }

Redis.class.php:  redis操作类

 1 <?php
 2 class RedisCtrl
 3 {
 4     //init redis
 5     static $redisIp = "127.0.0.1";
 6     static $redisPort =6379;
 7     static $redisPass ="";
 8     public $redis = null;
 9 
10     //Redis
11     public function __construct()
12     {
13         $this->redis = new Redis();
14         $this->redis->connect(self::$redisIp,self::$redisPort);
15         $this->redis->auth(self::$redisPass);
16     }
17 
18     public function llen($key)
19     {
20         $rel = $this->redis->llen($key);
21         return $rel;
22     }
23 
24     public function rpop($key)
25     {
26         $rel = $this->redis->rpop($key);
27         return $rel;
28     }
29 
30     public function rpoplpush($source,$destination)
31     {
32         $rel = $this->redis->rpoplpush($source,$destination);
33         return $rel;
34     }
35 
36     public function UpdateClickGoodDataToMysql($data)
37     {
38         //get id and time from redis list
39         $result = json_decode($data,true);
40         $id = $result['id'];
41         $time = $result['time'];
42         $arr = array();
43 
44         //like
45         $like = $this->redis->zscore("comment:like",$id);
46         $like = $like?$like:0;
47         //hate
48         $hate = $this->redis->zscore("comment:hate",$id);
49         $hate = $hate?$hate:0;
50 
51         $sql = "update comment_info set like_count=".$like.", hate_count=".$hate." where id=".$id;
52         $arr[] = $sql;
53         //update sql
54         $mysql = new MysqlCtrl();
55         $mysql->saveMySQL($arr);
56 
57         //更新完,将set集合里需要更新的id去掉
58         $this->redis->srem("comment:uploadset",$id);
59         //更新完毕,将备份队列里的数据去掉
60         $this->redis->lrem("comment:uploadcopylist",$data);
61 
62         return $sql."
";
63     }
64 }

Mysql.class.php  mysql类

 1 <?php
 2 //封装函数
 3 class MysqlCtrl
 4 {
 5     //初始化参数
 6     //数据库参数配置
 7     static $dbms = "mysql";
 8     static $host = Your host;
 9     static $user = Your user;
10     static $pass =  Your pass;
11     static $database = Your database;
12     //睡眠时间
13     static $sleep = 1;
14 
15     public $dsn = null;
16     public $dbh = null;
17 
18     public function __construct()
19     {
20         $this->dsn = self::$dbms.":host=".self::$host.";dbname=".self::$database;
21         //return $dsn;
22         try {
23               $this->dbh = new PDO($this->dsn, self::$user, self::$pass);
24               echo "Connected
";
25         } catch (Exception $e) {
26             echo $this->dsn;
27               die("Unable to connect: " . $e->getMessage());
28         }
29     }
30     
31     //保存数据到数据库
32     //PDO
33     public function saveMySQL($arr)
34     {
35 
36         try {  
37               $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
38 
39               $this->dbh->beginTransaction();
40 
41               $count = count($arr);
42               for ($i=0; $i < $count; $i++) { 
43                   $this->dbh->exec($arr[$i]);
44               }
45         
46               $this->dbh->commit();
47           
48         } catch (Exception $e) {
49               $this->dbh->rollBack();
50               echo "Failed: " . $e->getMessage()."
";
51               $json = json_encode($arr);
52               echo "False-SQL: ".$json."
";
53               exit();
54         }
55     }
56 }

Output_Log.class.php  输出信息到文件的类

 1 <?php
 2 class OutputLog
 3 {
 4     public function open($name,$r)
 5     {
 6         $text = fopen($name, $r);
 7         return $text;
 8     }
 9 
10     public function write($name,$title)
11     {
12         $rel = fwrite($name, $title);
13         return $rel;
14     }
15 
16     public function close($name)
17     {
18         fclose($name);
19     }
20 }

clickgood_log.txt  这里是保存输出的信息,里面是空白的。

hellO world

上面这些就是整套数据保存到mysql的操作,这是本人源码copy过来的,所以细分程度比较高,但是可扩展性也很高。有什么错误的地方希望小伙伴们能提出,谢谢。

最后就是我们的进程调用了,其实很简单

创建一个txt文件,然后改名为clickgood.bat,记得把txt后缀文件名改为bat

clickgood.bat:

 1 D:Softwarewamp64inphpphp7.0.10php.exe index.php 

关于上面的bat注意两点,一前面那个php.exe,按照你自己的路径去找,我使用wampserver测试环境,基本就是上面的路径。第二点,后面的index.php和clickgood.bat同一目录下。