feat: cache download status number

This commit is contained in:
Simon Ding
2024-12-11 20:04:37 +08:00
parent 7c05acd1cf
commit 1020190c01
2 changed files with 28 additions and 16 deletions

View File

@@ -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 {

View File

@@ -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