Refactor cache class. Fix driver instantiation bug. Allow closures as default parameter.
This commit is contained in:
parent
715bed748d
commit
aaae1acb34
|
@ -17,19 +17,39 @@ class Cache {
|
||||||
*/
|
*/
|
||||||
public static function driver($driver = null)
|
public static function driver($driver = null)
|
||||||
{
|
{
|
||||||
|
if (is_null($driver))
|
||||||
|
{
|
||||||
|
$driver = Config::get('cache.driver');
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! array_key_exists($driver, static::$drivers))
|
if ( ! array_key_exists($driver, static::$drivers))
|
||||||
{
|
{
|
||||||
if (is_null($driver))
|
|
||||||
{
|
|
||||||
$driver = Config::get('cache.driver');
|
|
||||||
}
|
|
||||||
|
|
||||||
static::$drivers[$driver] = Cache\Factory::make($driver);
|
static::$drivers[$driver] = Cache\Factory::make($driver);
|
||||||
}
|
}
|
||||||
|
|
||||||
return static::$drivers[$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.
|
* Pass all other methods to the default driver.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue