Skip to content

Commit

Permalink
Adding atomic increment and decrement methods to cache engines, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 21, 2010
1 parent a804050 commit dff7e17
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 0 deletions.
86 changes: 86 additions & 0 deletions cake/libs/cache.php
Expand Up @@ -328,6 +328,70 @@ function read($key, $config = null) {
return $success;
}

/**
* Increment a number under the key and return incremented value
*
* @param string $key Identifier for the data
* @param integer $offset How much to add
* @param string $config Optional - string configuration name
* @return mixed new value, or false if the data doesn't exist, is not integer, or if there was an error fetching it
* @access public
*/
function increment($key, $offset = 1, $config = null) {
$self =& Cache::getInstance();

if (!$config) {
$config = $self->__name;
}
$settings = $self->settings($config);

if (empty($settings)) {
return null;
}
if (!$self->isInitialized($config)) {
return false;
}
$key = $self->_engines[$config]->key($key);

if (!$key || is_resource($value)) {
return false;
}
$success = $_this->_Engine[$engine]->increment($settings['prefix'] . $key, $offset);
$self->set();
return $success;
}
/**
* Decrement a number under the key and return decremented value
*
* @param string $key Identifier for the data
* @param integer $offset How much to substract
* @param string $config Optional - string configuration name
* @return mixed new value, or false if the data doesn't exist, is not integer, or if there was an error fetching it
* @access public
*/
function decrement($key, $offset = 1, $config = null) {
$self =& Cache::getInstance();

if (!$config) {
$config = $self->__name;
}
$settings = $self->settings($config);

if (empty($settings)) {
return null;
}
if (!$self->isInitialized($config)) {
return false;
}
$key = $self->_engines[$config]->key($key);

if (!$key || is_resource($value)) {
return false;
}
$success = $_this->_Engine[$engine]->increment($settings['prefix'] . $key, $offset);
$self->set();
return $success;
}
/**
* Delete a key from the cache
*
Expand Down Expand Up @@ -498,6 +562,28 @@ function read($key) {
trigger_error(sprintf(__('Method read() not implemented in %s', true), get_class($this)), E_USER_ERROR);
}

/**
* Increment a number under the key and return incremented value
*
* @param string $key Identifier for the data
* @param integer $offset How much to add
* @return New incremented value, false otherwise
* @access public
*/
function increment($key, $offset = 1) {
trigger_error(sprintf(__('Method increment() not implemented in %s', true), get_class($this)), E_USER_ERROR);
}
/**
* Decrement a number under the key and return decremented value
*
* @param string $key Identifier for the data
* @param integer $value How much to substract
* @return New incremented value, false otherwise
* @access public
*/
function decrement($key, $offset = 1) {
trigger_error(sprintf(__('Method decrement() not implemented in %s', true), get_class($this)), E_USER_ERROR);
}
/**
* Delete a key from the cache
*
Expand Down
32 changes: 32 additions & 0 deletions cake/libs/cache/apc.php
Expand Up @@ -74,6 +74,38 @@ function read($key) {
return apc_fetch($key);
}

/**
* Increments the value of an integer cached key
*
* @param string $key Identifier for the data
* @param integer $offset How much to increment
* @param integer $duration How long to cache the data, in seconds
* @return New incremented value, false otherwise
* @access public
*/
function increment($key, $offset = 1) {
if (!is_integer($offset) || $offset < 0) {
return false;
}
return apc_inc($key, $offset);
}

/**
* Decrements the value of an integer cached key
*
* @param string $key Identifier for the data
* @param integer $offset How much to substract
* @param integer $duration How long to cache the data, in seconds
* @return New decremented value, false otherwise
* @access public
*/
function decrement($key, $offset = 1) {
if (!is_integer($offset) || $offset < 0) {
return false;
}
return apc_dec($key, $offset);
}

/**
* Delete a key from the cache
*
Expand Down
38 changes: 38 additions & 0 deletions cake/libs/cache/memcache.php
Expand Up @@ -125,6 +125,44 @@ function read($key) {
return $this->__Memcache->get($key);
}

/**
* Increments the value of an integer cached key
*
* @param string $key Identifier for the data
* @param integer $offset How much to increment
* @param integer $duration How long to cache the data, in seconds
* @return New incremented value, false otherwise
* @access public
*/
function increment($key, $offset = 1) {
if ($this->settings['compress']) {
trigger_error(sprintf(__('Method increment() not implemented for compressed cache in %s', true), get_class($this)), E_USER_ERROR);
}
if (!is_integer($offset) || $offset < 0) {
return false;
}
return $this->__Memcache->increment($key, $offset);
}

/**
* Decrements the value of an integer cached key
*
* @param string $key Identifier for the data
* @param integer $offset How much to substract
* @param integer $duration How long to cache the data, in seconds
* @return New decremented value, false otherwise
* @access public
*/
function decrement($key, $offset = 1) {
if ($this->settings['compress']) {
trigger_error(sprintf(__('Method decrement() not implemented for compressed cache in %s', true), get_class($this)), E_USER_ERROR);
}
if (!is_integer($offset) || $offset < 0) {
return false;
}
return $this->__Memcache->decrement($key, $offset);
}

