From aaae1acb34462ad1fcbc153a24423d1f454e931b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Jul 2011 07:38:59 -0700 Subject: [PATCH] Refactor cache class. Fix driver instantiation bug. Allow closures as default parameter. --- system/cache.php | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/system/cache.php b/system/cache.php index 7a6e55a1..12d67b54 100644 --- a/system/cache.php +++ b/system/cache.php @@ -17,19 +17,39 @@ class Cache { */ public static function driver($driver = null) { + if (is_null($driver)) + { + $driver = Config::get('cache.driver'); + } + if ( ! array_key_exists($driver, static::$drivers)) { - if (is_null($driver)) - { - $driver = Config::get('cache.driver'); - } - 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. *