• php 简单使用redis 队列示例


     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     }

     运行结果:

  • 相关阅读:
    server-conf-PPTConf
    client-autoReport-ppt
    client-autoReport-common
    浏览器书签导出
    微信公众号JSAPI支付-多公众号向同一商户号支付的问题解决
    Kettle 4.4.0 通过 Java 代码 输出日志到表
    spring tx:advice事务配置
    Spring异常捕获而且回滚事务的方法
    移动端服务器i-jetty下载编译安装及问题解决系列
    I-Jetty部署war包到安卓手机
  • 原文地址:https://www.cnblogs.com/wanglijun/p/8808733.html
Copyright © 2020-2023  润新知