redis案例 -- 商品拍卖

代码说明:在小程序上进行商品拍卖,多人抢拍,零秒抢拍,用redis(事物)来确认抢拍价格的准确性,什么不说了,附上代码!

    /**
     * 加价
     */
    public function plusPrice(){
        $member_id = $this->userInfo[‘member_id‘]>0 ? $this->userInfo[‘member_id‘] : 0;
        $mobile = $this->userInfo[‘member_name‘]>0 ? $this->userInfo[‘member_name‘] : 0;
        $lb_id = input(‘lb_id‘,0);
        $every_hand = input(‘price‘,0); // 每一手
        $goods_id = input(‘goods_id‘,0);
        $nickname = input(‘nickname‘,‘‘);
        $avatar = input(‘avatar‘,‘‘);
        $msg_type = input(‘msg_type‘,‘morker‘);
        $this->redis->set(‘msgType‘,$msg_type);

        // 判断参数
        if($lb_id<=0)
        {
            $this->returnmsg(‘2055‘, ‘直播ID不能为空‘);
        }
        if($goods_id<=0)
        {
            $this->returnmsg(‘2055‘, ‘商品ID不能为空‘);
        }
        if($member_id<=0)
        {
            $this->returnmsg(‘2055‘, ‘会员ID不能为空‘);
        }

        // redis事物控制价格不出问题
        $thing_key = ‘wxGoodsPlusPrice‘;
        $this->redis->setex($thing_key, 60);
        //监视keyTest
        $this->redis->watch(array($thing_key));
        $plus_max_price_key = ‘plusMaxPrices:‘.$lb_id.‘:‘.$goods_id;

        $iag_key = ‘setAuctionGoods:‘.$lb_id;
        $iag_g_info_json = $this->redis->get($iag_key);
        $iag_g_info = \json_decode($iag_g_info_json,true);

        $plus_max_prices = $this->redis->lRange($plus_max_price_key,0,-1);
        $max_price = max($plus_max_prices)>0 ? max($plus_max_prices) : $iag_g_info[‘starting_price‘]; // 当没有最大价格时,取起拍价
        $price = $max_price + $every_hand;
        //开启事务
        $this->redis->multi();
        $this->redis->incr($thing_key);
        $this->redis->lPush($plus_max_price_key,$price);
        //执行事务
        $result = $this->redis->exec();

        // 这里很重要,后续代码一定要在事物完成这个判断里进行 ,
        // 要不然,就会出现数据错乱的问题
        if($result){
            $auction_goods_id_key = ‘auctionGoodsId:‘.$lb_id;
            $auction_goods_id = $this->redis->get($auction_goods_id_key);
            if(empty($auction_goods_id)){
                $this->redis->lPop($plus_max_price_key);
                $this->returnmsg(‘2055‘, ‘拍卖结束‘);
            }

            // 把价格重新保存一下
            if($price<=$max_price){
                $this->redis->lPop($plus_max_price_key);
                $this->returnmsg(‘2055‘, ‘不是最高价‘);
            }

            // 加入数据
            $max_price_uinfo_key = ‘maxPriceUinfoKey:‘.$lb_id.‘-‘.$goods_id;
            $old_max_price_uinfo_json = $this->redis->get($max_price_uinfo_key);
            $old_max_price_uinfo = json_decode($old_max_price_uinfo_json,true);
            if($old_max_price_uinfo[‘user_id‘] == $member_id){
                $this->redis->lPop($plus_max_price_key);
                $this->returnmsg(‘2055‘, ‘您已经领先了‘);
            }

            // 判断不要重复数据
            $unique_price_key = ‘unique_price_key:‘.$lb_id.‘:‘.$goods_id;
            $price_arr = $this->redis->lRange($unique_price_key,0,-1);
            if(!in_array($price,$price_arr)){
                $this->redis->lPush($unique_price_key,$price);
                $max_price_uinfo = array(
                    ‘user_id‘=>$member_id,
                    ‘mobile‘=>$mobile,
                    ‘max_price‘=>$price,
                    ‘goods_id‘=>$goods_id,
                    ‘lb_id‘=>$lb_id,
                    ‘nickname‘=>$nickname,
                    ‘new_avatar‘=>$avatar
                );
                $this->redis->set($max_price_uinfo_key,\json_encode($max_price_uinfo,true));

                $plus_max_price_mtime_key = ‘plusMaxMtimePrices:‘.$lb_id.‘:‘.$goods_id;
                $this->redis->lPush($plus_max_price_mtime_key,$member_id.‘--‘.$price.‘--‘.$max_price.‘--‘.microtime(1));

                $mq_plus_price_consumption_key = ‘mq_plus_price_consumption‘;
                $this->redis->lPush($mq_plus_price_consumption_key,\json_encode($max_price_uinfo,true));
                // QueueClient::push(new LiveImSendMsgProcessQueue(), [‘reset_name‘ => [‘live_im_send_msg‘],‘lb_id‘=>$lb_id,‘user_id‘=>$member_id,‘price‘=>$price]);
            }
        }
        $this->returnmsg(‘200‘, ‘success‘);
    }

