Switch settings to be stored in a static property instead of cache.

This commit is contained in:
Matt Young 2025-06-28 12:18:00 -05:00
parent e14b678c74
commit 6f207edb0a
1 changed files with 20 additions and 13 deletions

View File

@ -3,10 +3,11 @@
namespace App; namespace App;
use App\Models\SiteSetting; use App\Models\SiteSetting;
use Illuminate\Support\Facades\Cache;
class Settings class Settings
{ {
public static $settings = null;
protected static $cacheKey = 'site_settings'; protected static $cacheKey = 'site_settings';
public static function __callStatic($key, $arguments) public static function __callStatic($key, $arguments)
@ -14,19 +15,23 @@ class Settings
return self::get($key); return self::get($key);
} }
// Load settings from the database and cache them // Load settings from the database
public static function loadSettings() public static function loadSettings()
{ {
$settings = SiteSetting::all()->pluck('setting_value', 'setting_key')->toArray(); if (self::$settings === null) {
Cache::put(self::$cacheKey, $settings, 3600); // Cache for 1 hour self::$settings = SiteSetting::all()->pluck('setting_value', 'setting_key')->toArray();
}
} }
// Get a setting value by key // Get a setting value by key
public static function get($key, $default = null) public static function get($key, $default = null)
{ {
$settings = Cache::get(self::$cacheKey, []); if (self::$settings === null) {
self::loadSettings();
}
return $settings[$key] ?? $default; return self::$settings[$key] ?? $default;
} }
// Set a setting value by key // Set a setting value by key
@ -35,15 +40,17 @@ class Settings
// Update the database // Update the database
SiteSetting::updateOrCreate(['setting_key' => $key], ['setting_value' => $value]); SiteSetting::updateOrCreate(['setting_key' => $key], ['setting_value' => $value]);
// Update the cache // Update the static property
$settings = Cache::get(self::$cacheKey, []); if (self::$settings === null) {
$settings[$key] = $value; self::loadSettings();
Cache::put(self::$cacheKey, $settings, 3600); // Cache for 1 hour }
self::$settings[$key] = $value;
} }
// Clear the cache // Clear the settings
public static function clearCache() public static function clearSettings()
{ {
Cache::forget(self::$cacheKey); self::$settings = null;
} }
} }