redis list 秒杀

<?php class Test { private static $instance = null; private static $conn=null; // 用单列模式 实例化Redis public static function Redis() { if (self::$instance == null) { $redis=new \Redis(); $redis->connect('127.0.0.1',6379); self::$instance = $redis; } return self::$instance; } public static function mysql($sql,$type) { $mysql_conf = array( 'host' => '127.0.0.1', 'db' => 'big', 'db_user' => 'root', 'db_pwd' => 'root', ); $mysqli = @new mysqli($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']); if ($mysqli->connect_errno) { die("could not connect to the database:\n" . $mysqli->connect_error);//诊断连接错误 } $mysqli->query("set names 'utf8';");//编码转化 $select_db = $mysqli->select_db($mysql_conf['db']); if (!$select_db) { die("could not connect to the db:\n" . $mysqli->error); } $res = $mysqli->query($sql); if($type==1){ $res = $res->fetch_assoc(); } return $res; } // 将商品库存循环到lpush的num里 public function doPageSaveNum() { $redis=self::Redis(); $goods_id=1; $sql="select id, num, money from ims_hotmallstore_goods where id=".$goods_id; $goods= $this->mysql($sql,1); if(!empty($goods)){ for($i=1; $i<=$goods['num']; $i++){ $redis->lpush('num',$i); } die('成功!'); }else{ $this->echoMsg(0,'商品不存在。'); } } // 抢购下单 public function doPageGoodsStore() { $goods_id=1; $sql="select id, num, money from ims_hotmallstore_goods where id=".$goods_id; $goods=$this->mysql($sql,1); $redis=self::Redis(); $count=$redis->lpop('num');//每次从num取出1 if($count==0){ $this->echoMsg(0,'no num redis'); } $this->doPageGoodsOrder($goods,1); } public function orderNo() { return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); } // 下单更新库存 public function doPageGoodsOrder($goods,$goods_number) { $user_id=rand(1,500); $orderNo=$this->orderNo(); $number=$goods['num']-$goods_number; if($number<0){ $redis=self::Redis(); $redis->lpush('user',$user_id); return $this->echoMsg(0,'库存扣减失败'.$user_id); } $order['user_id']=$user_id; $order['goods_id']=$goods['id']; $order['number']=$goods_number; $order['price']=$goods['money']; $order['status']=1; $order['sku_id']=2; $order['order_sn']=$orderNo; $order['create_time']=date('Y-m-d H:i:s'); $this->pdo_insert_order($order); $sql="update ims_hotmallstore_goods set num=num-".$goods_number." where num>0 and id=".$goods['id']; $res=$this->mysql($sql,2); if(!empty($res)){ return $this->echoMsg(1,'库存扣减成功'.$number); }elseif (condition) { $redis=self::Redis(); $redis->lpush('user',$user_id); return $this->echoMsg(0,'库存扣减失败'.$user_id); } } function pdo_insert_order($order) { $sql="insert into ims_order(user_id,goods_id,number,price,status,sku_id,order_sn,create_time) values('{$order['user_id']}','{$order['goods_id']}','{$order['number']}','{$order['price']}','{$order['status']}','{$order['sku_id']}','{$order['order_sn']}','{$order['create_time']}')"; $res=$this->mysql($sql,2); return $res; } // 保存日志 function echoMsg($status,$msg) { $time=time(); $sql="insert into ims_order_log(status,msg,create_time)values('{$status}','{$msg}','{$time}')"; $res=$this->mysql($sql,2); } } // 调用--将商品库存循环到lpush的num里 if($_GET['i']==1){ $model = new Test; $model->doPageSaveNum(); } // 调用--高并发抢购下单 if($_GET['i']==2){ $model = new Test; $model->doPageGoodsStore(); } ?>

sql
CREATE TABLE ims_hotmallstore_goods (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL COMMENT ‘商品名称’,
type_id int(11) NOT NULL COMMENT ‘商品分类’,
img text NOT NULL COMMENT ‘商品图片’,
money decimal(10,2) NOT NULL COMMENT ‘售价’,
money2 decimal(10,2) NOT NULL COMMENT ‘原价’,
is_show int(11) NOT NULL DEFAULT ‘1’ COMMENT ‘1.上架2.下架’,
uniacid int(11) NOT NULL COMMENT ‘小程序id’,
inventory int(11) NOT NULL COMMENT ‘库存’,
details text NOT NULL COMMENT ‘详情’,
store_id int(11) NOT NULL COMMENT ‘商家id’,
sales int(11) NOT NULL COMMENT ‘销量’,
logo varchar(100) NOT NULL,
num int(11) NOT NULL,
is_gg int(11) NOT NULL DEFAULT ‘2’ COMMENT ‘是否开启规格’,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

CREATE TABLE ims_order (
id int(11) NOT NULL AUTO_INCREMENT,
order_sn char(32) NOT NULL,
user_id int(11) NOT NULL,
status int(11) NOT NULL DEFAULT ‘0’,
goods_id int(11) NOT NULL DEFAULT ‘0’,
sku_id int(11) NOT NULL DEFAULT ‘0’,
number int(11) NOT NULL,
price int(10) NOT NULL COMMENT ‘价格:单位为分’,
create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=5940 DEFAULT CHARSET=utf8 COMMENT=‘订单表’;

CREATE TABLE ims_order_log (
id int(11) NOT NULL AUTO_INCREMENT,
status int(11) NOT NULL DEFAULT ‘0’,
msg text CHARACTER SET utf8,
create_time int(10) DEFAULT NULL,
PRIMARY KEY (id),
KEY status (status)
) ENGINE=InnoDB AUTO_INCREMENT=4864 DEFAULT CHARSET=gb2312 COMMENT=‘订单日志表’;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java实现秒杀系统@Controller @RequestMapping("seckill")//url:/模块/资源/{id}/细分 /seckill/list public class SeckillController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private SeckillService seckillService; @RequestMapping(value="/list",method = RequestMethod.GET) public String list(Model model){ //获取列表页 List list=seckillService.getSeckillList(); model.addAttribute("list",list); //list.jsp+model = ModelAndView return "list";//WEB-INF/jsp/"list".jsp } @RequestMapping(value = "/{seckillId}/detail",method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId, Model model){ if (seckillId == null){ return "redirect:/seckill/list"; } Seckill seckill = seckillService.getById(seckillId); if (seckill == null){ return "forward:/seckill/list"; } model.addAttribute("seckill",seckill); return "detail"; } //ajax json @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) @ResponseBody public SeckillResult exposer(@PathVariable("seckillId") Long seckillId){ SeckillResult result; try { Exposer exposer =seckillService.exportSeckillUrl(seckillId); result = new SeckillResult(true,exposer); } catch (Exception e) { logger.error(e.getMessage(),e); result = new SeckillResult(false,e.getMessage()); } return result; } @RequestMapping(value = "/{seckillId}/{md5}/execution", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"} ) @ResponseBody public SeckillResult execute(@PathVariable("seckillId")Long seckillId,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值