feature: Cache::remember($key, $ttl, $callback) + example usage

This commit is contained in:
slawkens
2023-02-18 08:53:42 +01:00
parent c1096415aa
commit 0002543cca
2 changed files with 22 additions and 4 deletions

View File

@@ -110,4 +110,21 @@ class Cache
* @return bool
*/
public function enabled() {return false;}
public static function remember($key, $ttl, $callback)
{
$cache = self::getInstance();
if(!$cache->enabled()) {
return $callback();
}
$value = null;
if ($cache->fetch($key, $value)) {
return unserialize($value);
}
$value = $callback();
$cache->set($key, serialize($value),$ttl);
return $value;
}
}