Skip to content

Commit

Permalink
feat: cache download status number
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Dec 11, 2024
1 parent 7c05acd commit 1020190
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
22 changes: 14 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down
22 changes: 14 additions & 8 deletions server/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1020190

Please sign in to comment.