From 9454927bd31a38bed6ea92f21f11d2c5b52fc297 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Jul 2011 08:04:29 -0700 Subject: [PATCH] Added Cache::maybe method. --- system/cache.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/system/cache.php b/system/cache.php index 12d67b54..91bb181f 100644 --- a/system/cache.php +++ b/system/cache.php @@ -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. *