优化记录存储

This commit is contained in:
2023-11-07 10:04:54 +08:00
parent aed08bc7a1
commit 789d1dc395
5 changed files with 46 additions and 44 deletions

View File

@@ -41,28 +41,28 @@ class Common extends BaseController
$list_nav_more = Nav::where('type', 8)->cacheAlways('type_list_8')->order('sort asc')->where('status', 1)->select(); $list_nav_more = Nav::where('type', 8)->cacheAlways('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)->cacheAlways('top_post')->select(); $top_posts = Post::where('is_top', 1)->limit(8)->where('type', 3)->cacheAlways(60)->select();
View::assign('top_posts', $top_posts); View::assign('top_posts', $top_posts);
$list_site_last_visit = PostVisit::with(['post'])->order('id desc') $list_site_last_visit = PostVisit::with(['post'])->order('id desc')
->group('post_id,ip,uid') ->group('post_id,ip,uid')
->limit(6) ->limit(6)
->cache(60) ->cacheAlways(60)
->select(); ->select();
View::assign('list_site_last_visit', $list_site_last_visit); View::assign('list_site_last_visit', $list_site_last_visit);
$total_hits = Post::cache(60)->sum('hits'); $total_hits = Post::cacheAlways(60)->sum('hits');
View::assign('total_hits', $total_hits); View::assign('total_hits', $total_hits);
$total_week_hits = PostVisit::cache(60)->where('create_time', '>', strtotime(date('Y-m-d 00:00:00')) - 86400 * 7)->count(); $total_week_hits = PostVisit::cacheAlways(60)->where('create_time', '>', strtotime(date('Y-m-d 00:00:00')) - 86400 * 7)->count();
$total_day_hits = PostVisit::cache(60)->where('create_time', '>', strtotime(date('Y-m-d 00:00:00')))->count(); $total_day_hits = PostVisit::cacheAlways(60)->where('create_time', '>', strtotime(date('Y-m-d 00:00:00')))->count();
View::assign('total_week_hits', $total_week_hits); View::assign('total_week_hits', $total_week_hits);
View::assign('total_day_hits', $total_day_hits); View::assign('total_day_hits', $total_day_hits);
$total_post_count = Post::cache(60)->where('status', 1)->where('type', 3)->count(); $total_post_count = Post::cacheAlways(60)->where('status', 1)->where('type', 3)->count();
View::assign('total_post_count', $total_post_count); View::assign('total_post_count', $total_post_count);
$total_month_post_count = Post::cache(60)->where('status', 1)->where('type', 3)->where('publish_time', '>', strtotime(date('Y-m-1 00:00:00')))->count(); $total_month_post_count = Post::cacheAlways(60)->where('status', 1)->where('type', 3)->where('publish_time', '>', strtotime(date('Y-m-1 00:00:00')))->count();
View::assign('total_month_post_count', $total_month_post_count); View::assign('total_month_post_count', $total_month_post_count);
$this->userHubLogin(); $this->userHubLogin();

View File

@@ -14,6 +14,13 @@ use think\model\Relation;
class Post extends Common class Post extends Common
{ {
public function initialize()
{
if ($this->request->action() != 'markVisit') {
parent::initialize();
}
}
/** /**
* 显示指定的资源. * 显示指定的资源.
* *
@@ -52,6 +59,7 @@ class Post extends Common
->limit(12) ->limit(12)
->cache(60) ->cache(60)
->select(); ->select();
View::assign('post', $model_post); View::assign('post', $model_post);
View::assign('list_last_visit', $list_last_visit); View::assign('list_last_visit', $list_last_visit);
@@ -96,9 +104,7 @@ class Post extends Common
public function markVisit($visit_id) public function markVisit($visit_id)
{ {
$model_visit = PostVisit::find($visit_id); PostVisit::where('id', $visit_id)->update(['is_js_run' => 1]);
$model_visit->is_js_run = 1;
$model_visit->save();
return json_message(); return json_message();
} }

View File

@@ -27,13 +27,7 @@ class Post extends Base
public const CACHE_KEY_HITS = 'cache_hits_'; public const CACHE_KEY_HITS = 'cache_hits_';
public static $autoClearCache = [ public static $autoClearCache = [
[
'name' => 'top_post'
],
[
'type' => 'tag',
'name' => 'page_cache'
],
]; ];
public static $stausNameList = [ public static $stausNameList = [
@@ -217,7 +211,7 @@ class Post extends Base
$list_post_category_id = $this->getAttr('categorys')->column('category_id'); $list_post_category_id = $this->getAttr('categorys')->column('category_id');
$list_start_post = Post::where('category_id', 'in', $list_post_category_id)->where('type', 'category-start')->cache(600)->where('status', 1)->select(); $list_start_post = Post::where('category_id', 'in', $list_post_category_id)->where('type', 'category-start')->cacheAlways(600)->where('status', 1)->select();
foreach ($list_start_post as $k_start_post => $v_start_post) { foreach ($list_start_post as $k_start_post => $v_start_post) {
$start_content .= $v_start_post->content_html; $start_content .= $v_start_post->content_html;
@@ -230,7 +224,7 @@ class Post extends Base
{ {
$end_content = ''; $end_content = '';
$list_post_category_id = $this->getAttr('categorys')->column('category_id'); $list_post_category_id = $this->getAttr('categorys')->column('category_id');
$list_end_post = Post::where('category_id', 'in', $list_post_category_id)->where('type', 'category-end')->cache(600)->where('status', 1)->select(); $list_end_post = Post::where('category_id', 'in', $list_post_category_id)->where('type', 'category-end')->cacheAlways(600)->where('status', 1)->select();
foreach ($list_end_post as $k_end_post => $v_end_post) { foreach ($list_end_post as $k_end_post => $v_end_post) {
$end_content.= $v_end_post->content_html; $end_content.= $v_end_post->content_html;
} }

View File

@@ -15,6 +15,7 @@ class PostVisit extends Model
// //
protected $json = [ protected $json = [
'client_bot',
'client_os', 'client_os',
'client', 'client',
]; ];

View File

@@ -6,14 +6,12 @@ use think\facade\Env;
// | 日志设置 // | 日志设置
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
$level = []; $level = [];
if (!env('app_debug')) { if (!env('app_debug')) {
$level = ['error']; $level = ['error'];
} }
return [ return [
// 默认日志记录通道 // 默认日志记录通道
'default' => Env::get('log.channel', 'debug_mysql'), 'default' => Env::get('log.channel', 'debug_mysql'),
@@ -27,6 +25,9 @@ return [
// 全局日志处理 支持闭包 // 全局日志处理 支持闭包
'processor' => null, 'processor' => null,
// 记录堆栈
'record_trace' => true,
// 日志通道列表 // 日志通道列表
'channels' => [ 'channels' => [
'file' => [ 'file' => [
@@ -71,7 +72,7 @@ return [
'charset' => Env::get('database.charset', 'utf8'), 'charset' => Env::get('database.charset', 'utf8'),
// 数据库表前缀 // 数据库表前缀
'prefix' => Env::get('database.prefix', 'ul_'), 'prefix' => Env::get('database.prefix', 'ul_'),
] ],
], ],
]; ];