From 0002543cca1ac01573ee24175c7441600b3448e3 Mon Sep 17 00:00:00 2001 From: slawkens Date: Sat, 18 Feb 2023 08:53:42 +0100 Subject: [PATCH] feature: Cache::remember($key, $ttl, $callback) + example usage --- system/libs/cache.php | 17 +++++++++++++++++ system/pages/gallery.php | 9 +++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/system/libs/cache.php b/system/libs/cache.php index 880c33c7..55c17a17 100644 --- a/system/libs/cache.php +++ b/system/libs/cache.php @@ -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; + } } diff --git a/system/pages/gallery.php b/system/pages/gallery.php index 8cbf3975..c893ce90 100644 --- a/system/pages/gallery.php +++ b/system/pages/gallery.php @@ -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) { ?>