diff --git a/server/server.go b/server/server.go index ba734920..2a75bcd8 100644 --- a/server/server.go +++ b/server/server.go @@ -7,9 +7,11 @@ import ( "net/url" "polaris/db" "polaris/log" + "polaris/pkg/cache" "polaris/pkg/tmdb" "polaris/server/core" "polaris/ui" + "time" ginzap "github.com/gin-contrib/zap" @@ -22,20 +24,24 @@ import ( func NewServer(db *db.Client) *Server { r := gin.Default() s := &Server{ - r: r, - db: db, - language: db.GetLanguage(), + r: r, + db: db, + language: db.GetLanguage(), + monitorNumCache: cache.NewCache[int, int](10 * time.Minute), + downloadNumCache: cache.NewCache[int, int](10 * time.Minute), } s.core = core.NewClient(db, s.language) return s } type Server struct { - r *gin.Engine - db *db.Client - core *core.Client - language string - jwtSerect string + r *gin.Engine + db *db.Client + core *core.Client + language string + jwtSerect string + monitorNumCache *cache.Cache[int, int] + downloadNumCache *cache.Cache[int, int] } func (s *Server) Serve() error { diff --git a/server/watchlist.go b/server/watchlist.go index 06826f91..3a76ac55 100644 --- a/server/watchlist.go +++ b/server/watchlist.go @@ -102,17 +102,23 @@ func (s *Server) GetTvWatchlist(c *gin.Context) (interface{}, error) { MonitoredNum: 0, DownloadedNum: 0, } - - details := s.db.GetMediaDetails(item.ID) - - for _, ep := range details.Episodes { - if ep.Monitored { - ms.MonitoredNum++ - if ep.Status == episode.StatusDownloaded { - ms.DownloadedNum++ + mon, ok1 := s.monitorNumCache.Get(item.ID) + dow, ok2 := s.downloadNumCache.Get(item.ID) + if ok1 && ok2 { + ms.MonitoredNum = mon + ms.DownloadedNum = dow + } else { + details := s.db.GetMediaDetails(item.ID) + for _, ep := range details.Episodes { + if ep.Monitored { + ms.MonitoredNum++ + if ep.Status == episode.StatusDownloaded { + ms.DownloadedNum++ + } } } } + res[i] = ms } return res, nil