优化网站缓存;

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'] ?? '';
$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);
}
}

View File

@@ -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();

View File

@@ -4,16 +4,26 @@ 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 $autoClearCache = [
[
'name' => 'category_type_list',
'field' => 'type'
]
];
public static $allCategory = [];
@@ -23,20 +33,20 @@ class Category extends Model
* @param string $id
* @return void
*/
public static function getListLevel($id = '',$type = 1)
public static function getListLevel($id = '', $type = 1)
{
if(empty(self::$allCategory)){
if (empty(self::$allCategory)) {
$model_list = Category::where('type',$type)->order('sort asc')->select();
self::$allCategory = array2level($model_list,0,0);
$model_list = Category::where('type', $type)->order('sort asc')->select();
self::$allCategory = array2level($model_list, 0, 0);
}
if(!empty($id)){
if (!empty($id)) {
$list = [];
$in_category = [$id];
foreach (self::$allCategory as $category) {
if(in_array($category->pid,$in_category)){
if (in_array($category->pid, $in_category)) {
$list[] = $category;
$in_category[] = $category->id;
}
@@ -57,7 +67,7 @@ class Category extends Model
public function posts()
{
return $this->hasMany(PostCategory::class,'category_id');
return $this->hasMany(PostCategory::class, 'category_id');
}
/**
@@ -72,7 +82,7 @@ class Category extends Model
$list_post = [];
foreach ($list_post_category as $list_post_category) {
array_push($list_post,$list_post_category->post);
array_push($list_post, $list_post_category->post);
}
return $list_post;
@@ -87,32 +97,32 @@ class Category extends Model
{
$list_post_category = $this->getAttr('posts');
$list_post = array_column($list_post_category->append(['post'])->toArray(),'post');
$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);
return Config::get('view_type.category.' . $value);
}
public function getModelParentAttr()
{
$pid = $this->getData('pid');
if($pid == 0){
if ($pid == 0) {
return $this;
}
return Category::where('id',$pid)->find();
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'))
return Category::where('pid', $this->getData('pid'))
->where('level', $this->getData('level'))
->where('id', '<>', $this->getData('id'))
->select();
}
@@ -123,9 +133,8 @@ class Category extends Model
*/
public function getModelSameParentAttr()
{
return Category::where('pid',$this->getData('pid'))
->where('level',$this->getData('level'))
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 = [
[
'name' => 'type_3_list'
'type' => 'key',
'name' => 'type_list',
'field' => 'type'
]
];

View File

@@ -1,4 +1,5 @@
<?php
use think\facade\Env;
use think\facade\App;
@@ -63,9 +64,9 @@ return [
'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
],
// 更多的数据库配置信息
],