From 80f5b7cf27756f5f7d62c1b367e30c1cbea0e2d0 Mon Sep 17 00:00:00 2001 From: augushong Date: Mon, 14 Feb 2022 22:54:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BD=91=E7=AB=99=E7=BC=93?= =?UTF-8?q?=E5=AD=98;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/traits/AutoClearCache.php | 5 +- app/index/controller/Common.php | 12 +- app/model/Category.php | 209 ++++++++++++++------------- app/model/Nav.php | 4 +- config/database.php | 12 +- 5 files changed, 129 insertions(+), 113 deletions(-) diff --git a/app/common/traits/AutoClearCache.php b/app/common/traits/AutoClearCache.php index f85af9f..5911267 100644 --- a/app/common/traits/AutoClearCache.php +++ b/app/common/traits/AutoClearCache.php @@ -43,15 +43,18 @@ trait AutoClearCache $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; + $cache_key = $cache_key . '_' . $model->getAttr($field); } } diff --git a/app/index/controller/Common.php b/app/index/controller/Common.php index b83fd06..5163b2c 100644 --- a/app/index/controller/Common.php +++ b/app/index/controller/Common.php @@ -18,22 +18,22 @@ class Common extends BaseController parent::initialize(); - $list_nav_slide = Nav::where('type', 3)->cache('type_3_list')->order('sort asc')->where('status', 1)->select(); + $list_nav_slide = Nav::where('type', 3)->cache('type_list_3')->order('sort asc')->where('status', 1)->select(); View::assign('list_nav_slide', $list_nav_slide); - $list_nav_friend_url = Nav::where('type', 2)->cache('type_2_list')->order('sort asc')->where('status', 1)->select(); + $list_nav_friend_url = Nav::where('type', 2)->cache('type_list_2')->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(); + $list_header_nav = Nav::where('type', 11)->cache('type_list_11')->order('sort asc')->where('status', 1)->select(); View::assign('list_header_nav', $list_header_nav); - $list_category_first_level = Category::where('level', 1)->where('status', 1)->where('type', 3)->order('sort asc')->select(); + $list_category_first_level = Category::where('level', 1)->where('status', 1)->where('type', 3)->cache('category_type_list_3')->order('sort asc')->select(); View::assign('list_category_first_level', $list_category_first_level); - $list_nav_more = Nav::where('type', 8)->order('sort asc')->where('status', 1)->select(); + $list_nav_more = Nav::where('type', 8)->cache('type_list_8')->order('sort asc')->where('status', 1)->select(); View::assign('list_nav_more', $list_nav_more); - $top_posts = Post::where('is_top', 1)->limit(8)->where('type', 3)->select(); + $top_posts = Post::where('is_top', 1)->limit(8)->where('type', 3)->cache('top_post')->select(); View::assign('top_posts', $top_posts); $this->userHubLogin(); diff --git a/app/model/Category.php b/app/model/Category.php index 8ef5cb3..e4b6a33 100644 --- a/app/model/Category.php +++ b/app/model/Category.php @@ -4,128 +4,137 @@ declare(strict_types=1); namespace app\model; +use app\common\model\Base; use think\facade\Config; use think\Model; /** * @mixin think\Model */ -class Category extends Model +class Category extends Base { - // - - public static $allCategory = []; + // - /** - * 获取指定id下的所有分类 - * - * @param string $id - * @return void - */ - public static function getListLevel($id = '',$type = 1) - { + public static $autoClearCache = [ + [ + 'name' => 'category_type_list', + 'field' => 'type' + ] + ]; - if(empty(self::$allCategory)){ - $model_list = Category::where('type',$type)->order('sort asc')->select(); - self::$allCategory = array2level($model_list,0,0); - } + public static $allCategory = []; - if(!empty($id)){ - $list = []; - $in_category = [$id]; - foreach (self::$allCategory as $category) { - if(in_array($category->pid,$in_category)){ - $list[] = $category; - $in_category[] = $category->id; + + /** + * 获取指定id下的所有分类 + * + * @param string $id + * @return void + */ + public static function getListLevel($id = '', $type = 1) + { + + if (empty(self::$allCategory)) { + + $model_list = Category::where('type', $type)->order('sort asc')->select(); + self::$allCategory = array2level($model_list, 0, 0); } - } - return $list; + if (!empty($id)) { + $list = []; + $in_category = [$id]; + foreach (self::$allCategory as $category) { + if (in_array($category->pid, $in_category)) { + $list[] = $category; + $in_category[] = $category->id; + } + } + + return $list; + } + + return self::$allCategory; } - return self::$allCategory; - } + public function getTitleImgAttr($value) + { - public function getTitleImgAttr($value) - { - - return get_source_link($value); - } - - public function posts() - { - return $this->hasMany(PostCategory::class,'category_id'); - } - - /** - * 返回的对应的post的模型 - * - * @return void - */ - public function getPostsModelListAttr() - { - $list_post_category = $this->getAttr('posts'); - - $list_post = []; - - foreach ($list_post_category as $list_post_category) { - array_push($list_post,$list_post_category->post); + return get_source_link($value); } - return $list_post; - } - - /** - * 返回的对应post的数据,性能比模型要高. - * - * @return void - */ - public function getPostsListAttr() - { - $list_post_category = $this->getAttr('posts'); - - $list_post = array_column($list_post_category->append(['post'])->toArray(),'post'); - - return $list_post; - } - - public function getTplNameAttr($value) - { - return Config::get('view_type.category.'.$value); - } - - public function getModelParentAttr() - { - $pid = $this->getData('pid'); - - if($pid == 0){ - return $this; + public function posts() + { + return $this->hasMany(PostCategory::class, 'category_id'); } - return Category::where('id',$pid)->find(); - } - // 返回除自身以外的其他的同级同类的分类 - public function getModelSiblingsAttr() - { - return Category::where('pid',$this->getData('pid')) - ->where('level',$this->getData('level')) - ->where('id','<>',$this->getData('id')) - ->select(); - } + /** + * 返回的对应的post的模型 + * + * @return void + */ + public function getPostsModelListAttr() + { + $list_post_category = $this->getAttr('posts'); - /** - * 获取同一个父元素的分类,包含自身 - * - * @return void - */ - public function getModelSameParentAttr() - { - return Category::where('pid',$this->getData('pid')) - ->where('level',$this->getData('level')) - ->select(); - } + $list_post = []; + foreach ($list_post_category as $list_post_category) { + array_push($list_post, $list_post_category->post); + } + + return $list_post; + } + + /** + * 返回的对应post的数据,性能比模型要高. + * + * @return void + */ + public function getPostsListAttr() + { + $list_post_category = $this->getAttr('posts'); + + $list_post = array_column($list_post_category->append(['post'])->toArray(), 'post'); + + return $list_post; + } + + public function getTplNameAttr($value) + { + return Config::get('view_type.category.' . $value); + } + + public function getModelParentAttr() + { + $pid = $this->getData('pid'); + + if ($pid == 0) { + return $this; + } + return Category::where('id', $pid)->find(); + } + + // 返回除自身以外的其他的同级同类的分类 + public function getModelSiblingsAttr() + { + return Category::where('pid', $this->getData('pid')) + ->where('level', $this->getData('level')) + ->where('id', '<>', $this->getData('id')) + ->select(); + } + + /** + * 获取同一个父元素的分类,包含自身 + * + * @return void + */ + public function getModelSameParentAttr() + { + return Category::where('pid', $this->getData('pid')) + ->where('level', $this->getData('level')) + ->select(); + } } diff --git a/app/model/Nav.php b/app/model/Nav.php index 9be23bd..133c159 100644 --- a/app/model/Nav.php +++ b/app/model/Nav.php @@ -15,7 +15,9 @@ class Nav extends Base public static $autoClearCache = [ [ - 'name' => 'type_3_list' + 'type' => 'key', + 'name' => 'type_list', + 'field' => 'type' ] ]; diff --git a/config/database.php b/config/database.php index 9cd91b5..25a6469 100644 --- a/config/database.php +++ b/config/database.php @@ -1,4 +1,5 @@ true, // 开启字段缓存 'fields_cache' => true, - + ], 'sqlite' => [ - // 数据库类型 + // 数据库类型 'type' => 'sqlite', - + // 服务器地址 - 'hostname' => Env::get('root_path').'ul.db', + 'hostname' => Env::get('root_path') . 'ul.db', // 数据库名 - 'database' => App::getRootPath().'ul.db', + 'database' => App::getRootPath() . 'ul.db', // 用户名 'username' => Env::get('database.username', ''), // 密码 @@ -94,6 +95,7 @@ return [ 'sql_explain' => false, // 是否需要断线重连 'break_reconnect' => false, + 'fields_cache' => true ], // 更多的数据库配置信息 ],