Files
fanqie-go/internal/fqapi/category_front.go
T
chenxin 586b714f3c ✨ feat(cache): 幂等只读接口接入缓存
- 新增 withCache 包装:命中直接返回,miss 回源后写入,仅缓存成功响应
- 详情/目录/批量详情/章节摘要缓存 24 小时;搜索/分类列表/分类详情短缓存 10 分钟
- 7 个 fqapi 处理函数原函数体不动、改名 fetchXxx,外层套缓存壳
- redisGetCache 补 3 秒读超时,与写路径一致,防远程 Redis 网络抖动拖垮请求
- 新增 TestWithCache:验证命中不回源、失败响应不缓存
2026-09-29 23:43:56 +08:00

51 lines
1.2 KiB
Go

package fqapi
import (
"fanqie/internal/common"
"fmt"
)
var categoryFrontLogger = common.NewLogger("category_front", "INFO", "")
// HandleCategoryFront 处理获取分类列表,分类列表短缓存 10 分钟
func HandleCategoryFront(tab interface{}) (string, error) {
return withCache(fmt.Sprintf("category_front_%v", tab), 600, func() (string, error) {
return fetchCategoryFront(tab)
})
}
func fetchCategoryFront(tab interface{}) (string, error) {
tabInt := ParseIntParam(tab, 0)
reqURL := BuildURL(
"https://reading.snssdk.com/reading/bookapi/new_category/front/v/",
fmt.Sprintf("new_category_tab=%d", tabInt),
fmt.Sprintf("update_version_code=%s", "58932"),
"distinct_style=1",
)
resp, err := GorgonRequest(reqURL, map[string]interface{}{
"method": "GET",
"headers": map[string]string{
"User-Agent": "com.dragon.read",
},
})
if err != nil {
categoryFrontLogger.Error(fmt.Sprintf("获取分类列表失败: %v", err))
return BuildErrorResponse("请求失败"), err
}
respJSON, err := resp.JSON()
if err != nil {
return BuildErrorResponse("解析响应失败"), err
}
if code, ok := respJSON["code"].(float64); ok && code == 0 {
return resp.Text, nil
}
return BuildErrorResponse("获取失败"), nil
}