优化文章详情缓存;

This commit is contained in:
2022-07-21 15:16:24 +08:00
parent da8b9b5e30
commit ec97f2d56a
2 changed files with 200 additions and 178 deletions

View File

@@ -5,105 +5,116 @@ declare(strict_types=1);
namespace app\index\controller;
use app\model\Post as ModelPost;
use think\facade\Cache;
use think\facade\View;
use think\model\Relation;
use think\Request;
class Post extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
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('链接已失效', '/');
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
}
$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
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}