Skip to content

Commit

Permalink
Shorten code with Cache::remember
Browse files Browse the repository at this point in the history
  • Loading branch information
slawkens committed Jan 9, 2025
1 parent c52ca27 commit 8e501c0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
20 changes: 4 additions & 16 deletions system/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 5 additions & 15 deletions system/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,19 @@
$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) {
Expand Down

0 comments on commit 8e501c0

Please sign in to comment.