Skip to content

Commit

Permalink
refactor: 优化设置信息储存代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Linux committed Oct 7, 2024
1 parent af1f1e9 commit 24f7c3b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 70 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/V1/Admin/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
84 changes: 15 additions & 69 deletions app/Support/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

class Setting extends Fluent
{
const CACHE_KEY = 'admin_settings';
public function __construct()
{
$this->attributes = self::fromDatabase();
}

/**
* 获取配置,并转化为数组.
*
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -52,93 +53,38 @@ 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));
}

/**
* 保存配置到数据库.
*
* @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;
}

/**
* @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();
});

}
}

0 comments on commit 24f7c3b

Please sign in to comment.