Shorten code with Cache::remember

This commit is contained in:
slawkens 2025-01-09 15:50:33 +01:00
parent c52ca27126
commit 8e501c0e9c
2 changed files with 9 additions and 31 deletions

View File

@ -589,24 +589,12 @@ function template_form()
{ {
global $template_name; global $template_name;
$cache = Cache::getInstance(); $templates = Cache::remember('templates', 5 * 60, function() {
if($cache->enabled()) return get_templates();
{ });
$tmp = '';
if($cache->fetch('templates', $tmp)) {
$templates = unserialize($tmp);
}
else
{
$templates = get_templates();
$cache->set('templates', serialize($templates), 30);
}
}
else
$templates = get_templates();
$options = ''; $options = '';
foreach($templates as $key => $value) foreach($templates as $value)
$options .= '<option ' . ($template_name == $value ? 'SELECTED' : '') . '>' . $value . '</option>'; $options .= '<option ' . ($template_name == $value ? 'SELECTED' : '') . '>' . $value . '</option>';
global $twig; global $twig;

View File

@ -138,29 +138,19 @@ if($twig_loader) {
$twig_loader->prependPath(BASE . $template_path); $twig_loader->prependPath(BASE . $template_path);
} }
function get_template_menus() { function get_template_menus(): array
{
global $template_name; global $template_name;
$cache = Cache::getInstance(); $result = Cache::remember('template_menus', 10 * 60, function () use ($template_name) {
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('template_menus', $tmp)) {
$result = unserialize($tmp);
}
}
if (!isset($result)) {
$result = Menu::select(['name', 'link', 'blank', 'color', 'category']) $result = Menu::select(['name', 'link', 'blank', 'color', 'category'])
->where('template', $template_name) ->where('template', $template_name)
->orderBy('category') ->orderBy('category')
->orderBy('ordering') ->orderBy('ordering')
->get(); ->get();
if ($cache->enabled()) { return $result->toArray();
$cache->set('template_menus', serialize($result->toArray()), 600); });
}
}
$menus = array(); $menus = array();
foreach($result as $menu) { foreach($result as $menu) {