原文地址:https://www.cnblogs.com/FLy-1992/p/12187417.html

时间: 2024-08-08 17:59:10

redis案例 -- 商品拍卖的相关文章

Redis案例——商品秒杀,购物车

秒杀案例: 1 <?php 2 header("content-type:text/html;charset=utf-8"); 3 $redis = new redis(); 4 $result = $redis->connect('10.10.10.119', 6379); 5 $mywatchkey = $redis->get("mywatchkey"); 6 $rob_total = 100; //抢购数量 7 if($mywatchkey&

Redis(1)-用redis存储商品-用户关系

package com.lipeng; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import

利用redis进行商品秒杀

这里是用redis的list集合开发,redis的list集合是具有原子性的,不必担心多线程时会取到重复的数据,即使请求同时到达也会排队进行数据操作 1. 先说说大概思路,关于数据库库存字段的设计.数据类型设置为非负数的unsigned,  如果秒杀的商品数量为50个. 秒杀开始前在redis里面建一个类型为list,键名为goods_num_商品id 的键,存入50个元素.元素值可随意取. 2. 建一个键名为user_id_商品id 的键,每次用户秒杀成功,把用户id存入里面,用来判断是否成功

redis解决商品秒杀问题

博主最近在项目中遇到了抢购问题!现在分享下.抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个:1 高并发对数据库产生的压力2 竞争状态下如何解决库存的正确减少("超卖"问题)对于第一个问题,已经很容易想到用缓存来处理抢购,避免直接操作数据库,例如使用Redis.重点在于第二个问题常规写法:查询出对应商品的库存,看是否大于0,然后执行生成订单等操作,但是在判断库存是否大于0处,如果在高并发下就会有问题,导致库存量出现负数 优化方案1:将库存字段number字段设为unsig

Redis 案例实战分析

[1]案例一现象:生产系统刚开始运行阶段,系统稳定.但是运行了一段时间后,发现部分时间段系统接口响应变慢.查看客户端日志经常会出现如下错误: redis.clients.jedis.exception.JedisConnectionException:java.net.SocketTimeoutException:Read time out问题定位:执行 slowlog 查看慢查询日志,发现大量的 keys 命令操作,keys 命令在大量并发情况下性能非常差,生产环境,尽量避免使用 keys,接

SpringBoot+RabbitMQ+Redis实现商品秒杀

业务分析 一般而言,商品秒杀大概可以拆分成以下几步: 用户校验 校验是否多次抢单,保证每个商品每个用户只能秒杀一次 下单 订单信息进入消息队列,等待消费 减少库存 消费订单消息,减少商品库存,增加订单记录 付款 十五分钟内完成支付,修改支付状态 创建表 goods_info 商品库存表 列 说明 id 主键(uuid) goods_name 商品名称 goods_stock 商品库存 package com.jason.seckill.order.entity; /** * 商品库存 */ pu

SSH实战项目——在线商品拍卖网

前言:这个项目属于学习Java Web的SSH框架的练习之作,参考至<Struts2+Spring+Hibernate框架技术与项目实战>这本书的第24章.当然部分内容我觉得还是存在不足的,因此我根据自己的理解进行了优化. 注:整个项目已经开源,有需要源代码进行研究的可以自行下载:https://github.com/zifangsky/OnlineAuction 一 整个项目介绍 (1)运行效果截图: 注:因为编译好的项目被删过,因此当时上传的图片就没了 (2)采用技术: Struts2+S

redis案例-set来存储关注关系-redis

<?php /* * This example would probably work best if you're using * an MVC framework, but it can be used standalone as well. * * This example also assumes you are using Predis, the excellent * PHP Redis library available here: * https://github.com/nrk

javaweb——表单案例 商品列表

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <