feat: 首页增加内存缓存(30s TTL)

- 新增 internal/cache/home_cache.go,实现带 TTL 的首页文章列表内存缓存
- frontend.go 首页 handler 优先读取缓存,未命中/过期时查询 DB 并回填
- PostService 注入失效回调,在 Create/Update/Delete/Approve 后自动失效缓存
- 减少首页每次请求都查 DB 的开销
This commit is contained in:
2026-06-02 17:20:59 +08:00
parent 3e0fed80ce
commit 593b52096f
6 changed files with 110 additions and 11 deletions

View File

@ -22,10 +22,16 @@ func setupFrontendRoutes(r *gin.Engine, cfg *config.Config, d *dependencies) {
})
{
pages.GET("/", func(c *gin.Context) {
posts, total, err := d.postService.List("", 1, 6)
if err != nil {
posts = nil
total = 0
posts, total, ok := d.homeCache.Get()
if !ok {
var err error
posts, total, err = d.postService.List("", 1, 6)
if err != nil {
posts = nil
total = 0
} else {
d.homeCache.Set(posts, total)
}
}
c.HTML(http.StatusOK, "home/index.html", common.BuildPageData(c, gin.H{
"Title": "首页",