Switch settings to be stored in a static property instead of cache.
This commit is contained in:
parent
e14b678c74
commit
6f207edb0a
|
|
@ -3,10 +3,11 @@
|
|||
namespace App;
|
||||
|
||||
use App\Models\SiteSetting;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class Settings
|
||||
{
|
||||
public static $settings = null;
|
||||
|
||||
protected static $cacheKey = 'site_settings';
|
||||
|
||||
public static function __callStatic($key, $arguments)
|
||||
|
|
@ -14,19 +15,23 @@ class Settings
|
|||
return self::get($key);
|
||||
}
|
||||
|
||||
// Load settings from the database and cache them
|
||||
// Load settings from the database
|
||||
public static function loadSettings()
|
||||
{
|
||||
$settings = SiteSetting::all()->pluck('setting_value', 'setting_key')->toArray();
|
||||
Cache::put(self::$cacheKey, $settings, 3600); // Cache for 1 hour
|
||||
if (self::$settings === null) {
|
||||
self::$settings = SiteSetting::all()->pluck('setting_value', 'setting_key')->toArray();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Get a setting value by key
|
||||
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
|
||||
|
|
@ -35,15 +40,17 @@ class Settings
|
|||
// Update the database
|
||||
SiteSetting::updateOrCreate(['setting_key' => $key], ['setting_value' => $value]);
|
||||
|
||||
// Update the cache
|
||||
$settings = Cache::get(self::$cacheKey, []);
|
||||
$settings[$key] = $value;
|
||||
Cache::put(self::$cacheKey, $settings, 3600); // Cache for 1 hour
|
||||
// Update the static property
|
||||
if (self::$settings === null) {
|
||||
self::loadSettings();
|
||||
}
|
||||
self::$settings[$key] = $value;
|
||||
|
||||
}
|
||||
|
||||
// Clear the cache
|
||||
public static function clearCache()
|
||||
// Clear the settings
|
||||
public static function clearSettings()
|
||||
{
|
||||
Cache::forget(self::$cacheKey);
|
||||
self::$settings = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue