增加自动清除缓存特性;

This commit is contained in:
2022-02-12 17:02:45 +08:00
parent 90bbf61b6a
commit 7d8b948543
7 changed files with 260 additions and 154 deletions

View File

@@ -18,6 +18,7 @@ use think\exception\ValidateException;
use think\Validate;
use think\facade\View;
use think\exception\HttpResponseException;
use think\facade\Log;
/**
* 控制器基础类
@@ -65,6 +66,7 @@ abstract class BaseController
// 初始化
protected function initialize()
{
Log::debug('request url :'.$this->request->url());
}
/**

10
app/common/model/Base.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace app\common\model;
use app\common\traits\AutoClearCache;
use think\Model;
class Base extends Model
{
// use AutoClearCache;
}

View File

@@ -0,0 +1,65 @@
<?php
namespace app\common\traits;
use think\facade\Cache;
/**
*
*/
trait AutoClearCache
{
/**
* 自动清除的缓存值
*
* [
* 'name'=>'',
* 'type'=>'', // tag/key
* 'field'=>'' // 为空则不做拼接
* ]
*
* @var array
*/
public static $autoClearCache = [];
public static function onAfterWrite($model)
{
static::autoRemoveCache($model);
}
public static function onAfterDelete($model)
{
static::autoRemoveCache($model);
}
public static function onAfterRestore($model)
{
static::autoRemoveCache($model);
}
public static function autoRemoveCache($model)
{
foreach (static::$autoClearCache as $cache_item) {
$type = $cache_item['type'] ?: 'key';
$field = $cache_item['field'] ?: '';
$cache_key = $cache_item['name'] ?: '';
if (empty($cache_key)) {
continue;
}
if (!empty($field)) {
if (!is_null($model->$field)) {
$cache_key = $cache_key . '_' . $model->$field;
}
}
if ($type == 'key') {
Cache::delete($type);
} else {
Cache::tag($cache_key)->clear();
}
}
}
}

View File

@@ -17,9 +17,11 @@ class Common extends BaseController
{
parent::initialize();
$list_nav_slide = Nav::where('type', 3)->order('sort asc')->where('status', 1)->select();
$list_nav_slide = Nav::where('type', 3)->cache('type_3_list')->order('sort asc')->where('status', 1)->select();
View::assign('list_nav_slide', $list_nav_slide);
$list_nav_friend_url = Nav::where('type', 2)->order('sort asc')->where('status', 1)->select();
$list_nav_friend_url = Nav::where('type', 2)->cache('type_2_list')->order('sort asc')->where('status', 1)->select();
View::assign('list_nav_friend_url', $list_nav_friend_url);
$list_header_nav = Nav::where('type', 11)->order('sort asc')->where('status', 1)->select();

View File

@@ -12,5 +12,5 @@ return [
// 页面Trace调试
// \think\middleware\TraceDebug::class,
'\app\\middleware\ConfigInit'
// '\app\\middleware\ConfigInit'
];

View File

@@ -4,14 +4,21 @@ declare(strict_types=1);
namespace app\model;
use think\Model;
use app\common\model\Base;
/**
* @mixin think\Model
*/
class Nav extends Model
class Nav extends Base
{
public static $autoClearCache = [
[
'name' => 'type_3_list'
]
];
public static $statusName = [
0 => '不显示',
1 => '显示'

View File

@@ -6,7 +6,7 @@ use think\facade\Env;
// +----------------------------------------------------------------------
return [
// 默认日志记录通道
'default' => Env::get('log.channel', 'file'),
'default' => Env::get('log.channel', 'debug_mysql'),
// 日志记录级别
'level' => [],
// 日志类型记录的通道 ['error'=>'email',...]
@@ -41,6 +41,26 @@ return [
'realtime_write' => false,
],
// 其它日志通道配置
// 其它日志通道配置
'debug_mysql' => [
'type' => 'DebugMysql',
// 服务器地址
'hostname' => Env::get('database.hostname', ''),
// 数据库名
'database' => Env::get('database.database', ''),
// 用户名
'username' => Env::get('database.username', ''),
// 密码
'password' => Env::get('database.password', ''),
// 端口
'hostport' => Env::get('database.hostport', '3306'),
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => Env::get('database.charset', 'utf8'),
// 数据库表前缀
'prefix' => Env::get('database.prefix', 'ul_'),
]
],
];