mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-27 01:39:22 +02:00
feature: Cache::remember($key, $ttl, $callback) + example usage
This commit is contained in:
parent
c1096415aa
commit
0002543cca
@ -110,4 +110,21 @@ class Cache
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function enabled() {return false;}
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,14 +101,15 @@ if(isset($_GET['image']))
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$images =
|
$images = Cache::remember('gallery_' . ($canEdit ? '1' : '0'), 60, function () use ($db, $canEdit) {
|
||||||
$db->query('SELECT `id`, `comment`, `image`, `author`, `thumb`' .
|
return $db->query('SELECT `id`, `comment`, `image`, `author`, `thumb`' .
|
||||||
($canEdit ? ', `hidden`, `ordering`' : '') .
|
($canEdit ? ', `hidden`, `ordering`' : '') .
|
||||||
' FROM `' . TABLE_PREFIX . 'gallery`' .
|
' FROM `' . TABLE_PREFIX . 'gallery`' .
|
||||||
(!$canEdit ? ' WHERE `hidden` != 1' : '') .
|
(!$canEdit ? ' WHERE `hidden` != 1' : '') .
|
||||||
' ORDER BY `ordering`;');
|
' ORDER BY `ordering`;')->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
});
|
||||||
|
|
||||||
$last = $images->rowCount();
|
$last = count($images);
|
||||||
if(!$last)
|
if(!$last)
|
||||||
{
|
{
|
||||||
?>
|
?>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user