Refactor cache class. Fix driver instantiation bug. Allow closures as default parameter.
This commit is contained in:
parent
715bed748d
commit
aaae1acb34
|
@ -16,20 +16,40 @@ class Cache {
|
|||
* @return Cache\Driver
|
||||
*/
|
||||
public static function driver($driver = null)
|
||||
{
|
||||
if ( ! array_key_exists($driver, static::$drivers))
|
||||
{
|
||||
if (is_null($driver))
|
||||
{
|
||||
$driver = Config::get('cache.driver');
|
||||
}
|
||||
|
||||
if ( ! array_key_exists($driver, static::$drivers))
|
||||
{
|
||||
static::$drivers[$driver] = Cache\Factory::make($driver);
|
||||
}
|
||||
|
||||
return static::$drivers[$driver];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from the cache.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @param string $driver
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($key, $default = null, $driver = null)
|
||||
{
|
||||
$item = static::driver($driver)->get($key);
|
||||
|
||||
if (is_null($item))
|
||||
{
|
||||
return is_callable($default) ? call_user_func($default) : $default;
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass all other methods to the default driver.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue