'datetime', 'balance' => 'float', ]; public function toDrops($amount = 1) { $rate = Cache::get('drops_rate', 100); $cache_key = 'user_drops_' . $this->id; $drops = Cache::get($cache_key, 0); $total = 0; if ($drops < 0) { $amount += abs($drops) / $rate; } $total += $amount * $rate; $lock = Cache::lock("lock_" . $cache_key, 5); try { $lock->block(5); $this->balance -= $amount; $this->save(); // increment user drops Cache::increment($cache_key, $total); // if user balance <= 0 if ($this->balance < $amount) { throw new BalanceNotEnoughException('余额不足'); } } catch (LockTimeoutException) { throw new CommonException('暂时无法处理此请求,请稍后再试。'); } finally { optional($lock)->release(); } return $this; } }