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

decrement() public method

Performs a decrement operation on a specified numeric cache item.
public decrement ( string $key, integer $offset = 1 ) : integer | boolean
$key string Key of numeric cache item to decrement.
$offset integer Offset to decrement - defaults to `1`.
return integer | boolean The item's new value on successful decrement, else `false`.
    public function decrement($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 testDecrementWithScope()
 {
     $adapter = new File(array('scope' => 'primary'));
     $this->File->write(array('primary_key1' => 5));
     $this->File->write(array('key1' => 10));
     $expected = 4;
     $result = $adapter->decrement('key1');
     $this->assertEqual($expected, $result);
     $expected = array('key1' => 4);
     $result = $adapter->read(array('key1'));
     $this->assertEqual($expected, $result);
 }