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;
}
}

View File

@ -101,14 +101,15 @@ if(isset($_GET['image']))
return;
}
$images =
$db->query('SELECT `id`, `comment`, `image`, `author`, `thumb`' .
$images = Cache::remember('gallery_' . ($canEdit ? '1' : '0'), 60, function () use ($db, $canEdit) {
return $db->query('SELECT `id`, `comment`, `image`, `author`, `thumb`' .
($canEdit ? ', `hidden`, `ordering`' : '') .
' FROM `' . TABLE_PREFIX . 'gallery`' .
(!$canEdit ? ' WHERE `hidden` != 1' : '') .
' ORDER BY `ordering`;');
' ORDER BY `ordering`;')->fetchAll(PDO::FETCH_ASSOC);
});
$last = $images->rowCount();
$last = count($images);
if(!$last)
{
?>