Added Cache::maybe method.

This commit is contained in:
Taylor Otwell 2011-07-07 08:04:29 -07:00
parent 1d96b96828
commit 9454927bd3
1 changed files with 24 additions and 0 deletions

View File

@ -50,6 +50,30 @@ public static function get($key, $default = null, $driver = null)
return $item;
}
/**
* Get an item from the cache. If it doesn't exist, store the default value
* in the cache and return it.
*
* @param string $key
* @param mixed $default
* @param int $minutes
* @param string $driver
* @return mixed
*/
public static function maybe($key, $default, $minutes, $driver = null)
{
if ( ! is_null($item = static::get($key)))
{
return $item;
}
$default = is_callable($default) ? call_user_func($default) : $default;
static::driver($driver)->put($key, $default, $minutes);
return $default;
}
/**
* Pass all other methods to the default driver.
*