php和redis的交互
来源:互联网 发布:mysql安装版 64位 编辑:程序博客网 时间:2024/06/05 01:57
博客原文地址https://xgs888.top/post/view?id=62
在thinkphp5.0框架使用redis,redis支持五种数据类型 string hash list set zset
<?phpnamespace app\index\controller;use think\db;class Index{ /** * 模拟商品的总库存 */ private $goods_count=100; private $redis; public function __construct(){ $this->redis = $this->redisConnect(); } /** * redis链接 * @return \Redis */ public function redisConnect(){ $redis = new \Redis(); $redis->connect('127.0.0.1',6379); return $redis; } public function shop(){ $msg = []; $uid = mt_rand(1000,9999); $len = $this->redis->HLEN('goods_users'); // echo $len;exit; $goodsid = time(); if($len<=$this->goods_count){ // $this->redis->delete('goods_user_queue');exit; //判断用户是已经抢购了商品 if(!($this->redis->hExists('goods_users','user_'.$uid))){ $data= [ 'user_'.$uid =>$uid, 'goodsid' =>$goodsid ]; $this->redis->hset('goods_users','user_'.$uid,$uid); //如果没有抢到商品加入到队列 $this->redis->lpush('goods_user_queue',json_encode($data)); $msg['status']=1; $msg['msg'] = '恭喜你抢购成功'; }else{ $msg['status'] = 2; $msg['msg'] = 'sorry您已经购买过该商品了,只能参加一次'; } }else{ $msg['status'] = 3; $msg['msg'] = '活动已经结束'; } echo json_encode($msg); } //跑定时任务 public function insertdata(){ $len = $this->redis->lLen('goods_user_queue'); if($len){ //如果redis存在购买信息则减少库存 $datajson = $this->redis->lPop('goods_user_queue'); $data = json_decode($datajson,true); $where['id'] = $data['goodsid']; //库存-1 db('goods')->where($where)->update(['goodsnum'=>'goodsnum-1']); //写入订单表 db('order')->insert(['goodsid'=>$data['goodsid'],'uid'=>$data['userid']]); } } }
阅读全文
0 0
- php和redis的交互
- php和mysql的交互
- redis和 redis的php扩展
- [转载]redis和 redis的php扩展
- php和redis的故事
- 表单的建立和PHP的交互
- HTML 和 PHP 、MySQL 的交互
- android 和 php 简单的数据交互
- android 和 php 简单的数据交互
- PHP和Nginx的交互方式
- PHP和JS的数据交互
- PHP和Mysql简单的交互
- Unity和PHP的Http数据交互
- android 和 php 简单的数据交互
- unity3d 和 php交互
- redis数据库和python的交互(完整版)
- PHP中redis 的安装和使用
- php redis 和memcache 的扩展
- 如何使用gdb结合qemu调试linux内核源码
- java研发手机归属地批量查询
- extern C的作用详解
- Springboot 的内部工具类 SpringFactoriesLoader
- Android应用层View绘制流程与源码分析
- php和redis的交互
- oracle_day3_
- C# 反射通过类名实例化类
- Android 开发笔记:Android特效 五种Toast详解
- Ubuntu中root用户和user用户的相互切换
- 建了索引却不走索引案例分析
- Java 使用动态代理来动态修改注解值
- 欢迎使用CSDN-markdown编辑器
- 全局环境变量