zoukankan      html  css  js  c++  java
  • R-MS

    //1.获取秒杀商品信息的接口
            public ProductInfo GetProduct(int productId)
            {
                ProductInfo productInfo = new ProductInfo();
                productInfo.PId = 1;
                productInfo.PName = "Iphone11 Pro Max";
                productInfo.PImg = @"";
                productInfo.SalePrice = 12999;
                productInfo.KillPrice = 9999;
                productInfo.KillBeginTime = DateTime.Now.AddMinutes(-30);//开始时间减30分钟
                productInfo.KillEndTime = DateTime.Now.AddMinutes(1);
                productInfo.KillCount = 150;
                //把秒杀的库存写入Redis里
                SetRedisValue("KillCount" + productInfo.PId, "150");
                return productInfo;
            }
            //2.秒杀下单
            public int KillAddOrder(OrderInfo info)
            {
                //res的返回值表示
                //1.下单成功
                //2.库存不足
                //3.三分钟内只能点一次
                int res = 0;
                //同一个用户在规定时间内只能点一次
                var UserKillTime = GetRedisValue("UserId" + info.UserId);
                if (!string.IsNullOrEmpty(UserKillTime))
                {
                    //此处表示用户不是第一次下单
                    DateTime dateTime = Convert.ToDateTime(UserKillTime);
                    var tmpValue = DateTime.Now - dateTime;
                    if (tmpValue.TotalMilliseconds < 3)
                    {
                        //用户三分钟内只能点一次
                        res = 3;
                        return res;
                    }
                    SetRedisValue("UserId" + info.UserId, DateTime.Now.ToString());
                }
                else
                {
                    //此处表示用户是第一次点击
                    SetRedisValue("UserId" + info.UserId, DateTime.Now.ToString());
                    //判断商品库存够不够
                    var count = Convert.ToInt32(GetRedisValue("KillCount-" + info.PId));
                    if (count > 0)
                    {
                        count = count - 1;
                        SetRedisValue("KillCount-" + info.PId, count.ToString());
                    }
                    else
                    {
                        res = 2;
                        return res;
                    }
                }
                return res;
            }
            /// <summary>
            /// 写入redis的值
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            private void SetRedisValue(string key, string value)
            {
                RedisClient redisClient = new RedisClient("127.0.0.1");
                redisClient.Set<string>(key, value);
            }
            /// <summary>
            /// 读取redis的值
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            private string GetRedisValue(string key)
            {
                RedisClient redisClient = new RedisClient("127.0.0.1");
                return redisClient.Get<string>(key);
            }
        }

  • 相关阅读:
    eclipse插件开发:创建向导和导航器配置
    eclipse插件开发:属性视图
    标识出下列SQL语句的执行先后顺序
    webapi和webservice的本质区别
    某仪表上市公司.net-————Sql面试题
    Asp.net MVC 用EF来保存高精度小数时会碰到保留4位小数时,后两位默认为0的解决方法
    Asp.net MVC 集成AD域认证
    jquery zTree插件 json 数据详解
    模板列传值到子窗体中,子窗体中多选gridview中checkbox保存数据多项到数据库中
    asp.net comp雷达图
  • 原文地址:https://www.cnblogs.com/gc1229/p/13197242.html
Copyright © 2011-2022 走看看