/** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * * @return void */ public function decrement($key, $value = 1) { $this->connection->transaction(function () use($key, $value) { return $this->incrementOrDecrement($key, $value, function ($current) use($value) { return $current - $value; }); }); }
/** * Increment or decrement an item in the cache. * * @param string $key * @param mixed $value * @param \Closure $callback * @return int|bool */ protected function incrementOrDecrement($key, $value, Closure $callback) { return $this->connection->transaction(function () use($key, $value, $callback) { $prefixed = $this->prefix . $key; $cache = $this->table()->where('key', $prefixed)->lockForUpdate()->first(); if (is_null($cache)) { return false; } $current = $this->encrypter->decrypt($cache->value); $new = $callback($current, $value); if (!is_numeric($current)) { return false; } $this->table()->where('key', $prefixed)->update(['value' => $this->encrypter->encrypt($new)]); return $new; }); }