From 24f7c3beeae50d07462ecd993d9a2a3c0b9642a1 Mon Sep 17 00:00:00 2001 From: Linux Date: Tue, 8 Oct 2024 01:30:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=BF=A1=E6=81=AF=E5=82=A8=E5=AD=98=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/V1/Admin/ConfigController.php | 1 - app/Support/Setting.php | 84 ++++--------------- 2 files changed, 15 insertions(+), 70 deletions(-) diff --git a/app/Http/Controllers/V1/Admin/ConfigController.php b/app/Http/Controllers/V1/Admin/ConfigController.php index ae5c95c7c..239afc08f 100755 --- a/app/Http/Controllers/V1/Admin/ConfigController.php +++ b/app/Http/Controllers/V1/Admin/ConfigController.php @@ -192,7 +192,6 @@ public function save(ConfigSave $request) if(isset(get_defined_constants(true)['user']['Workerman'])){ posix_kill(posix_getppid(), SIGUSR1); } - Cache::forget('admin_settings'); // \Artisan::call('horizon:terminate'); //重启队列使配置生效 return $this->success(true); } diff --git a/app/Support/Setting.php b/app/Support/Setting.php index fa84b5c92..85ae5f581 100644 --- a/app/Support/Setting.php +++ b/app/Support/Setting.php @@ -10,10 +10,11 @@ class Setting extends Fluent { + const CACHE_KEY = 'admin_settings'; public function __construct() { - $this->attributes = self::fromDatabase(); } + /** * 获取配置,并转化为数组. * @@ -41,7 +42,7 @@ public function getArray($key, $default = []) */ public function get($key, $default = null) { - return Arr::get($this->attributes, $key, $default); + return Arr::get($this->fromDatabase(), $key, $default); } /** @@ -52,49 +53,14 @@ public function get($key, $default = null) */ public function set($key, $value = null) { - $data = is_array($key) ? $key : [$key => $value]; - - foreach ($data as $key => $value) { - Arr::set($this->attributes, $key, $value); + if (is_array($value)) { + $value = json_encode($value); } - + SettingModel::updateOrCreate(['name' => $key], ['value' => $value]); + Cache::forget(self::CACHE_KEY); return $this; } - /** - * 追加数据. - * - * @param mixed $key - * @param mixed $value - * @param mixed $k - * @return $this - */ - public function add($key, $value, $k = null) - { - $results = $this->getArray($key); - - if ($k !== null) { - $results[] = $value; - } else { - $results[$k] = $value; - } - - return $this->set($key, $results); - } - - /** - * 批量追加数据. - * - * @param string $key - * @param array $value - * @return $this - */ - public function addMany($key, array $value) - { - $results = $this->getArray($key); - - return $this->set($key, array_merge($results, $value)); - } /** * 保存配置到数据库. @@ -102,27 +68,11 @@ public function addMany($key, array $value) * @param array $data * @return $this */ - public function save(array $data = []) + public function save(array $data = []): static { - if ($data) { - $this->set($data); - } - - foreach ($this->attributes as $key => $value) { - if (is_array($value)) { - $value = json_encode($value); - } - - $model = SettingModel::query() - ->where('name', $key) - ->first() ?: new SettingModel(); - - $model->fill([ - 'name' => $key, - 'value' => (string) $value, - ])->save(); + foreach ($data as $key => $value) { + $this->set($key, $value); } - Cache::forget('admin_settings'); return $this; } @@ -130,15 +80,11 @@ public function save(array $data = []) /** * @return static */ - public static function fromDatabase() + public static function fromDatabase(): array { - $values = []; - try { - $values = Cache::remember('admin_settings', env('ADMIN_SETTING_CACHE', 0), function () { - return SettingModel::pluck('value', 'name')->toArray(); - }); - } catch (QueryException $e) { - } - return $values; + return Cache::rememberForever(self::CACHE_KEY, function () { + return SettingModel::pluck('value', 'name')->toArray(); + }); + } }