Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch '1.3-cache' into 1.3

* 1.3-cache:
  Removing repeated checks, as they've been refactored into Cache::increment() and Cache::decrement().
  Removing php5 visibility keywords.
  Fixing various errors related to Cache::increment() and Cache::decrement().
  Removing test ignore
  Adding atomic increment and decrement methods to cache engines, closes #234
  • Loading branch information...
commit e77ed9264647868fb0b199b8e4bf86b456522f40 2 parents b21538c + 513db9a
@lorenzo lorenzo authored
View
86 cake/libs/cache.php
@@ -329,6 +329,70 @@ function read($key, $config = null) {
}
/**
+ * 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_integer($offset) || $offset < 0) {
+ return false;
+ }
+ $success = $self->_engines[$config]->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_integer($offset) || $offset < 0) {
+ return false;
+ }
+ $success = $self->_engines[$config]->decrement($settings['prefix'] . $key, $offset);
+ $self->set();
+ return $success;
+ }
+/**
* Delete a key from the cache
*
* @param string $key Identifier for the data
@@ -499,6 +563,28 @@ function read($key) {
}
/**
+ * 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
*
* @param string $key Identifier for the data
View
26 cake/libs/cache/apc.php
@@ -75,6 +75,32 @@ function read($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) {
+ 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) {
+ return apc_dec($key, $offset);
+ }
+
+/**
* Delete a key from the cache
*
* @param string $key Identifier for the data
View
32 cake/libs/cache/memcache.php
@@ -126,6 +126,38 @@ function read($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);
+ }
+ 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);
+ }
+ return $this->__Memcache->decrement($key, $offset);
+ }
+
+/**
* Delete a key from the cache
*
* @param string $key Identifier for the data
View
27 cake/libs/cache/xcache.php
@@ -91,6 +91,33 @@ function read($key) {
}
/**
+ * 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) {
+ 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) {
+ return xcache_dec($key, $offset);
+ }
+/**
* Delete a key from the cache
*
* @param string $key Identifier for the data
View
2  cake/tests/cases/libs/cache.test.php
@@ -73,7 +73,7 @@ function testConfig() {
*
* @return void
*/
- function XXtestConfigWithLibAndPluginEngines() {
+ function testConfigWithLibAndPluginEngines() {
App::build(array(
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
View
55 cake/tests/cases/libs/cache/apc.test.php
@@ -27,7 +27,7 @@
* @package cake
* @subpackage cake.tests.cases.libs.cache
*/
-class ApcEngineTest extends UnitTestCase {
+class ApcEngineTest extends CakeTestCase {
/**
* skip method
@@ -140,5 +140,58 @@ function testDeleteCache() {
$result = Cache::delete('delete_test');
$this->assertTrue($result);
}
+
+/**
+ * testDecrement method
+ *
+ * @access public
+ * @return void
+ */
+ function testDecrement() {
+ if ($this->skipIf(!function_exists('apc_dec'), 'No apc_dec() function, cannot test decrement() %s')) {
+ return;
+ }
+ $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
+ */
+ function testIncrement() {
+ if ($this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment() %s')) {
+ return;
+ }
+ $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);
+ }
}
?>
View
48 cake/tests/cases/libs/cache/memcache.test.php
@@ -100,7 +100,7 @@ function testMultipleServers() {
foreach($servers as $server) {
list($host, $port) = explode(':', $server);
- if (!$Memcache->addServer($host, $port)) {
+ if (!@$Memcache->connect($host, $port)) {
$available = false;
}
}
@@ -220,5 +220,51 @@ function testDeleteCache() {
$result = Cache::delete('delete_test');
$this->assertTrue($result);
}
+
+/**
+ * testDecrement method
+ *
+ * @access public
+ * @return void
+ */
+ 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
+ */
+ function testIncrement() {
+ $result = Cache::write('test_increment', 5);
+ $this->assertTrue($result);
+
+ $result = Cache::increment('test_increment');
+ $this->assertEqual(6, $result);
+
+ $result = Cache::read('test_increment');
+ $this->assertEqual(6, $result);
+
+ $result = Cache::increment('test_increment', 2);
+ $this->assertEqual(8, $result);
+
+ $result = Cache::read('test_increment');
+ $this->assertEqual(8, $result);
+ }
}
?>
View
46 cake/tests/cases/libs/cache/xcache.test.php
@@ -173,5 +173,51 @@ function testClearCache() {
$result = Cache::clear();
$this->assertTrue($result);
}
+
+/**
+ * testDecrement method
+ *
+ * @access public
+ * @return void
+ */
+ 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
+ */
+ function testIncrement() {
+ $result = Cache::write('test_increment', 5);
+ $this->assertTrue($result);
+
+ $result = Cache::increment('test_increment');
+ $this->assertEqual(6, $result);
+
+ $result = Cache::read('test_increment');
+ $this->assertEqual(6, $result);
+
+ $result = Cache::increment('test_increment', 2);
+ $this->assertEqual(8, $result);
+
+ $result = Cache::read('test_increment');
+ $this->assertEqual(8, $result);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.