Ticket Details

Read-through caching not working as expected

RFC Ticket (pending)

###What happened:
- Call to:

{{{
$data = Cache::read("default", $key, 
		array('write' => 
			function() use ($key) {
	    			return array('+5 minutes' => Item::get($key));
		}));
}}}

returns a bool false.

###What was expected:
- should return the document object returned by Item:get() or a cached version...

- Changed Cache::read as follows to resolve:

{{{

	/**
	 * Reads from the specified cache configuration
	 *
	 * @param string $name Configuration to be used for reading
	 * @param mixed $key Key to be retrieved
	 * @param mixed $options Options for the method and strategies.
	 * @return mixed Read results on successful cache read, null otherwise
	 * @filter This method may be filtered.
	 */
	public static function read($name, $key, array $options = array()) {
		$options += array('conditions' => null, 'strategies' => true, 'write' => null);
		$settings = static::config();

		if (!isset($settings[$name])) {
			return false;
		}
		
		$conditions = $options['conditions'];
		if (is_callable($conditions) && !$conditions()) {
			return false;
		}
		
		$key = static::key($key);
		$method = static::adapter($name)->read($key);
		$params = compact('key');
		$filters = $settings[$name]['filters'];
		$result = static::_filter(__FUNCTION__, $params, $method, $filters);
		
//		Bug Fix PT 2010-12-11
//		if ($result === null && $options['write']) {	// << delete
		if ($result === false && $options['write']) {	// << add
			$write = (is_callable($options['write'])) ? $options['write']() : $options['write'];
			list($expiry, $value) = each($write);

			static::write($name, $key, $value, $expiry);	// << add
			return $value;	// << add
//			return static::write($name, $key, $value, $expiry);	// << delete
		}

		if ($options['strategies']) {
			$options = array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
			$result = static::applyStrategies(__FUNCTION__, $name, $result, $options);
		}
		return $result;
	}


}}}

on 12.11.10 reported by: pault

Updates

on 12.16.10 by moos3
Tested this and your patch is a correct fix for this. Please see my commit in my fork commit : c011dda2c56f9ce16f2ca4c1fea340301559514e


Thanks.