栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

TP使用Redis列表list完成秒杀(限制商品)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TP使用Redis列表list完成秒杀(限制商品)

redis = new Redis();        // 实例化
        $this->redis->connect('127.0.0.1','6379');
        $this->redis->auth('zxf123456');
    }

    
    public function Ms_init()
    {
        // 删除缓存列表
        $this->redis->del($this->cachekey);

        $len = $this->redis->llen($this->cachekey);
        $count = $this->store - $len;

        for ($i=0; $i < $count; $i++) { 

            // 向库存列表推进50个,模拟50个商品库存
            $this->redis->lpush($this->cachekey,1);
        }

        echo "库存初始化完成:".$this->redis->llen($this->cachekey);
    }
 

    
    public function index()
    {
        $id = 1;    //商品编号
        
        if (empty($id)) {
            // 记录失败日志
            return $this->writeLog(0,'商品编号不存在');    
        }

        // 计算库存列表长度
        $count = $this->redis->llen($this->cachekey);

        // 先判断库存是否为0,为0秒杀失败,不为0,则进行先移除一个元素,再进行数据库操作
        if ($count == 0) {    //库存为0

            $this->writeLog(0,'库存为0');
            echo "库存为0";
            exit;

        }else{
            // 有库存
            //先移除一个列表元素
            $this->redis->lpop($this->cachekey);

            $ordersn = $this->build_order_no();    //生成订单
            $uid = rand(0,9999);    //随机生成用户id
            $status = 1;
            // 再进行数据库操作
            $data = Db::table('ab_goods')->field('count,amount')->where('id',$id)->find();    //查找商品

            if (!$data) {
                return $this->writeLog(0,'该商品不存在');
            }

            $insert_data = [
                'order_sn' => $ordersn,
                'user_id' => $uid,
                'goods_id' => $id,
                'price'    => $data['amount'],
                'status' => $status,
                'addtime' => date('Y-m-d H:i:s')
            ];

            // 订单入库
            $result = Db::table('ab_order')->insert($insert_data);
            // 自动减少一个库存
            $res = Db::table('ab_goods')->where('id',$id)->setDec('count');

            if ($res) {
                echo "第".$count."件秒杀成功";
                $this->writeLog(1,'秒杀成功');
            }else{
                echo "第".$count."件秒杀失败";
                $this->writeLog(0,'秒杀失败');
            }
        }
    }

    
    public function build_order_no()
    {
        return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    
    public function writeLog($status = 1,$msg)
    {
        $data['count'] = 1;
        $data['status'] = $status;
        $data['addtime'] = date('Y-m-d H:i:s');
        $data['msg'] = $msg;
        return Db::table('ab_log')->insertGetId($data);
    }

}

通过list 弹出商品能达到拒绝超卖 , 但是会有秒杀到不付款的情况 , 根据业务需求可以再制作倒计时功能,倒计时结束后通过lpush放回列表中

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736991.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号