From 853303f104c576ddaaa2e645b7f4334bd2157053 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 9 Nov 2013 22:00:47 +0100 Subject: implement cache_function to easily cache return values of functions Signed-off-by: Florian Pritz --- application/helpers/filebin_helper.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'application/helpers') diff --git a/application/helpers/filebin_helper.php b/application/helpers/filebin_helper.php index 71ce7e6ca..e5637ce94 100644 --- a/application/helpers/filebin_helper.php +++ b/application/helpers/filebin_helper.php @@ -265,4 +265,22 @@ function stateful_client() return true; } +/** + * Cache the result of the function call + * @param key cache key to use + * @param ttl time to live for the cache entry + * @param function function to call + * @return return value of function (will be cached) + */ +function cache_function($key, $ttl, $function) +{ + $CI =& get_instance(); + $CI->load->driver('cache', array('adapter' => $CI->config->item("cache_backend"))); + if (! $content = $CI->cache->get($key)) { + $content = $function(); + $CI->cache->save($key, $content, $ttl); + } + return $content; +} + # vim: set noet: -- cgit v1.2.3-24-g4f1b