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

View File

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