diff --git a/internal/common/cache/clear.go b/internal/common/cache/clear.go new file mode 100644 index 0000000..b8f41c3 --- /dev/null +++ b/internal/common/cache/clear.go @@ -0,0 +1,18 @@ +package cache + +import ( + "github.com/TensoRaws/NuxBT-Backend/module/cache" + "strconv" +) + +// ClearCacheByKeys 清除缓存 +func ClearCacheByKeys(keys ...string) { + c := cache.Cache + c.Del(keys...) +} + +// ClearTorrentDetailCacheByTorrentID 清除种子详情缓存 +func ClearTorrentDetailCacheByTorrentID(torrentID int32) { + key := "/api/v1/torrent/detail?torrent_id=" + strconv.Itoa(int(torrentID)) + ClearCacheByKeys(key) +} diff --git a/internal/router/api/v1/torrent.go b/internal/router/api/v1/torrent.go index d08a47c..fd1b266 100644 --- a/internal/router/api/v1/torrent.go +++ b/internal/router/api/v1/torrent.go @@ -35,9 +35,14 @@ func TorrentRouterGroup(api *gin.RouterGroup) { jwt.RequireAuth(false), rbac.RABC(role.REVIEWER), torrent_service.Review) - // 获取种子文件列表 + // 种子详情 + torrent.GET("detail", + jwt.RequireAuth(false), + middleware_cache.Response(1*time.Hour), + torrent_service.Detail) + // 种子文件列表 torrent.GET("filelist", jwt.RequireAuth(false), - middleware_cache.Response(24*time.Hour), + middleware_cache.Response(6*time.Hour), torrent_service.FileList) } diff --git a/internal/service/torrent/delete.go b/internal/service/torrent/delete.go index 2ce46da..b3549f2 100644 --- a/internal/service/torrent/delete.go +++ b/internal/service/torrent/delete.go @@ -1,6 +1,7 @@ package torrent import ( + "github.com/TensoRaws/NuxBT-Backend/internal/common/cache" "github.com/TensoRaws/NuxBT-Backend/internal/common/db" "github.com/TensoRaws/NuxBT-Backend/module/code" "github.com/TensoRaws/NuxBT-Backend/module/log" @@ -29,6 +30,7 @@ func Delete(c *gin.Context) { } resp.OK(c) + cache.ClearTorrentDetailCacheByTorrentID(req.TorrentID) log.Logger.Infof("delete torrent success, torrent_id: %d", req.TorrentID) } diff --git a/internal/service/torrent/detail.go b/internal/service/torrent/detail.go new file mode 100644 index 0000000..ab64f6d --- /dev/null +++ b/internal/service/torrent/detail.go @@ -0,0 +1,39 @@ +package torrent + +import ( + "github.com/TensoRaws/NuxBT-Backend/internal/common/db" + "github.com/TensoRaws/NuxBT-Backend/module/code" + "github.com/TensoRaws/NuxBT-Backend/module/log" + "github.com/TensoRaws/NuxBT-Backend/module/resp" + "github.com/gin-gonic/gin" +) + +type DetailRequest struct { + TorrentID int32 `form:"torrent_id" binding:"required"` +} + +// Detail 获取种子文件列表 (GET /detail) +func Detail(c *gin.Context) { + // 绑定参数 + var req DetailRequest + if err := c.ShouldBindQuery(&req); err != nil { + resp.AbortWithMsg(c, code.RequestErrorInvalidParams, err.Error()) + return + } + + bt, err := db.GetTorrentByID(req.TorrentID) + if err != nil { + resp.AbortWithMsg(c, code.DatabaseErrorRecordNotFound, err.Error()) + return + } + + torrentInfo, err := GetTorrentInfo(bt) + if err != nil { + resp.AbortWithMsg(c, code.UnknownError, err.Error()) + log.Logger.Error("failed to get torrent info: " + err.Error()) + return + } + + resp.OKWithData(c, torrentInfo) + log.Logger.Infof("get torrent detail success: %v", req.TorrentID) +} diff --git a/internal/service/torrent/edit.go b/internal/service/torrent/edit.go index 821387b..d23f0f8 100644 --- a/internal/service/torrent/edit.go +++ b/internal/service/torrent/edit.go @@ -1,6 +1,7 @@ package torrent import ( + "github.com/TensoRaws/NuxBT-Backend/internal/common/cache" "github.com/TensoRaws/NuxBT-Backend/internal/common/db" "github.com/TensoRaws/NuxBT-Backend/module/code" "github.com/TensoRaws/NuxBT-Backend/module/log" @@ -63,6 +64,7 @@ func Edit(c *gin.Context) { } resp.OK(c) + cache.ClearTorrentDetailCacheByTorrentID(req.TorrentID) log.Logger.Infof("update torrent info success, torrent id: %v", req.TorrentID) }