mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-03-03 16:24:28 +08:00
优化文章详情缓存;
This commit is contained in:
@@ -5,105 +5,116 @@ declare(strict_types=1);
|
|||||||
namespace app\index\controller;
|
namespace app\index\controller;
|
||||||
|
|
||||||
use app\model\Post as ModelPost;
|
use app\model\Post as ModelPost;
|
||||||
|
use think\facade\Cache;
|
||||||
use think\facade\View;
|
use think\facade\View;
|
||||||
use think\model\Relation;
|
use think\model\Relation;
|
||||||
use think\Request;
|
use think\Request;
|
||||||
|
|
||||||
class Post extends Common
|
class Post extends Common
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 显示资源列表
|
* 显示资源列表
|
||||||
*
|
*
|
||||||
* @return \think\Response
|
* @return \think\Response
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 显示创建资源表单页.
|
|
||||||
*
|
|
||||||
* @return \think\Response
|
|
||||||
*/
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 保存新建的资源
|
|
||||||
*
|
|
||||||
* @param \think\Request $request
|
|
||||||
* @return \think\Response
|
|
||||||
*/
|
|
||||||
public function save(Request $request)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 显示指定的资源
|
|
||||||
*
|
|
||||||
* @param int $id
|
|
||||||
* @return \think\Response
|
|
||||||
*/
|
|
||||||
public function read($uid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
|
|
||||||
$model_post = ModelPost::with([
|
|
||||||
'comments' => function (Relation $query) {
|
|
||||||
$query->order('id asc');
|
|
||||||
}
|
|
||||||
])->where('uid', $uid)->find();
|
|
||||||
|
|
||||||
if (empty($model_post)) {
|
|
||||||
return $this->error('链接已失效', '/');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$model_post->hits = $model_post->hits + 1;
|
/**
|
||||||
|
* 显示创建资源表单页.
|
||||||
|
*
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
$model_post->save();
|
/**
|
||||||
|
* 保存新建的资源
|
||||||
|
*
|
||||||
|
* @param \think\Request $request
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function save(Request $request)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
View::assign('post', $model_post);
|
/**
|
||||||
|
* 显示指定的资源
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function read($uid)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
|
||||||
return View::fetch();
|
$cache_key = 'post_' . $uid;
|
||||||
}
|
|
||||||
|
$model_post = Cache::get($cache_key);
|
||||||
|
|
||||||
|
if (empty($model_post)) {
|
||||||
|
|
||||||
|
$model_post = ModelPost::with([
|
||||||
|
'comments' => function (Relation $query) {
|
||||||
|
$query->order('id asc');
|
||||||
|
}
|
||||||
|
])->where('uid', $uid)->find();
|
||||||
|
|
||||||
|
if (empty($model_post)) {
|
||||||
|
return $this->error('链接已失效', '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
Cache::set($cache_key, $model_post);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$model_post->hits = $model_post->hits + 1;
|
||||||
|
|
||||||
|
$model_post->save();
|
||||||
|
|
||||||
|
View::assign('post', $model_post);
|
||||||
|
|
||||||
|
return View::fetch();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 显示编辑资源表单页.
|
* 显示编辑资源表单页.
|
||||||
*
|
*
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @return \think\Response
|
* @return \think\Response
|
||||||
*/
|
*/
|
||||||
public function edit($id)
|
public function edit($id)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存更新的资源
|
* 保存更新的资源
|
||||||
*
|
*
|
||||||
* @param \think\Request $request
|
* @param \think\Request $request
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @return \think\Response
|
* @return \think\Response
|
||||||
*/
|
*/
|
||||||
public function update(Request $request, $id)
|
public function update(Request $request, $id)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除指定资源
|
* 删除指定资源
|
||||||
*
|
*
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @return \think\Response
|
* @return \think\Response
|
||||||
*/
|
*/
|
||||||
public function delete($id)
|
public function delete($id)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace app\index\controller;
|
namespace app\index\controller;
|
||||||
|
|
||||||
|
use app\model\Post;
|
||||||
use app\model\PostComment as ModelPostComment;
|
use app\model\PostComment as ModelPostComment;
|
||||||
|
use think\facade\Cache;
|
||||||
use think\facade\Session;
|
use think\facade\Session;
|
||||||
use think\facade\Validate;
|
use think\facade\Validate;
|
||||||
use think\Request;
|
use think\Request;
|
||||||
@@ -12,112 +14,121 @@ use think\validate\ValidateRule;
|
|||||||
|
|
||||||
class PostComment extends Common
|
class PostComment extends Common
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 显示资源列表
|
* 显示资源列表
|
||||||
*
|
*
|
||||||
* @return \think\Response
|
* @return \think\Response
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 显示创建资源表单页.
|
|
||||||
*
|
|
||||||
* @return \think\Response
|
|
||||||
*/
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 保存新建的资源
|
|
||||||
*
|
|
||||||
* @param \think\Request $request
|
|
||||||
* @return \think\Response
|
|
||||||
*/
|
|
||||||
public function save(Request $request)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
|
|
||||||
$user_uid = Session::get('user_uid');
|
|
||||||
|
|
||||||
if (empty($user_uid)) {
|
|
||||||
return json_message('请先登录');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$post_data = $request->post();
|
/**
|
||||||
|
* 显示创建资源表单页.
|
||||||
$validate = Validate::rule('post_id', ValidateRule::isRequire())
|
*
|
||||||
->rule('content|评论内容', ValidateRule::isRequire()->length('5,200', '评论字符长度需要在5-200之间'));
|
* @return \think\Response
|
||||||
|
*/
|
||||||
if (!$validate->check($post_data)) {
|
public function create()
|
||||||
return json_message($validate->getError());
|
{
|
||||||
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
$post_data['type'] = 3;
|
/**
|
||||||
|
* 保存新建的资源
|
||||||
|
*
|
||||||
|
* @param \think\Request $request
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function save(Request $request)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
|
||||||
|
$user_uid = Session::get('user_uid');
|
||||||
|
|
||||||
|
if (empty($user_uid)) {
|
||||||
|
return json_message('请先登录');
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_data = $request->post();
|
||||||
|
|
||||||
|
$validate = Validate::rule('post_id', ValidateRule::isRequire())
|
||||||
|
->rule('content|评论内容', ValidateRule::isRequire()->length('5,200', '评论字符长度需要在5-200之间'));
|
||||||
|
|
||||||
|
if (!$validate->check($post_data)) {
|
||||||
|
return json_message($validate->getError());
|
||||||
|
}
|
||||||
|
|
||||||
|
$model_post = Post::find($post_data['post_id']);
|
||||||
|
|
||||||
|
if (empty($model_post)) {
|
||||||
|
return json_message('文章不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_data['type'] = 3;
|
||||||
|
|
||||||
|
$post_data['user_uid'] = $user_uid;
|
||||||
|
|
||||||
|
ModelPostComment::create($post_data);
|
||||||
|
|
||||||
|
ModelPostComment::clearCountCache();
|
||||||
|
|
||||||
|
Cache::delete('post_' . $model_post->uid);
|
||||||
|
|
||||||
|
|
||||||
$post_data['user_uid'] = $user_uid;
|
|
||||||
|
|
||||||
ModelPostComment::create($post_data);
|
|
||||||
|
|
||||||
ModelPostComment::clearCountCache();
|
return json_message();
|
||||||
|
}
|
||||||
|
|
||||||
return json_message();
|
/**
|
||||||
}
|
* 显示指定的资源
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function read($id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 显示指定的资源
|
* 显示编辑资源表单页.
|
||||||
*
|
*
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @return \think\Response
|
* @return \think\Response
|
||||||
*/
|
*/
|
||||||
public function read($id)
|
public function edit($id)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 显示编辑资源表单页.
|
* 保存更新的资源
|
||||||
*
|
*
|
||||||
* @param int $id
|
* @param \think\Request $request
|
||||||
* @return \think\Response
|
* @param int $id
|
||||||
*/
|
* @return \think\Response
|
||||||
public function edit($id)
|
*/
|
||||||
{
|
public function update(Request $request, $id)
|
||||||
//
|
{
|
||||||
}
|
//
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存更新的资源
|
* 删除指定资源
|
||||||
*
|
*
|
||||||
* @param \think\Request $request
|
* @param int $id
|
||||||
* @param int $id
|
* @return \think\Response
|
||||||
* @return \think\Response
|
*/
|
||||||
*/
|
public function delete($id)
|
||||||
public function update(Request $request, $id)
|
{
|
||||||
{
|
//
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
ModelPostComment::destroy($id);
|
||||||
* 删除指定资源
|
|
||||||
*
|
|
||||||
* @param int $id
|
|
||||||
* @return \think\Response
|
|
||||||
*/
|
|
||||||
public function delete($id)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
|
|
||||||
ModelPostComment::destroy($id);
|
ModelPostComment::clearCountCache();
|
||||||
|
|
||||||
ModelPostComment::clearCountCache();
|
return json_message();
|
||||||
|
}
|
||||||
return json_message();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user