完成评论统计

This commit is contained in:
augushong
2021-01-03 22:02:34 +08:00
parent 3b5a56aef5
commit cc83df7fb9
5 changed files with 43 additions and 1 deletions

View File

@@ -75,6 +75,10 @@ admin.域名->访问后台应用
api.域名->访问接口应用
```
#### 用户体系
本站实现了用户中心的授权登陆,如果需要登陆的话,需要额外搭建一个用户中心站点,具体请看:https://gitee.com/ulthon/user_hub
#### 重置密码

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace app\admin\controller;
use app\model\PostComment as ModelPostComment;
use think\facade\Cache;
use think\facade\View;
use think\Request;
@@ -96,6 +97,8 @@ class PostComment extends Common
ModelPostComment::destroy($id);
ModelPostComment::clearCountCache();
return json_message();
}
}

View File

@@ -64,6 +64,9 @@ class PostComment extends Common
ModelPostComment::create($post_data);
ModelPostComment::clearCountCache();
return json_message();
}
@@ -113,6 +116,8 @@ class PostComment extends Common
ModelPostComment::destroy($id);
ModelPostComment::clearCountCache();
return json_message();
}
}

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace app\model;
use think\facade\Cache;
use think\Model;
use think\model\concern\SoftDelete;
use think\Paginator;
@@ -39,6 +40,15 @@ class Post extends Model
return $this->hasMany(PostComment::class,'post_id');
}
public function getCommentCountAttr()
{
return PostComment::getPostCommentsCount($this->getData('id'));
}
public function setPublishTimeAttr($value)
{
return strtotime($value);

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace app\model;
use app\index\controller\Common;
use think\facade\Cache;
use think\Model;
/**
@@ -32,4 +33,23 @@ class PostComment extends Model
return $read_url;
}
public static function getPostCommentsCount($post_id)
{
$cache_key = 'post_comment_count_' . $post_id;
$count = Cache::get($cache_key);
if (is_null($count)) {
$count = PostComment::where('post_id', $post_id)->count();
Cache::tag('comment')->set($cache_key, $count);
}
return $count;
}
public static function clearCountCache()
{
Cache::tag('comment')->clear();
}
}