Permalink
Browse files

Fixing issue with read-through cache writes not returning a value. Im…

…plementing alternate read-through syntax.
  • Loading branch information...
1 parent df7a29c commit 59dd4af6461c70360c5ad9051eaf9ed8bcc02d1c @nateabele nateabele committed Mar 19, 2012
Showing with 25 additions and 18 deletions.
  1. +10 −7 storage/Cache.php
  2. +15 −11 tests/cases/storage/CacheTest.php
View
@@ -101,8 +101,8 @@ public static function write($name, $key, $data, $expiry = null, array $options
if (!isset($settings[$name])) {
return false;
}
-
$conditions = $options['conditions'];
+
if (is_callable($conditions) && !$conditions()) {
return false;
}
@@ -140,8 +140,8 @@ public static function read($name, $key, array $options = array()) {
if (!isset($settings[$name])) {
return false;
}
-
$conditions = $options['conditions'];
+
if (is_callable($conditions) && !$conditions()) {
return false;
}
@@ -151,15 +151,18 @@ public static function read($name, $key, array $options = array()) {
$filters = $settings[$name]['filters'];
$result = static::_filter(__FUNCTION__, $params, $method, $filters);
- if ($result === null && $options['write']) {
- $write = (is_callable($options['write'])) ? $options['write']() : $options['write'];
+ if ($result === null && ($write = $options['write'])) {
+ $write = is_callable($write) ? $write() : $write;
list($expiry, $value) = each($write);
+ $value = is_callable($value) ? $value() : $value;
- return static::write($name, $key, $value, $expiry);
+ if (static::write($name, $key, $value, $expiry)) {
+ $result = $value;
+ }
}
if ($options['strategies']) {
- $options = array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
+ $options = compact('key') + array('mode' => 'LIFO', 'class' => __CLASS__);
$result = static::applyStrategies(__FUNCTION__, $name, $result, $options);
}
return $result;
@@ -181,8 +184,8 @@ public static function delete($name, $key, array $options = array()) {
if (!isset($settings[$name])) {
return false;
}
-
$conditions = $options['conditions'];
+
if (is_callable($conditions) && !$conditions()) {
return false;
}
@@ -203,19 +203,16 @@ public function testCacheReadMultipleItems() {
}
public function testCacheReadWithConditions() {
- $config = array('default' => array(
- 'adapter' => 'Memory', 'filters' => array()
- ));
+ $config = array('default' => array('adapter' => 'Memory', 'filters' => array()));
Cache::config($config);
+
$result = Cache::config();
$expected = $config;
$this->assertEqual($expected, $result);
- $conditions = function() {
+ $result = Cache::read('default', 'some_key', array('conditions' => function() {
return false;
- };
-
- $result = Cache::read('default', 'some_key', compact('conditions'));
+ }));
$this->assertFalse($result);
$conditions = function() use (&$config) {
@@ -226,8 +223,7 @@ public function testCacheReadWithConditions() {
$result = Cache::read('default', 'some_key', compact('conditions'));
$this->assertTrue($result);
- $result = Cache::read('non_existing', 'key_value', compact('conditions'));
- $this->assertFalse($result);
+ $this->assertFalse(Cache::read('non_existing', 'key_value', compact('conditions')));
}
public function testCacheIncrementDecrementWithConditions() {
@@ -313,8 +309,7 @@ public function testCacheReadThroughWrite() {
$write = function() {
return array('+1 minute' => 'read-through write');
};
- $result = Cache::read('default', 'read_through');
- $this->assertNull($result);
+ $this->assertNull(Cache::read('default', 'read_through'));
$result = Cache::read('default', 'read_through', compact('write'));
$this->assertIdentical('read-through write', $result);
@@ -328,6 +323,15 @@ public function testCacheReadThroughWrite() {
$result = Cache::read('default', 'string_read_through');
$this->assertIdentical('string read-through write', $result);
+
+ $this->assertNull(Cache::read('default', 'string_read_through_2'));
+
+ $result = Cache::read('default', 'string_read_through_2', array('write' => array(
+ '+1 minute' => function() {
+ return 'read-through write 2';
+ }
+ )));
+ $this->assertIdentical('read-through write 2', $result);
}
public function testCacheReadAndWrite() {

0 comments on commit 59dd4af

Please sign in to comment.