优化网站性能;

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
return [
'\app\middleware\PermissionRecord',
'\app\middleware\AdminLog',
// '\app\middleware\PermissionRecord',
// '\app\middleware\AdminLog',
];

View File

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

View File

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

View File

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

View File

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

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace app\model;
use app\common\model\Base;
use think\facade\Cache;
use think\facade\Request;
use think\Model;
@@ -13,10 +14,20 @@ use think\Paginator;
/**
* @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 = [
0 => '不发布',
1 => '发布'
@@ -38,7 +49,7 @@ class Post extends Model
public function comments()
{
return $this->hasMany(PostComment::class,'post_id');
return $this->hasMany(PostComment::class, 'post_id');
}