protected function doIncrement($key, $offset, $initial, $expire)
{
/*
* I used to have all this logic in a huge & ugly query, but getting
* that right on multiple SQL engines proved challenging (SQLite doesn't
* do INSERT ... ON DUPLICATE KEY UPDATE ..., for example)
* I'll just stuff it in a transaction & leverage existing methods.
*/
$this->client->beginTransaction();
$this->clearExpired();
$value = $this->get($key);
if ($value === false) {
$return = $this->add($key, $initial, $expire);
if ($return) {
$this->client->commit();
return $initial;
}
} elseif (is_numeric($value)) {
$value += $offset;
// < 0 is never possible
$value = max(0, $value);
$return = $this->replace($key, $value, $expire);
if ($return) {
$this->client->commit();
return (int) $value;
}
}
$this->client->rollBack();
return false;
}