/**
* Delete a key from the cache
*
Expand Down
33 changes: 33 additions & 0 deletions cake/libs/cache/xcache.php
Expand Up @@ -90,6 +90,39 @@ function read($key) {
return false;
}

/**
* Increments the value of an integer cached key
* If the cache key is not an integer it will be treated as 0
*
* @param string $key Identifier for the data
* @param integer $offset How much to increment
* @param integer $duration How long to cache the data, in seconds
* @return New incremented value, false otherwise
* @access public
*/
function increment($key, $offset = 1) {
if (!is_integer($offset) || $offset < 0) {
return false;
}
return xcache_inc($key, $offset);
}

/**
* Decrements the value of an integer cached key.
* If the cache key is not an integer it will be treated as 0
*
* @param string $key Identifier for the data
* @param integer $offset How much to substract
* @param integer $duration How long to cache the data, in seconds
* @return New decremented value, false otherwise
* @access public
*/
function decrement($key, $offset = 1) {
if (!is_integer($offset) || $offset < 0) {
return false;
}
return xcache_dec($key, $offset);
}
/**
* Delete a key from the cache
*
Expand Down
47 changes: 47 additions & 0 deletions cake/tests/cases/libs/cache/apc.test.php
Expand Up @@ -140,5 +140,52 @@ function testDeleteCache() {
$result = Cache::delete('delete_test');
$this->assertTrue($result);
}

/**
* testDecrement method
*
* @access public
* @return void
*/
public function testDecrement() {
$result = Cache::write('test_decrement', 5);
$this->assertTrue($result);

$result = Cache::decrement('test_decrement');
$this->assertEqual(4, $result);

$result = Cache::read('test_decrement');
$this->assertEqual(4, $result);

$result = Cache::decrement('test_decrement', 2);
$this->assertEqual(2, $result);

$result = Cache::read('test_decrement');
$this->assertEqual(2, $result);

}

/**
* testIncrement method
*
* @access public
* @return void
*/
public function testIncrement() {
$result = Cache::write('test_increment', 5);
$this->assertTrue($result);

$result = Cache::increment('test_increment');
$this->assertEqual(5, $result);

$result = Cache::read('test_increment');
$this->assertEqual(5, $result);

$result = Cache::increment('test_increment', 2);
$this->assertEqual(7, $result);

$result = Cache::read('test_increment');
$this->assertEqual(7, $result);
}
}
?>
47 changes: 47 additions & 0 deletions cake/tests/cases/libs/cache/memcache.test.php
Expand Up @@ -220,5 +220,52 @@ function testDeleteCache() {
$result = Cache::delete('delete_test');
$this->assertTrue($result);
}

/**
* testDecrement method
*
* @access public
* @return void
*/
public function testDecrement() {
$result = Cache::write('test_decrement', 5);
$this->assertTrue($result);

$result = Cache::decrement('test_decrement');
$this->assertEqual(4, $result);

$result = Cache::read('test_decrement');
$this->assertEqual(4, $result);

$result = Cache::decrement('test_decrement', 2);
$this->assertEqual(2, $result);

$result = Cache::read('test_decrement');
$this->assertEqual(2, $result);

}

/**
* testIncrement method
*
* @access public
* @return void
*/
public function testIncrement() {
$result = Cache::write('test_increment', 5);
$this->assertTrue($result);

$result = Cache::increment('test_increment');
$this->assertEqual(5, $result);

$result = Cache::read('test_increment');
$this->assertEqual(5, $result);

$result = Cache::increment('test_increment', 2);
$this->assertEqual(7, $result);

$result = Cache::read('test_increment');
$this->assertEqual(7, $result);
}
}
?>
47 changes: 47 additions & 0 deletions cake/tests/cases/libs/cache/xcache.test.php
Expand Up @@ -173,5 +173,52 @@ function testClearCache() {
$result = Cache::clear();
$this->assertTrue($result);
}

/**
* testDecrement method
*
* @access public
* @return void
*/
public function testDecrement() {
$result = Cache::write('test_decrement', 5);
$this->assertTrue($result);

$result = Cache::decrement('test_decrement');
$this->assertEqual(4, $result);

$result = Cache::read('test_decrement');
$this->assertEqual(4, $result);

$result = Cache::decrement('test_decrement', 2);
$this->assertEqual(2, $result);

$result = Cache::read('test_decrement');
$this->assertEqual(2, $result);

}

/**
* testIncrement method
*
* @access public
* @return void
*/
public function testIncrement() {
$result = Cache::write('test_increment', 5);
$this->assertTrue($result);

$result = Cache::increment('test_increment');
$this->assertEqual(5, $result);

$result = Cache::read('test_increment');
$this->assertEqual(5, $result);

$result = Cache::increment('test_increment', 2);
$this->assertEqual(7, $result);

$result = Cache::read('test_increment');
$this->assertEqual(7, $result);
}
}
?>

0 comments on commit dff7e17

Please sign in to comment.