lithium\storage\cache\adapter\File::increment PHP Method

increment() public method

Performs an increment operation on a specified numeric cache item.
public increment ( string $key, integer $offset = 1 ) : integer | boolean
$key string Key of numeric cache item to increment
$offset integer Offset to increment - defaults to `1`.
return integer | boolean The item's new value on successful increment, else `false`.
    public function increment($key, $offset = 1)
    {
        if ($this->_config['scope']) {
            $key = "{$this->_config['scope']}_{$key}";
        }
        if (!($result = $this->_read($key))) {
            return false;
        }
        if (!$this->_write($key, $result['value'] += $offset, $result['expiry'])) {
            return false;
        }
        return $result['value'];
    }

Usage Example

Example #1
0
 public function testIncrementWithScope()
 {
     $adapter = new File(array('scope' => 'primary'));
     $this->File->write(array('primary_key1' => 5));
     $this->File->write(array('key1' => 10));
     $expected = 6;
     $result = $adapter->increment('key1');
     $this->assertEqual($expected, $result);
     $expected = array('key1' => 6);
     $result = $adapter->read(array('key1'));
     $this->assertEqual($expected, $result);
 }