优化网站性能;

This commit is contained in:
2022-03-18 23:09:28 +08:00
parent d2cb402f71
commit 370a6cdc4d
6 changed files with 437 additions and 404 deletions

View File

@@ -1,6 +1,6 @@
<?php <?php
return [ return [
'\app\middleware\PermissionRecord', // '\app\middleware\PermissionRecord',
'\app\middleware\AdminLog', // '\app\middleware\AdminLog',
]; ];

View File

@@ -26,20 +26,19 @@ function json_message($data = [], $code = 0, $msg = '')
{ {
if (is_string($data)) { if (is_string($data)) {
if(strpos($data,'http') === 0 || strpos($data,'/') === 0){ if (strpos($data, 'http') === 0 || strpos($data, '/') === 0) {
$data = [ $data = [
'jump_to_url'=>$data 'jump_to_url' => $data
]; ];
}else{ } else {
$code = $code === 0 ? 500 : $code; $code = $code === 0 ? 500 : $code;
$msg = $data; $msg = $data;
$data = []; $data = [];
} }
} else if ($data instanceof Url) {
}else if($data instanceof Url){
$data = [ $data = [
'jump_to_url'=>(string)$data 'jump_to_url' => (string)$data
]; ];
} }
@@ -57,7 +56,7 @@ function get_system_config($name = '', $default = '')
try { try {
$list = SystemConfig::column('value', 'name'); $list = SystemConfig::column('value', 'name');
Cache::set('system_config',$list); Cache::set('system_config', $list);
} catch (\Throwable $th) { } catch (\Throwable $th) {
return $default; return $default;
} }
@@ -77,7 +76,7 @@ function get_system_config($name = '', $default = '')
function get_source_link($url) function get_source_link($url)
{ {
if(empty($url)){ if (empty($url)) {
$url = '/static/images/avatar.jpeg'; $url = '/static/images/avatar.jpeg';
} }
if (strpos($url, '/') === 0) { if (strpos($url, '/') === 0) {
@@ -217,43 +216,43 @@ function array2level($array, $pid = 0, $level = 1)
} }
function check_permission($key,$admin_id = null) function check_permission($key, $admin_id = null)
{ {
if(is_null($admin_id)){ if (is_null($admin_id)) {
$admin_id = Session::get('admin_id'); $admin_id = Session::get('admin_id');
} }
if(empty($admin_id)){ if (empty($admin_id)) {
return true; return true;
} }
if($admin_id == 1){ if ($admin_id == 1) {
return true; return true;
} }
$model_admin = Admin::cache(60)->find($admin_id); $model_admin = Admin::cache(60)->find($admin_id);
if(empty($model_admin->getData('group_id'))){ if (empty($model_admin->getData('group_id'))) {
return true; return true;
} }
$cache_key = 'permission_'.$key; $cache_key = 'permission_' . $key;
$model_permission = Cache::get($cache_key); $model_permission = Cache::get($cache_key);
if (empty($model_permission)) { if (empty($model_permission)) {
$model_permission = AdminPermission::where('key',$key)->find(); $model_permission = AdminPermission::where('key', $key)->find();
Cache::set($cache_key,$model_permission); Cache::set($cache_key, $model_permission);
} }
if (empty($model_permission)) { if (empty($model_permission)) {
$model_permission = AdminPermission::create([ $model_permission = AdminPermission::create([
'key'=>$key 'key' => $key
]); ]);
Cache::set($cache_key,$model_permission,60); Cache::set($cache_key, $model_permission, 60);
} }
if(in_array($model_permission->id,$model_admin->group->permissions)){ if (in_array($model_permission->id, $model_admin->group->permissions)) {
return true; return true;
} }

View File

@@ -5,6 +5,7 @@ namespace app\index\controller;
use app\model\Category; use app\model\Category;
use app\model\Post; use app\model\Post;
use app\model\PostCategory; use app\model\PostCategory;
use think\facade\Cache;
use think\facade\Session; use think\facade\Session;
use think\facade\View; use think\facade\View;
use think\Request; use think\Request;
@@ -18,6 +19,17 @@ class Index extends Common
*/ */
public function index() public function index()
{ {
$page_cache_key = md5($this->request->url());
$content = Cache::get($page_cache_key);
if (!empty($content)) {
return $content;
}
// //
$sub_category = []; $sub_category = [];
$current_category = []; $current_category = [];
@@ -52,7 +64,8 @@ class Index extends Common
$list_post = $model_post->paginate([ $list_post = $model_post->paginate([
'url' => 'Index/index', 'url' => 'Index/index',
'query' => $this->request->get() 'query' => $this->request->get(),
'list_rows' => 10
]); ]);
View::assign('current_category', $current_category); View::assign('current_category', $current_category);
@@ -60,7 +73,9 @@ class Index extends Common
View::assign('list_post', $list_post); View::assign('list_post', $list_post);
return View::fetch(); $content = View::fetch();
Cache::tag('page_cache')->set($page_cache_key, $content);
} }
@@ -68,8 +83,8 @@ class Index extends Common
{ {
Session::clear(); Session::clear();
$back_url = $this->request->param('back_url','/'); $back_url = $this->request->param('back_url', '/');
return $this->success('退出成功',$back_url); return $this->success('退出成功', $back_url);
} }
/** /**

View File

@@ -20,7 +20,11 @@ class Category extends Base
[ [
'name' => 'category_type_list', 'name' => 'category_type_list',
'field' => 'type' 'field' => 'type'
] ],
[
'type' => 'tag',
'name' => 'page_cache'
],
]; ];

View File

@@ -18,7 +18,11 @@ class Nav extends Base
'type' => 'key', 'type' => 'key',
'name' => 'type_list', 'name' => 'type_list',
'field' => 'type' 'field' => 'type'
] ],
[
'type' => 'tag',
'name' => 'page_cache'
],
]; ];
public static $statusName = [ public static $statusName = [

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace app\model; namespace app\model;
use app\common\model\Base;
use think\facade\Cache; use think\facade\Cache;
use think\facade\Request; use think\facade\Request;
use think\Model; use think\Model;
@@ -13,10 +14,20 @@ use think\Paginator;
/** /**
* @mixin think\Model * @mixin think\Model
*/ */
class Post extends Model class Post extends Base
{ {
// //
public static $autoClearCache = [
[
'name' => 'top_post'
],
[
'type' => 'tag',
'name' => 'page_cache'
],
];
public static $stausNameList = [ public static $stausNameList = [
0 => '不发布', 0 => '不发布',
1 => '发布' 1 => '发布'
@@ -38,7 +49,7 @@ class Post extends Model
public function comments() public function comments()
{ {
return $this->hasMany(PostComment::class,'post_id'); return $this->hasMany(PostComment::class, 'post_id');
} }