protected function doIncrement($operation, $key, $offset, $initial, $expire)
{
if (isset($this->keys[$key])) {
if (in_array($this->keys[$key][0], array('set', 'add', 'replace', 'cas'))) {
// we're trying to increment a key that's only just being stored
// in this transaction - might as well combine those
$symbol = $this->keys[$key][1] === 'increment' ? 1 : -1;
$this->keys[$key][2]['value'] += $symbol * $offset;
$this->keys[$key][2]['expire'] = $expire;
} elseif (in_array($this->keys[$key][0], array('increment', 'decrement'))) {
// we're trying to increment a key that's already being incremented
// or decremented in this transaction - might as well combine those
// we may be combining an increment with a decrement
// we must carefully figure out how these 2 apply against each other
$symbol = $this->keys[$key][0] === 'increment' ? 1 : -1;
$previous = $symbol * $this->keys[$key][2]['offset'];
$symbol = $operation === 'increment' ? 1 : -1;
$current = $symbol * $offset;
$offset = $previous + $current;
$this->keys[$key][2]['offset'] = abs($offset);
// initial value must also be adjusted to include the new offset
$this->keys[$key][2]['initial'] += $current;
$this->keys[$key][2]['expire'] = $expire;
// adjust operation - it might just have switched from increment to
// decrement or vice versa
$operation = $offset >= 0 ? 'increment' : 'decrement';
$this->keys[$key][0] = $operation;
$this->keys[$key][1] = array($this->cache, $operation);
} else {
// touch & delete become useless if incrementing/decrementing after
unset($this->keys[$key]);
}
}
if (!isset($this->keys[$key])) {
$args = array('key' => $key, 'offset' => $offset, 'initial' => $initial, 'expire' => $expire);
$this->keys[$key] = array($operation, array($this->cache, $operation), $args);
}
}