<?php //auth: yaodunyuan //QQ:398123185 class RedisClass { public $redis; private $redis_domain; private $redis_point; private $redis_password; //实例化 function __construct(){ //实例化redis $this->redis = new Redis(); //获取域名 $this->domain = $_SERVER['HTTP_HOST']; $this->redis_domain ; //IP地址 $this->redis_point ; //端口 $this->redis_password ;//庙貌 if ($this->domain == '127.0.0.1') { $this->redis_domain = $this->domain; $this->redis_password = 123456; $this->redis_point = 6379; } else { //设置成服务器地址 } //redis 连接 $this->redis->connect($this->redis_domain,$this->redis_point); //使用密码 $this->redis->auth($this->redis_password); //选择库 $this->redis->select(1); } //返回redis对象 function return_redis():Redis{ return $this->redis; } } /** * 测试类 * 1.抽奖 * 2.队列 * 3.锁 * 4.微信access_token */ class Test{ //定义redis public $redis; //实例化 function __construct(){ $this->RedisCalss = new RedisClass(); $this->redis = $this->RedisCalss->return_redis(); } /** * 1抽奖,使用Key */ function luck_draw(){ //首先看看奖品池内,是否有奖品 if(!$this->redis->exists('luck')){ //没有奖品,添加奖品到redis内 $arrary = json_encode(['一等奖' ,'二等奖','二等奖','三等奖','三等奖','三等奖'],true); $this->redis->set('luck',$arrary); } //在随机取出奖品 $rand = random_int(1,7); //反序列化 $luck_arr = json_decode($this->redis->get('luck')); //取出中奖 $return_luck = $luck_arr[$rand]; //去除数组 unset($luck_arr[$rand]); //重新装入key钟 $this->redis->set('luck',json_encode($luck_arr,true)); return $return_luck; } /** * 2.队列 */ function queue(){ //放入队列 $arr = array('h','e','l','l','o','w','o','r','l','d'); foreach($arr as $k=>$v){ $this->redis->rpush('mylist',$v); } //获取尾部单个 //return $this->redis->rpop('mylist'); //获取头部单个 //return $this->redis->lpop('mylist'); //移除某个 //return $this->redis->lrem('mylist',1); //获取长度 //return $this->redis->llen('mylist'); //获取全部 return $this->redis->lrange('mylist',0,9); } /** * 锁 */ function lock(){ //设置redis锁 $randValue = rand(10000, 99999); $lockKey = 'lock_key'; //声明锁 $lock = $this->redis->set($lockKey, $randValue, ['NX', 'EX'=>1]); while ($lock) { //返回第一个物品 $luck = $this->redis->rpop('mylist'); //移除奖品 $this->redis->lrem('mylist', 1); return ['redis' => $this->redis,'lock' => $lock,'luck'=>$luck]; } } /** * 微信access_token */ function accesstoken($appid,$secret){ if (!$this->redis->exists('Wx_access_token')) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"; $res = json_decode(curl('GET', $url, []), true); //dump($res);die; if (isset($res['access_token'])) { $this->redis->set('Wx_access_token', $res['access_token'], ['ex'=> 7100]); return $this->redis ->get('Wx_access_token'); } else { json_encode([0, '获取ACCESS_TOKEN错误:'.$res['errcode'].",msg:".$res['errmsg']]); } } return $this->redis->get('Wx_access_token'); } } $class = new Test(); //1.抽奖 print_r($class->luck_draw()); //2.队列 print_r($class->queue()); //3锁 $arr = $class->lock(); //解除锁 $arr['redis']->del($arr['lock']); print_r($arr['luck']); //4微信accesstoken appid 和secret 纯属乱打 print_r($class->accesstoken('sdfssdfsldkfjsldkfs','sldkjflskdjflksjdlkf')); ?>