插件优化
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
yiqiu
2025-12-28 14:48:25 +08:00
parent 957ac89c4d
commit 91b0d4f1e4
4 changed files with 265 additions and 71 deletions

View File

@@ -1,6 +1,7 @@
<?php
namespace addon\theme_configurator\model;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Db;
@@ -10,6 +11,10 @@ use think\facade\Db;
class ThemeConfigModel
{
public const TABLE = 'addon_theme_configurator';
// 缓存配置
private const CACHE_KEY = 'theme_config_cache';
private const CACHE_TTL = 3600; // 缓存1小时
public static function tableName(): string
{
@@ -25,17 +30,31 @@ class ThemeConfigModel
}
/**
* 获取配置
* 获取配置 (带缓存)
*/
public function getConfig(): array
{
// 尝试从缓存获取
$config = Cache::get(self::CACHE_KEY);
if ($config !== false && $config !== null) {
// 缓存命中
return $config;
}
// 缓存未命中,从数据库读取
$row = $this->query()->order('id', 'asc')->find();
if (!$row) {
return $this->defaultConfig();
$config = $this->defaultConfig();
} else {
$config = json_decode($row['config'], true);
$config = is_array($config) ? $config : $this->defaultConfig();
}
$config = json_decode($row['config'], true);
return is_array($config) ? $config : $this->defaultConfig();
// 写入缓存
Cache::set(self::CACHE_KEY, $config, self::CACHE_TTL);
return $config;
}
/**
@@ -48,13 +67,17 @@ class ThemeConfigModel
'update_time' => time(),
];
$exists = $this->query()->order('id', 'asc')->find();
$exists = $this->query()->find();
if ($exists) {
$this->query()->where('id', $exists['id'])->update($payload);
} else {
$payload['create_time'] = time();
$this->query()->insert($payload);
}
// 清除缓存,确保下次读取最新数据
Cache::delete(self::CACHE_KEY);
return $config;
}