0.2.58 - Requests module
This commit is contained in:
@@ -232,3 +232,50 @@ func hasRadarrCover(images []radarr.Image, coverType string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const radarrMovieCacheKey = "radarr:movies:v1"
|
||||
|
||||
// radarrMovieCatalogue is Radarr's whole movie list, cached the way sonarrSeriesCatalogue is
|
||||
// and for the same reason: the request page needs the state of every title one viewer has
|
||||
// ever asked for, and per-title lookups would be a round trip per card on a page somebody is
|
||||
// waiting in front of. Shared across the household, because Radarr's catalogue is.
|
||||
//
|
||||
// Failure degrades to asking Radarr directly — a cache that is down costs latency, never the
|
||||
// answer.
|
||||
func (s *Server) radarrMovieCatalogue(ctx context.Context) ([]radarr.Movie, error) {
|
||||
if s.radarr == nil {
|
||||
return nil, fmt.Errorf("radarr: not configured")
|
||||
}
|
||||
if movies := s.cachedRadarrMovies(ctx); movies != nil {
|
||||
return movies, nil
|
||||
}
|
||||
|
||||
s.radarrMu.Lock()
|
||||
defer s.radarrMu.Unlock()
|
||||
if movies := s.cachedRadarrMovies(ctx); movies != nil {
|
||||
return movies, nil
|
||||
}
|
||||
|
||||
movies, err := s.radarr.Movies(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body, marshalErr := json.Marshal(movies); marshalErr == nil {
|
||||
if cacheErr := s.cache.Set(ctx, radarrMovieCacheKey, body, s.cfg.RadarrTTL); cacheErr != nil {
|
||||
s.loggerFor(ctx).Warn("radarr movie cache write failed", "error", cacheErr)
|
||||
}
|
||||
}
|
||||
return movies, nil
|
||||
}
|
||||
|
||||
func (s *Server) cachedRadarrMovies(ctx context.Context) []radarr.Movie {
|
||||
raw, err := s.cache.Get(ctx, radarrMovieCacheKey)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var movies []radarr.Movie
|
||||
if json.Unmarshal(raw, &movies) != nil {
|
||||
return nil
|
||||
}
|
||||
return movies
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user