From cc29874a16e4c506c92cc9896a8dbc0b111134fb Mon Sep 17 00:00:00 2001 From: augushong Date: Wed, 20 Apr 2022 15:00:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=87=AA=E5=8A=A8=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=8A=9F=E8=83=BD=E9=9B=86=E6=88=90=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .example.env | 1 + app/admin/controller/Index.php | 1 + app/admin/model/SystemAdmin.php | 10 ++- app/admin/model/SystemNode.php | 4 +- app/admin/model/SystemQuick.php | 7 +- app/common/model/BaseModel.php | 100 +++++++++++++++++++++++++++++ app/common/model/TimeModel.php | 8 +-- app/common/provider/db/Query.php | 64 ++++++++++++++++++ app/common/service/AuthService.php | 26 ++++---- config/database.php | 74 +++++++++++---------- 10 files changed, 238 insertions(+), 57 deletions(-) create mode 100644 app/common/model/BaseModel.php create mode 100644 app/common/provider/db/Query.php diff --git a/.example.env b/.example.env index 77a5f36..7dd3876 100644 --- a/.example.env +++ b/.example.env @@ -3,6 +3,7 @@ COVER_THEME=index [APP] DEFAULT_TIMEZONE=Asia/Shanghai +AUTO_CACHE_LOG=false [DATABASE] TYPE=mysql diff --git a/app/admin/controller/Index.php b/app/admin/controller/Index.php index 4122d71..6b05f28 100644 --- a/app/admin/controller/Index.php +++ b/app/admin/controller/Index.php @@ -34,6 +34,7 @@ class Index extends AdminController $quicks = SystemQuick::field('id,title,icon,href') ->where(['status' => 1]) ->order('sort', 'desc') + ->autoCache('welcome_list') ->limit(8) ->select(); $this->assign('quicks', $quicks); diff --git a/app/admin/model/SystemAdmin.php b/app/admin/model/SystemAdmin.php index bf738e1..aa49258 100644 --- a/app/admin/model/SystemAdmin.php +++ b/app/admin/model/SystemAdmin.php @@ -20,6 +20,13 @@ class SystemAdmin extends TimeModel protected $deleteTime = 'delete_time'; + public static $autoCache = [ + [ + 'name' => 'info', + 'field' => 'id' + ] + ]; + public function getAuthList() { $list = (new SystemAuth()) @@ -27,5 +34,4 @@ class SystemAdmin extends TimeModel ->column('title', 'id'); return $list; } - -} \ No newline at end of file +} diff --git a/app/admin/model/SystemNode.php b/app/admin/model/SystemNode.php index 1cda933..86a3f4e 100644 --- a/app/admin/model/SystemNode.php +++ b/app/admin/model/SystemNode.php @@ -44,6 +44,4 @@ class SystemNode extends TimeModel } return $newList; } - - -} \ No newline at end of file +} diff --git a/app/admin/model/SystemQuick.php b/app/admin/model/SystemQuick.php index 4d68fdb..d2aa2b5 100644 --- a/app/admin/model/SystemQuick.php +++ b/app/admin/model/SystemQuick.php @@ -20,4 +20,9 @@ class SystemQuick extends TimeModel protected $deleteTime = 'delete_time'; -} \ No newline at end of file + public static $autoCache = [ + [ + 'name' => 'welcome_list' + ] + ]; +} diff --git a/app/common/model/BaseModel.php b/app/common/model/BaseModel.php new file mode 100644 index 0000000..c7fea05 --- /dev/null +++ b/app/common/model/BaseModel.php @@ -0,0 +1,100 @@ +'', + * 'type'=>'', // tag/key + * 'field'=>'' // 为空则不做拼接 + * ] + * + * @var array + */ + public static $autoCache = []; + + 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) + { + static::$autoCache[] = [ + 'name' => 'table', + 'type' => 'tag' + ]; + + static::$autoCache[] = [ + 'name' => 'read', + 'field' => 'id' + ]; + + foreach (static::$autoCache as $cache_item) { + $type = $cache_item['type'] ?? 'key'; + + $field = $cache_item['field'] ?? ''; + + $cache_key = $cache_item['name'] ?? ''; + + if (empty($cache_key)) { + continue; + } + + $cache_key = Str::snake($model->getName()) . '_' . $cache_key; + + + if (!empty($field) && !is_null($model->$field)) { + // 用字段新的值拼接key + $cache_key_attr = $cache_key . '_' . $model->getAttr($field); + + static::doRemoveCache($type, $cache_key_attr); + + // 用字段旧的值拼接key + $cache_key_original = $cache_key . '_' . $model->getOrigin($field); + + if ($cache_key_original != $cache_key_attr) { + static::doRemoveCache($type, $cache_key_original); + } + } else { + static::doRemoveCache($type, $cache_key); + } + } + } + + protected static function doRemoveCache($type, $cache_key) + { + + if (Env::get('app.auto_cache_log')) { + Log::debug("清除 ORM 自动缓存 {$type}:{$cache_key}"); + } + + if ($type == 'key') { + Cache::delete($cache_key); + } else { + Cache::tag($cache_key)->clear(); + } + } + + public static function getClassNameKey() + { + return str_replace('\\', '_', static::class); + } +} diff --git a/app/common/model/TimeModel.php b/app/common/model/TimeModel.php index 89973d1..6cba712 100644 --- a/app/common/model/TimeModel.php +++ b/app/common/model/TimeModel.php @@ -14,7 +14,6 @@ namespace app\common\model; -use think\Model; use think\model\concern\SoftDelete; /** @@ -22,7 +21,7 @@ use think\model\concern\SoftDelete; * Class TimeModel * @package app\common\model */ -class TimeModel extends Model +class TimeModel extends BaseModel { /** @@ -47,9 +46,8 @@ class TimeModel extends Model * 软删除 */ use SoftDelete; - + protected $deleteTime = 'delete_time'; protected $defaultSoftDelete = 0; - -} \ No newline at end of file +} diff --git a/app/common/provider/db/Query.php b/app/common/provider/db/Query.php new file mode 100644 index 0000000..d928155 --- /dev/null +++ b/app/common/provider/db/Query.php @@ -0,0 +1,64 @@ +getName()); + + if (is_null($key)) { + $key = $this->getOptionsMd5(); + } + + if (!is_null($field_key_value)) { + $key = $key . '_' . $field_key_value; + } + $key = $table_name . '_' . $key; + + if (!is_null($tag)) { + + if (!is_null($field_key_value)) { + $tag = $tag . '_' . $field_tag_value; + } + + $tag = $table_name . '_' . $tag; + } + + if(Env::get('app.auto_cache_log')){ + Log::debug('use auto cache:' . $key); + Log::debug('use auto cache tag:' . $tag); + } + + $this->cache($key, null, $tag); + + return $this; + } + + public function getOptionsMd5() + { + $options = $this->getOptions(); + + // TODO:支持获取回调函数的设置 + + return md5(json_encode($options)); + } +} diff --git a/app/common/service/AuthService.php b/app/common/service/AuthService.php index 07ab03e..76d7287 100644 --- a/app/common/service/AuthService.php +++ b/app/common/service/AuthService.php @@ -39,7 +39,7 @@ class AuthService 'system_admin' => 'system_admin', // 用户表 'system_auth' => 'system_auth', // 权限表 'system_node' => 'system_node', // 节点表 - 'system_auth_node' => 'system_auth_node',// 权限-节点表 + 'system_auth_node' => 'system_auth_node', // 权限-节点表 ]; /** @@ -140,11 +140,12 @@ class AuthService public function getAdminNode() { $nodeList = []; - $adminInfo = Db::name($this->config['system_admin']) - ->where([ - 'id' => $this->adminId, - 'status' => 1, - ])->find(); + $adminInfo = $this->getAdminInfo(); + + if ($adminInfo['status'] != 1) { + return $nodeList; + } + if (!empty($adminInfo) && !empty($adminInfo['auth_ids'])) { $buildAuthSql = Db::name($this->config['system_auth']) ->distinct(true) @@ -170,9 +171,11 @@ class AuthService * @return array * @author zhongshaofa */ - public function getNodeList(){ + public function getNodeList() + { return Db::name($this->config['system_node']) - ->column('id,node,title,type,is_auth','node'); + ->autoCache(null, null, 'table') + ->column('id,node,title,type,is_auth', 'node'); } /** @@ -184,9 +187,11 @@ class AuthService * @throws \think\db\exception\ModelNotFoundException * @author zhongshaofa */ - public function getAdminInfo(){ + public function getAdminInfo() + { return Db::name($this->config['system_admin']) ->where('id', $this->adminId) + ->autoCache('info', $this->adminId) ->find(); } @@ -211,5 +216,4 @@ class AuthService $node = implode('/', $array); return $node; } - -} \ No newline at end of file +} diff --git a/config/database.php b/config/database.php index ecb8fac..181d6f7 100644 --- a/config/database.php +++ b/config/database.php @@ -1,5 +1,6 @@ Env::get('database.fields_cache', false), + 'query' => Query::class ], 'sqlite' => [ // 数据库类型 - 'type' => 'sqlite', - - // 服务器地址 - 'hostname' => Env::get('root_path').'ul.db', - // 数据库名 - 'database' => App::getRootPath().'ul.db', - // 用户名 - '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_'), - // 数据库调试模式 - 'debug' => Env::get('database.debug', true), - // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) - 'deploy' => 0, - // 数据库读写是否分离 主从式有效 - 'rw_separate' => false, - // 读写分离后 主服务器数量 - 'master_num' => 1, - // 指定从服务器序号 - 'slave_no' => '', - // 是否严格检查字段是否存在 - 'fields_strict' => true, - // 是否需要进行SQL性能分析 - 'sql_explain' => false, - // 是否需要断线重连 - 'break_reconnect' => false, - ], + 'type' => 'sqlite', + + // 服务器地址 + 'hostname' => Env::get('root_path') . 'ul.db', + // 数据库名 + 'database' => App::getRootPath() . 'ul.db', + // 用户名 + '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_'), + // 数据库调试模式 + 'debug' => Env::get('database.debug', true), + // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) + 'deploy' => 0, + // 数据库读写是否分离 主从式有效 + 'rw_separate' => false, + // 读写分离后 主服务器数量 + 'master_num' => 1, + // 指定从服务器序号 + 'slave_no' => '', + // 是否严格检查字段是否存在 + 'fields_strict' => true, + // 是否需要进行SQL性能分析 + 'sql_explain' => false, + // 是否需要断线重连 + 'break_reconnect' => false, + + 'query' => Query::class + ], // 更多的数据库配置信息