✨ feat(cache): 幂等只读接口接入缓存
- 新增 withCache 包装:命中直接返回,miss 回源后写入,仅缓存成功响应 - 详情/目录/批量详情/章节摘要缓存 24 小时;搜索/分类列表/分类详情短缓存 10 分钟 - 7 个 fqapi 处理函数原函数体不动、改名 fetchXxx,外层套缓存壳 - redisGetCache 补 3 秒读超时,与写路径一致,防远程 Redis 网络抖动拖垮请求 - 新增 TestWithCache:验证命中不回源、失败响应不缓存
This commit is contained in:
1 parent
f0d89b462b
commit
586b714f3c
10 files changed
+109
-8
No files matched your search
@@ -255,6 +255,19 @@ func isCacheEnabled() bool {
|
||||
return cfg.Cache.Enabled
|
||||
}
|
||||
|
||||
// withCache 只读接口的缓存包装:命中直接返回,miss 回源后写入(仅缓存成功响应)
|
||||
// ponytail: 空参等防御分支若漏到此处,其错误 JSON 也会被缓存到 TTL 到期——handler 层 validateParams 已拦截
|
||||
func withCache(key string, ttl int, fetch func() (string, error)) (string, error) {
|
||||
if c := GetCache(key); c != "" {
|
||||
return c, nil
|
||||
}
|
||||
result, err := fetch()
|
||||
if err == nil {
|
||||
UpdateCache(key, result, ttl)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
// GetCache 获取正文内容缓存
|
||||
func GetCache(key string) string {
|
||||
if !isCacheEnabled() {
|
||||
|
||||
Reference in new issue
Block a user