feat: 首页增加内存缓存(30s TTL)
- 新增 internal/cache/home_cache.go,实现带 TTL 的首页文章列表内存缓存 - frontend.go 首页 handler 优先读取缓存,未命中/过期时查询 DB 并回填 - PostService 注入失效回调,在 Create/Update/Delete/Approve 后自动失效缓存 - 减少首页每次请求都查 DB 的开销
This commit is contained in:
@ -21,7 +21,11 @@ func (s *PostService) Update(postID uint, title, body string) error {
|
||||
post.PendingTitle = title
|
||||
post.PendingBody = body
|
||||
post.RejectReason = "" // 清空旧退回理由
|
||||
return s.repo.Update(post)
|
||||
err := s.repo.Update(post)
|
||||
if err == nil {
|
||||
s.invalidateHomeCache()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 草稿/已退回:直接修改原文(现有逻辑)
|
||||
@ -34,12 +38,20 @@ func (s *PostService) Update(postID uint, title, body string) error {
|
||||
post.RejectReason = ""
|
||||
}
|
||||
|
||||
return s.repo.Update(post)
|
||||
err = s.repo.Update(post)
|
||||
if err == nil {
|
||||
s.invalidateHomeCache()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 软删除(权限在 controller 层检查)
|
||||
func (s *PostService) Delete(postID uint) error {
|
||||
return s.repo.SoftDelete(postID)
|
||||
if err := s.repo.SoftDelete(postID); err != nil {
|
||||
return err
|
||||
}
|
||||
s.invalidateHomeCache()
|
||||
return nil
|
||||
}
|
||||
|
||||
// SubmitForAudit 提交审核:draft → pending
|
||||
@ -79,6 +91,7 @@ func (s *PostService) Approve(postID uint) error {
|
||||
if err := s.repo.Update(post); err != nil {
|
||||
return err
|
||||
}
|
||||
s.invalidateHomeCache()
|
||||
// 通知作者修订审核通过
|
||||
if s.notifier != nil {
|
||||
s.notifier.NotifyPostApproved(post.UserID, post.Title, post.ID)
|
||||
@ -95,6 +108,7 @@ func (s *PostService) Approve(postID uint) error {
|
||||
if err := s.repo.Update(post); err != nil {
|
||||
return err
|
||||
}
|
||||
s.invalidateHomeCache()
|
||||
// 通知作者审核通过
|
||||
if s.notifier != nil {
|
||||
s.notifier.NotifyPostApproved(post.UserID, post.Title, post.ID)
|
||||
|
||||
Reference in New Issue
Block a user