优化网站缓存;

This commit is contained in:
2022-02-14 22:54:35 +08:00
parent c4ac0764b9
commit 80f5b7cf27
5 changed files with 129 additions and 113 deletions

View File

@@ -43,15 +43,18 @@ trait AutoClearCache
$field = $cache_item['field'] ?? ''; $field = $cache_item['field'] ?? '';
$cache_key = $cache_item['name'] ?? ''; $cache_key = $cache_item['name'] ?? '';
if (empty($cache_key)) { if (empty($cache_key)) {
continue; continue;
} }
if (!empty($field)) { if (!empty($field)) {
if (!is_null($model->$field)) { if (!is_null($model->$field)) {
$cache_key = $cache_key . '_' . $model->$field; $cache_key = $cache_key . '_' . $model->getAttr($field);
} }
} }

View File

@@ -18,22 +18,22 @@ class Common extends BaseController
parent::initialize(); 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); 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); 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); 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); 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); 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); View::assign('top_posts', $top_posts);
$this->userHubLogin(); $this->userHubLogin();

View File

@@ -4,128 +4,137 @@ declare(strict_types=1);
namespace app\model; namespace app\model;
use app\common\model\Base;
use think\facade\Config; use think\facade\Config;
use think\Model; use think\Model;
/** /**
* @mixin think\Model * @mixin think\Model
*/ */
class Category extends Model class Category extends Base
{ {
// //
public static $allCategory = [];
/** public static $autoClearCache = [
* 获取指定id下的所有分类 [
* 'name' => 'category_type_list',
* @param string $id 'field' => 'type'
* @return void ]
*/ ];
public static function getListLevel($id = '',$type = 1)
{
if(empty(self::$allCategory)){
$model_list = Category::where('type',$type)->order('sort asc')->select(); public static $allCategory = [];
self::$allCategory = array2level($model_list,0,0);
}
if(!empty($id)){
$list = []; /**
$in_category = [$id]; * 获取指定id下的所有分类
foreach (self::$allCategory as $category) { *
if(in_array($category->pid,$in_category)){ * @param string $id
$list[] = $category; * @return void
$in_category[] = $category->id; */
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);
{
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 $list_post; public function posts()
} {
return $this->hasMany(PostCategory::class, 'category_id');
/**
* 返回的对应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() * 返回的对应的post的模型
{ *
return Category::where('pid',$this->getData('pid')) * @return void
->where('level',$this->getData('level')) */
->where('id','<>',$this->getData('id')) public function getPostsModelListAttr()
->select(); {
} $list_post_category = $this->getAttr('posts');
/** $list_post = [];
* 获取同一个父元素的分类,包含自身
*
* @return void
*/
public function getModelSameParentAttr()
{
return Category::where('pid',$this->getData('pid'))
->where('level',$this->getData('level'))
->select();
}
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();
}
} }

View File

@@ -15,7 +15,9 @@ class Nav extends Base
public static $autoClearCache = [ public static $autoClearCache = [
[ [
'name' => 'type_3_list' 'type' => 'key',
'name' => 'type_list',
'field' => 'type'
] ]
]; ];

View File

@@ -1,4 +1,5 @@
<?php <?php
use think\facade\Env; use think\facade\Env;
use think\facade\App; use think\facade\App;
@@ -56,16 +57,16 @@ return [
'trigger_sql' => true, 'trigger_sql' => true,
// 开启字段缓存 // 开启字段缓存
'fields_cache' => true, 'fields_cache' => true,
], ],
'sqlite' => [ 'sqlite' => [
// 数据库类型 // 数据库类型
'type' => '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', ''), 'username' => Env::get('database.username', ''),
// 密码 // 密码
@@ -94,6 +95,7 @@ return [
'sql_explain' => false, 'sql_explain' => false,
// 是否需要断线重连 // 是否需要断线重连
'break_reconnect' => false, 'break_reconnect' => false,
'fields_cache' => true
], ],
// 更多的数据库配置信息 // 更多的数据库配置信息
], ],