电商秒杀功能实现

知乎:

      

开源社区:

 

1) 客户端通过算法进行过滤

2) 负载均衡分配

3) 队列计数

4) 秒杀完成,更新数据库

 他人代码参考:

   public ActionResult SecKill()
        {
            //双十一那天的秒杀活动,
            //早上11点钟的有611个名额,下午三点钟的有500个名额
            int totalPlaces = 0;//名额
            DateTime time11 = new DateTime(2015, 11, 11, 11, 0, 0);
            DateTime time15 = new DateTime(2015, 11, 11, 18, 0, 0);
            //根据时间获取队列的最大的容量
            if(time11<DateTime.Now&&DateTime.Now<time15)
            {
                totalPlaces = 611;
            }else if(DateTime.Now>time15)
            {
                totalPlaces = 500;
            }

            SecKillQueue q = new SecKillQueue(totalPlaces);//创建队列
            //入队
            while (!q.IsFull())
            {
                q.In(SessionContext.User.UserId);

                int count = totalPlaces - q.GetQueueLength();

                return Json(new { leftCount=count });//返回剩下的名额
            }

            //根据队列中保存的用户ID,异步往Consume表插入记录
            Task.Run(() => InsertToConsume(q));

            return Json(new { ret = 1, msg = "秒杀结束" });
        }

  

public static void InsertToConsume(SecKillQueue q)
        {
            //出列
            while (!q.IsEmpty())
            {
                int userId = (int)q.Out();
                User user = UserServices.GetItemById(userId);
 
                //这里还要判断当前出列的用户是否已经参与参与过秒杀活动了,
                //如果有的话就忽略,并且还要把名额重新添加到队列
 
                Consume consume = new Consume()
                {
                    WorkShopID = user.IsWorkShop == 2 ? (int)user.WorkShopID : user.Id,
                    SerialNumber = PromotionHelper.getOrderCode() + "-" + new Random().Next(100000, 999999),
                    Amount = 110,
                    ConsumeType = 1,     //本金预存             
                    PayStatus = 0       //未支付
                };
                ConsumeServices.Insert(consume);
            }
        }

  

转载于:https://www.cnblogs.com/licin/p/6547539.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值