Ticket Details

Read-through caching seems to be broken

BUG Ticket (closed)

###What happened:
- [read this article](http://dev.lithify.me/lithium/wiki/blog/Lithium-0-9-5-Dont-Stop-Till-You-Get-Enough)
- loved it
- tried it and cried.

{{{
return Cache::read('default', 'spam', array('write' => function() {
    return array('+1 hour' => 'spam and eggs');
}));
}}}

###What was expected:
- cache to be written and set value to be returned.
- a test case to exist

I have no filters and the `$results` is `false` (and not `null`), see: [Cache.php#151](http://dev.lithify.me/lithium/source/libraries/lithium/storage/Cache.php#151)
on 05.30.11 reported by: greut owned by: nate

Updates

on 06.01.11 by greut
A test case for my problem (the same one but using `Memcache`)

**lithium\tests\cases\storageCacheTest.php**
{{{
	public function testCacheReadThroughWriteMemcache() {
		$this->skipIf(!Memcache::enabled(), 'Memcache is required');
		$config = array('default' => array(
			'adapter' => 'Memcache', 'filters' => array()
		));
		Cache::config($config);
		$result = Cache::config();
		$expected = $config;
		$this->assertEqual($expected, $result);

		$write = function() {
			return array('+1 minute' => 'read-through write');
		};
		$result = Cache::read('default', 'read_through', compact('write'));
		$this->assertEqual('read-through write', $result);

		$result = Cache::read('default', 'read_through');
		$this->assertEqual('read-through write', $result);

		$write = array('+1 minute' => 'string read-through write');
		$result = Cache::read('default', 'string_read_through', compact('write'));
		$this->assertEqual('string read-through write', $result);

		$result = Cache::read('default', 'string_read_through');
		$this->assertEqual('string read-through write', $result);
	}
}}}

**375.patch**

a quick fix.

{{{
diff --git a/libraries/lithium/storage/Cache.php b/libraries/lithium/storage/Cache.php
index 1d3c3fe..345d1ba 100644
--- a/libraries/lithium/storage/Cache.php
+++ b/libraries/lithium/storage/Cache.php
@@ -150,11 +150,10 @@ class Cache extends \lithium\core\Adaptable {
                $filters = $settings[$name]['filters'];
                $result = static::_filter(__FUNCTION__, $params, $method, $filters);
 
-               if ($result === null && $options['write']) {
+               if (($result === null || $result === false) && $options['write']) {
                        $write = (is_callable($options['write'])) ? $options['write']() : $options['write'];
-                       list($expiry, $value) = each($write);
-
-                       return static::write($name, $key, $value, $expiry);
+                       list($expiry, $result) = each($write);
+                       return static::write($name, $key, $result, $expiry);
                }
 
                if ($options['strategies']) {
}}}
on 06.01.11 by greut
check the [ commit](http://rad-dev.org/forks/greut/lithium/commits/view/bef75491ab57b9d8a8f6bde3b841a60bddf6eb25) out.
(fixed) on 06.01.11 by nate
Should be fixed in [920d33bbaeaeabf623dae2a0961bddf937b75f84]. Thanks for the report.
on 06.01.11 by greut
I still have “1” the first time.

{{{
-			list($expiry, $value) = each($write);
+			list($expiry, $result) = each($write);
-			return static::write($name, $key, $value, $expiry);
+			static::write($name, $key, $result, $expiry);
}}}
http://rad-dev.org/forks/greut/lithium/commits/view/adb876de87c8b72009198648309d1b8eaa0347a6#highlight

I'll try to bake a test case for that.