public async Task <WrappedResponse <MoneyInfo> > Handle(GetMoneyCommand request, CancellationToken cancellationToken)
        {
            var moneyInfo = await _moneyRedisRep.GetMoneyInfo(request.Id);

            if (moneyInfo == null)
            {
                using (var locker = _moneyRedisRep.Locker(KeyGenTool.GenUserKey(request.Id,
                                                                                MoneyInfo.ClassName)))
                {
                    await locker.LockAsync();

                    moneyInfo = await _moneyRep.FindAndAdd(request.Id,
                                                           new MoneyInfo(request.Id, 0, 0, 0, 0, 0));

                    _ = _moneyRedisRep.SetMoneyInfo(moneyInfo);
                }

                if (moneyInfo == null)
                {
                    return(new WrappedResponse <MoneyInfo>(ResponseStatus.GetMoneyError, null, null));
                }
            }
            WrappedResponse <MoneyInfo> response = new WrappedResponse <MoneyInfo>
                                                       (ResponseStatus.Success, null, moneyInfo);

            Log.Debug($"GetMoneyCommand:{moneyInfo.CurCoins},{moneyInfo.Carry}");
            return(response);
        }
Esempio n. 2
0
        public async Task <BodyResponse <MoneyInfo> > Handle(GetMoneyCommand request,
                                                             CancellationToken cancellationToken)
        {
            var moneyInfo = await _redis.GetMoney(request.Id);

            if (moneyInfo == null)
            {
                using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id,
                                                                          MoneyInfo.ClassName)))
                {
                    await locker.LockAsync();

                    moneyInfo = await _moneyRepository.FindAndAdd(request.Id,
                                                                  new MoneyInfo(request.Id, 0, 0, 0, 0, 0));

                    _ = _redis.SetMoney(request.Id, moneyInfo);
                }

                if (moneyInfo == null)
                {
                    return(new BodyResponse <MoneyInfo>(StatusCodeDefines.GetMoneyError, null, null));
                }
            }
            BodyResponse <MoneyInfo> response = new BodyResponse <MoneyInfo>
                                                    (StatusCodeDefines.Success, null, moneyInfo);

            Log.Debug($"GetMoneyCommand:{moneyInfo.CurCoins},{moneyInfo.Carry}");
            return(response);
        }