1 public function redisAction(){ 2 $redis = new Redis(); 3 $redis->connect('127.0.0.1', 6379); 4 echo "Connection to server sucessfully<br/>"; 5 //存储数据到列表中 6 $redis->lpush("tutorial-list", "Redis"); 7 $redis->lpush("tutorial-list", "Mongodb"); 8 $redis->lpush("tutorial-list", "Mysql"); 9 // 获取存储的数据并输出 10 $arList = $redis->lrange("tutorial-list", 0 ,5); 11 echo "Stored string in redis------<br/>"; 12 echo"压入队列:<br/>";var_export($arList); 13 echo "<br/>"; 14 // 获取列表长度 15 $llen = $redis->llen('tutorial-list'); 16 while ($llen){ 17 echo "弹出数据:".$redis->lpop('tutorial-list')."<br/>"; 18 $llen = $redis->llen('tutorial-list'); 19 } 20 // 获取存储的数据并输出 21 $arList = $redis->lrange("tutorial-list", 0 ,5); 22 echo "Stored string in redis------<br/>"; 23 var_export($arList); 24 } 25 //测试reds 26 public function testredisAction(){ 27 28 $store=10; 29 $redis=new Redis(); 30 $result=$redis->connect('127.0.0.1',6379); 31 $res=$redis->llen('goods_store'); 32 echo $res."<br>"; 33 $count=$store-$res; 34 for($i=0;$i<$count;$i++){ 35 $redis->lpush('goods_store',1); 36 } 37 echo $redis->llen('goods_store'); 38 39 } 40 public function buyAction(){ 41 //模拟下单操作 42 //下单前判断redis队列库存量 43 $redis=new Redis(); 44 $result=$redis->connect('127.0.0.1',6379); 45 $count=$redis->lpop('goods_store'); 46 echo $count."<br>"; 47 echo $redis->llen('goods_store')."<br>"; 48 49 if(!$count){ 50 echo('error:no store redis'); 51 }else{ 52 53 //生成订单 54 $order_sn=$this->build_order_no(); 55 echo $order_sn; 56 } 57 58 } 59 //生成唯一订单号 60 function build_order_no(){ 61 return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); 62 }
运行结果: