签入版本
This commit is contained in:
@@ -1,31 +1,31 @@
|
||||
# 5.13 系统参数管理
|
||||
|
||||
:::tip 概述
|
||||
ParamService 管理系统级参数,如默认密码、系统名称等,采用两级缓存(请求级 + 持久缓存),大幅减少数据库查询。
|
||||
ParamService 管理系统级参数(存于 `laravel_param` 表),如默认密码、限流阈值等,采用两级缓存(请求级 + 持久缓存),大幅减少数据库查询。
|
||||
:::
|
||||
|
||||
## 缓存架构
|
||||
|
||||
```text
|
||||
请求级缓存(static::$cache) ← 同一请求内不重复查库
|
||||
请求级缓存(静态变量) ← 同一请求内不重复查库
|
||||
│ 未命中
|
||||
▼
|
||||
持久缓存(cache(),1小时) ← 跨请求复用
|
||||
持久缓存(cache(),1小时) ← 跨请求复用(Redis)
|
||||
│ 未命中
|
||||
▼
|
||||
数据库查询(param 表) ← 查询后写入两级缓存
|
||||
数据库查询(param 表) ← 查询后写入两级缓存
|
||||
```
|
||||
|
||||
## 核心方法
|
||||
|
||||
```php
|
||||
// app/service/ParamService.php
|
||||
// app/Services/ParamService.php
|
||||
|
||||
// 获取参数值(通用)
|
||||
$value = ParamService::get('DEFAULT_PASSWORD', '123456');
|
||||
|
||||
// 获取字符串参数
|
||||
$value = ParamService::getString('SITE_NAME', 'RXThinkCMF');
|
||||
$value = ParamService::getString('SITE_NAME', 'My CMS');
|
||||
|
||||
// 获取整数参数
|
||||
$value = ParamService::getInt('MAX_LOGIN_ATTEMPTS', 5);
|
||||
@@ -33,14 +33,9 @@ $value = ParamService::getInt('MAX_LOGIN_ATTEMPTS', 5);
|
||||
// 获取布尔参数
|
||||
$value = ParamService::getBool('ENABLE_SMS', false);
|
||||
|
||||
// 获取默认密码(从系统参数读取,默认 '123456')
|
||||
$password = ParamService::getDefaultPassword();
|
||||
|
||||
// 清除指定参数缓存
|
||||
// 清除指定参数缓存($code 为空时清空全部参数缓存)
|
||||
ParamService::clearCache('DEFAULT_PASSWORD');
|
||||
|
||||
// 清除所有参数缓存
|
||||
ParamService::clearAllCache();
|
||||
ParamService::clearCache();
|
||||
```
|
||||
|
||||
## 方法签名
|
||||
@@ -51,13 +46,14 @@ ParamService::clearAllCache();
|
||||
| `getString($code, $default)` | 获取字符串 | string |
|
||||
| `getInt($code, $default)` | 获取整数 | int |
|
||||
| `getBool($code, $default)` | 获取布尔 | bool |
|
||||
| `getDefaultPassword()` | 获取默认密码明文 | string |
|
||||
| `clearCache($code)` | 清除指定缓存 | void |
|
||||
| `clearAllCache()` | 清除所有缓存 | void |
|
||||
| `clearCache($code = '')` | 清除指定/全部缓存 | void |
|
||||
|
||||
**消费方示例**:`PasswordService::getDefaultPasswordPlain()` 即通过 `ParamService::get('DEFAULT_PASSWORD', '123456')` 读取默认密码(明文方法,切勿写入日志或返回前端)。
|
||||
|
||||
## 查询条件
|
||||
|
||||
仅查询满足以下条件的参数:
|
||||
|
||||
- `status = 1`(启用)
|
||||
- `is_delete = 0`(未删除)
|
||||
|
||||
@@ -67,10 +63,10 @@ ParamService::clearAllCache();
|
||||
|------|------|--------|------|
|
||||
| GET | `/api/param/page` | sys:param:page | 分页列表 |
|
||||
| GET | `/api/param/list` | sys:param:list | 全量列表 |
|
||||
| GET | `/api/param/detail/:id` | sys:param:detail | 详情 |
|
||||
| GET | `/api/param/detail/{id}` | sys:param:detail | 详情 |
|
||||
| POST | `/api/param/add` | sys:param:add | 新增 |
|
||||
| PUT | `/api/param/update` | sys:param:update | 修改 |
|
||||
| DELETE | `/api/param/delete/:id` | sys:param:delete | 删除 |
|
||||
| DELETE | `/api/param/delete/{id}` | sys:param:delete | 删除 |
|
||||
| DELETE | `/api/param/batchDelete` | sys:param:batchDelete | 批量删除 |
|
||||
|
||||
## 与配置管理的区别
|
||||
@@ -79,11 +75,27 @@ ParamService::clearAllCache();
|
||||
|------|------------------|-------------------|
|
||||
| 数据结构 | 独立的键值对 | 分组 + 配置项 |
|
||||
| 适用场景 | 系统级参数(默认密码、限流阈值) | 网站配置(名称、Logo、SMTP) |
|
||||
| 读取方式 | ParamService::get('code') | ConfigItemLogic 查询 |
|
||||
| 缓存 | 两级缓存 | 无特殊缓存 |
|
||||
| 读取方式 | `ParamService::get('code')` | `ConfigItem` 模型查询或 `/api/config/web/index` |
|
||||
| 服务封装 | `ParamService`(静态方法 + 缓存) | `ConfigService` |
|
||||
|
||||
## 常用系统参数
|
||||
|
||||
| 编码 | 说明 | 默认值 |
|
||||
|------|------|--------|
|
||||
| `DEFAULT_PASSWORD` | 默认密码 | 123456 |
|
||||
| `DEFAULT_PASSWORD` | 默认密码(重置密码时使用) | 123456 |
|
||||
|
||||
## 缓存管理
|
||||
|
||||
修改参数后缓存不会自动失效(参数无 Observer),需要手动刷新:
|
||||
|
||||
```bash
|
||||
# 修改参数后在代码中调用
|
||||
ParamService::clearCache('DEFAULT_PASSWORD');
|
||||
|
||||
# 或重启服务 / 清除应用缓存兜底
|
||||
php artisan cache:clear
|
||||
```
|
||||
|
||||
:::warning 修改参数后的生效方式
|
||||
参数写入数据库后,旧值仍可能存在缓存中(TTL 1 小时)。通过后台「参数管理」修改时建议同步调用 `clearCache()`;直接改库时用 `php artisan cache:clear` 兜底。
|
||||
:::
|
||||
|
||||
Reference in New Issue
Block a user