Commit e75eaf7e authored by vicotor's avatar vicotor

update for query check

parent c03c7cda
......@@ -231,3 +231,48 @@ func (c *CacheData) costFreeTime(uid int64, user *UserInfo, userLevel *model.Use
return nil
}
func (c *CacheData) costCharge(uid int64, fee int64) error {
// todo: just incr charge.
chargeKey := fmt.Sprintf("charge-%d:", uid)
_, err := c.rdb.IncrBy(c.ctx, chargeKey, fee).Result()
return err
}
func (c *CacheData) costForFee(uid int64, fee int64) error {
// todo: decr charge and balance.
chargeKey := fmt.Sprintf("charge-%d:", uid)
balKey := fmt.Sprintf("bal-%d:", uid)
if fee > 0 {
txp := c.rdb.TxPipeline()
txp.DecrBy(c.ctx, chargeKey, fee)
txp.DecrBy(c.ctx, balKey, fee)
_, err := txp.Exec(c.ctx)
return err
}
if fee < 0 {
txp := c.rdb.TxPipeline()
txp.DecrBy(c.ctx, chargeKey, fee)
_, err := txp.Exec(c.ctx)
return err
}
return nil
}
func (c *CacheData) PayforFee(uid int64, fee int64) error {
locked, release, _ := c.getUserLockWithRetry(uid, USER_INFO_LOCK_DURATION*10)
if !locked {
return ErrWaitLockTimeout
}
defer release()
return c.costForFee(uid, fee)
}
func (c *CacheData) RollbackForFee(uid int64, fee int64) error {
locked, release, _ := c.getUserLockWithRetry(uid, USER_INFO_LOCK_DURATION*10)
if !locked {
return ErrWaitLockTimeout
}
defer release()
return c.costForFee(uid, -fee)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment