优化记录存储

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();
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);
$list_site_last_visit = PostVisit::with(['post'])->order('id desc')
->group('post_id,ip,uid')
->limit(6)
->cache(60)
->cacheAlways(60)
->select();
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);
$total_week_hits = PostVisit::cache(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_week_hits = PostVisit::cacheAlways(60)->where('create_time', '>', strtotime(date('Y-m-d 00:00:00')) - 86400 * 7)->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_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);
$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);
$this->userHubLogin();

View File

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

View File

@@ -27,13 +27,7 @@ class Post extends Base
public const CACHE_KEY_HITS = 'cache_hits_';
public static $autoClearCache = [
[
'name' => 'top_post'
],
[
'type' => 'tag',
'name' => 'page_cache'
],
];
public static $stausNameList = [
@@ -217,7 +211,7 @@ class Post extends Base
$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) {
$start_content .= $v_start_post->content_html;
@@ -230,7 +224,7 @@ class Post extends Base
{
$end_content = '';
$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) {
$end_content.= $v_end_post->content_html;
}

View File

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

View File

@@ -6,48 +6,49 @@ use think\facade\Env;
// | 日志设置
// +----------------------------------------------------------------------
$level = [];
if (!env('app_debug')) {
$level = ['error'];
}
return [
// 默认日志记录通道
'default' => Env::get('log.channel', 'debug_mysql'),
'default' => Env::get('log.channel', 'debug_mysql'),
// 日志记录级别
'level' => $level,
'level' => $level,
// 日志类型记录的通道 ['error'=>'email',...]
'type_channel' => [],
// 关闭全局日志写入
'close' => false,
'close' => false,
// 全局日志处理 支持闭包
'processor' => null,
'processor' => null,
// 记录堆栈
'record_trace' => true,
// 日志通道列表
'channels' => [
'channels' => [
'file' => [
// 日志记录方式
'type' => 'File',
'type' => 'File',
// 日志保存目录
'path' => '',
'path' => '',
// 单文件日志写入
'single' => false,
'single' => false,
// 独立日志级别
'apart_level' => [],
'apart_level' => [],
// 最大日志文件数量
'max_files' => 0,
'max_files' => 0,
// 使用JSON格式记录
'json' => false,
'json' => false,
// 日志处理
'processor' => null,
'processor' => null,
// 关闭通道日志写入
'close' => false,
'close' => false,
// 日志输出格式化
'format' => '[%s][%s] %s',
'format' => '[%s][%s] %s',
// 是否实时写入
'realtime_write' => false,
],
@@ -56,22 +57,22 @@ return [
'debug_mysql' => [
'type' => 'DebugMysql',
// 服务器地址
'hostname' => Env::get('database.hostname', ''),
'hostname' => Env::get('database.hostname', ''),
// 数据库名
'database' => Env::get('database.database', ''),
'database' => Env::get('database.database', ''),
// 用户名
'username' => Env::get('database.username', ''),
'username' => Env::get('database.username', ''),
// 密码
'password' => Env::get('database.password', ''),
'password' => Env::get('database.password', ''),
// 端口
'hostport' => Env::get('database.hostport', '3306'),
'hostport' => Env::get('database.hostport', '3306'),
// 数据库连接参数
'params' => [],
'params' => [],
// 数据库编码默认采用utf8
'charset' => Env::get('database.charset', 'utf8'),
'charset' => Env::get('database.charset', 'utf8'),
// 数据库表前缀
'prefix' => Env::get('database.prefix', 'ul_'),
]
'prefix' => Env::get('database.prefix', 'ul_'),
],
],
];