Skip to content

Commit

Permalink
Optimize cache hits (#16)
Browse files Browse the repository at this point in the history
* Optimize cache hits
  • Loading branch information
stepanenko3 authored Mar 31, 2024
1 parent 03ee8f1 commit cac3bda
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
4 changes: 4 additions & 0 deletions config/nova-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
'types' => [
\Stepanenko3\NovaSettings\Types\General::class,
],

'storage' => [],

'cache_lifetime' => 3600,
];
32 changes: 22 additions & 10 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;

if (!function_exists('settings')) {
function settings(
Expand All @@ -13,16 +14,27 @@ function settings(
$env = config('app.env');
}

$settings = Cache::remember(
key: 'settings.' . $section . '.' . $env,
ttl: config('cache.lifetime'),
callback: fn () => config('nova-settings.model')::query()
->select('settings')
->where('slug', $section)
->where('env', $env)
->first()
->settings ?? [],
);
$configKey = 'nova-settings.storage.' . $section . '.' . $env;

if ($value = config($configKey)) {
$settings = json_decode($value, true);
} else {
$settings = Cache::remember(
key: 'settings.' . $section . '.' . $env,
ttl: config('nova-settings.cache_lifetime', 3600),
callback: fn () => config('nova-settings.model')::query()
->select('settings')
->where('slug', $section)
->where('env', $env)
->first()
->settings ?? [],
);

Config::set(
key: $configKey,
value: json_encode($settings),
);
}

if ($key === null) {
return $settings;
Expand Down

0 comments on commit cac3bda

Please sign in to comment.