Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update http cache rules to be more effective #1870

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions plextraktsync/config/HttpCacheConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HttpCacheConfig:
"api.trakt.tv/shows/*/seasons?extended=episodes": 28800,
"api.trakt.tv/shows/*/seasons": DO_NOT_CACHE,
"api.trakt.tv/sync/collection/shows": "1m",
"api.trakt.tv/users/*/collection/movies?extended=metadata": "1m",
"api.trakt.tv/users/*/collection/movies?extended=metadata": "10m",
"api.trakt.tv/users/*/collection/movies": DO_NOT_CACHE,
"api.trakt.tv/users/*/collection/shows": "1m",
"api.trakt.tv/users/*/ratings/episodes": "1m",
Expand All @@ -55,10 +55,10 @@ class HttpCacheConfig:
# Watchlist better be fresh for next run
"api.trakt.tv/users/*/watchlist/movies": "1s",
"api.trakt.tv/users/*/watchlist/shows": "1s",
"metadata.provider.plex.tv/library/sections/watchlist/all?*includeUserState=0": "1s",
"metadata.provider.plex.tv/library/sections/watchlist/all": "1s",
"api.trakt.tv/users/likes/lists": DO_NOT_CACHE,
"api.trakt.tv/users/me": DO_NOT_CACHE,
"metadata.provider.plex.tv/library/sections/watchlist/all?*includeUserState=0": "60m",
"metadata.provider.plex.tv/library/sections/watchlist/all": "10m",
"api.trakt.tv/users/likes/lists": "5m",
"api.trakt.tv/users/me": "60m",
# Public Lists
"api.trakt.tv/lists/*": "1d",
# Online Plex patterns
Expand All @@ -73,34 +73,35 @@ class HttpCacheConfig:
# Plex account
# Cache for some time, this activates 304 responses
"plex.tv/users/account": "1m",
"plex.tv/api/v2/user": "1m",
"plex.tv/api/v2/user": "15m",
# Plex patterns
# Ratings search
"*/library/sections/*/all?*userRating%3E%3E=-1*": "1m",
"*/library/sections/*/all?*userRating*=-1*": "5m",
# len(PlexLibrarySection)
"*/library/sections/*/all?includeCollections=0&X-Plex-Container-Size=0&X-Plex-Container-Start=0": DO_NOT_CACHE,
"*/library/sections/*/all?*X-Plex-Container-Size=0": "5m",
# __iter__(PlexLibrarySection)
"*/library/sections/*/all?includeGuids=1": DO_NOT_CACHE,
"*/library/sections/*/all?includeGuids=1": "5m",
# find_by_title
"*/library/sections/*/all?includeGuids=1&title=*": DO_NOT_CACHE,
"*/library/sections/*/all?includeGuids=1&title=*": "5m",
# episodes
"*/library/sections/*/all?includeGuids=1&type=4*": DO_NOT_CACHE,
# fetch_item, fetch_items
"*/library/sections/*/all?*": DO_NOT_CACHE,
"*/library/sections/*/collections": DO_NOT_CACHE,
"*/library/sections/*/collections?*X-Plex-Container-Size=0": "10m",
"*/library/sections/*/collections": "60m",
# library_sections
"*/library/sections": DO_NOT_CACHE,
"*/library/sections": "15m",
# reloads
"*/library/metadata/*?*include*": DO_NOT_CACHE,
# episodes
"*/library/sections/*/all?includeGuids=1&type=4*": DO_NOT_CACHE,
"*/library/metadata/*?*include*": "1h",
# find_by_id
"*/library/metadata/*": DO_NOT_CACHE,
"*/library/metadata/*": "1m",
# mark played, mark unplayed
"*/:/scrobble?key=*&identifier=com.plexapp.plugins.library": DO_NOT_CACHE,
"*/:/unscrobble?key=&&identifier=com.plexapp.plugins.library": DO_NOT_CACHE,
# playlists
"*/playlists?title=": DO_NOT_CACHE,
"*/playlists/*/items": DO_NOT_CACHE,
"*/library": DO_NOT_CACHE,
"*/playlists?title=": "5m",
"*/playlists/*/items": "15m",
"*/library": "60m",
# history
"*/status/sessions/history/all": DO_NOT_CACHE,
# has_sessions
Expand All @@ -109,8 +110,9 @@ class HttpCacheConfig:
"*/devices": DO_NOT_CACHE,
# system_account
"*/accounts": DO_NOT_CACHE,
# Plex server root
# version, updated_at
# "*/": DO_NOT_CACHE,
"*/": "10m",
}

@property
Expand Down
9 changes: 8 additions & 1 deletion plextraktsync/factory/Factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,15 @@ def session(self):

return CachedSession(
cache_name=self.config.cache_path,
cache_control=True,
# Plex sends "Cache-Control: no-cache" headers to requests we want to cache
cache_control=False,
urls_expire_after=self.urls_expire_after,
# https://requests-cache.readthedocs.io/en/stable/user_guide/expiration.html#expiration-and-error-handling
stale_if_error=True,
# https://requests-cache.readthedocs.io/en/stable/user_guide/expiration.html#asynchronous-revalidation
stale_while_revalidate=True,
# Plex doesn't Send Vary: X-Plex-Container-Start
match_headers=["X-Plex-Container-Start"],
)

@cached_property
Expand Down
2 changes: 0 additions & 2 deletions plextraktsync/plex/PlexServerConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from requests.exceptions import ConnectionError, SSLError

from plextraktsync.config import PLEX_PLATFORM
from plextraktsync.decorators.nocache import nocache
from plextraktsync.factory import Factory, logging


Expand All @@ -29,7 +28,6 @@ def config(self):
def session(self):
return self.factory.session

@nocache
def connect(self, urls: list[str], token: str):
plexapi.X_PLEX_PLATFORM = PLEX_PLATFORM
plexapi.TIMEOUT = self.timeout
Expand Down
Loading