<?php
namespace redis;
use \Redis as RedisBase;
class cls_redis
{
public static $redis = null;
private static $timeout = 600;
/**
* 初始化Redis连接
* 所有配置参数在实例化Redis类时加入参数即可
*/
public function __construct($db=0)
{
if(self::$redis==null){
self::$redis = new RedisBase();
self::$redis->connect('127.0.0.1',6379,self::$timeout) or die('Redis 连接失败!');
//if(!empty($_SERVER['RD_AUTH'])){
//self::$redis->auth($_SERVER['RD_AUTH']); //如果有设置密码,则需要连接密码
//}
}
$db=(int)$db;
if($db){
self::$redis->select($db); //选择缓存库
}
}
/**
* 开启事务
*/
public static function transation()
{
self::$redis->multi();
}
/**
* 提交事务
*/
public static function commit()
{
self::$redis->exec();
}
/**
* 取消事务
*/
public static function discard()
{
self::$redis->discard();
}
public static function myself()
{
return self::$redis;
}
//通过管道批量操作
public function lpush_arr($key,$arr=[])
{
if(count($arr)==0){return 0;}
$ple=self::$redis->multi();
foreach ($arr as $v){
$ple->lpush($key,$v);
}
$ple->exec();//执行事务
}
//队列右侧插入
public function rpush_arr($key,$arr=[])
{
if(count($arr)==0){return 0;}
$ple=self::$redis->multi();
foreach ($arr as $v){
$ple->rpush($key,$v);
}
$ple->exec();//执行事务
}
//无序集合
public function sadd_arr($key,$arr=[])
{
if(count($arr)==0){return 0;}
$ple=self::$redis->multi();
foreach ($arr as $v){
$ple->sadd($key,$v);
}
$ple->exec();//执行事务
}
//有序集合 $arr=['张三'=>80,'李四'=>90,'菠菜'=>100];
public function zadd_arr($key,$arr=[])
{
if(count($arr)==0){return 0;}
$ple=self::$redis->multi();
foreach ($arr as $k=>$v){
$ple->zadd($key,$v,$k);
}
$ple->exec();//执行事务
}
//多维数组设置
public function hset_Multi($key,$arr=[]){
if(count($arr)==0){return 0;}
$ple=self::$redis->multi();
foreach ($arr as $k=>$v){
if(is_array($v)){
$ple->hset($key.':',$k,$key.':'.$k);
$this->arr_set($ple,$key.':'.$k,$v);
}else{
$ple->hset($key.':',$k,$v);
}
}
$ple->exec();//执行事务
}
//多维数组递归设置
private function arr_set(&$ple,$key,$val){
if(count($val)==0){return 0;}
foreach ($val as $k=>$v){
if(is_array($v)){
$ple->hset($key,$k,$key.':'.$k);
$this->arr_set($ple,$key.':'.$k,$v);
}else{
$ple->hset($key,$k,$v);
}
}
}
//多维数组获取
public function hget_Multi($key){
$arr=self::$redis->hgetall($key.':');
$this->arr_get($key,$arr);
return $arr;
}
//多维数组递归获取
private function arr_get($key,&$arr){
if(count($arr)==0){return 0;}
$ple=self::$redis->multi();
$i=0;
$karr=[];
foreach ($arr as $k=>$v){
if($key.':'.$k==$v){
$ple->hgetall($v);
$karr[$i]=$k;
$i++;
}
}
if(count($karr)>0){
$res=$ple->exec();//执行事务
if(count($res)>0){
foreach($res as $k=>$v){
$this->arr_get($key.':'.$karr[$k],$v);
$arr[$karr[$k]]=$v;
}
}
}else{
$ple->discard();//取消事务
}
}
}
?>
<?php
define('API_PATH', str_replace('\\', '/',dirname(__FILE__)) . '/');
require API_PATH.'inc/cls_redis.php'; //引入cls_redis文件
$rd_obj = new \redis\cls_redis(1); //初始redis对象 并进入分片
$redis=$rd_obj::$redis; //传递静态变量里面的操作句柄
$key='dat';
$arr=['张三'=>80,'李四'=>['赵五','钱六',['周七','郑八','王九']],'菠菜'=>100,'武时'=>['钱六','赵五',['郑八','周七','王九']]];
$rd_obj->hset_Multi($key,$arr);
$res=$rd_obj->hget_Multi($key);
print_r($res);
exit();