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

Refactor: Add plex cache policy patterns #1279

Merged
merged 8 commits into from
Dec 22, 2022

Conversation

glensc
Copy link
Collaborator

@glensc glensc commented Dec 11, 2022

Continue #1221, but for plex urls.

git grep --function-context -C2 @nocache

Docs:

@glensc glensc requested a review from simonc56 December 11, 2022 21:21
@glensc glensc self-assigned this Dec 11, 2022
@glensc
Copy link
Collaborator Author

glensc commented Dec 11, 2022

An easy way to find the @nocache patterns is to run full sync and grep logs for:

grep -o 'Skipping cache write for URL: https://.*plex.direct.*' plextraktsync.log

@glensc
Copy link
Collaborator Author

glensc commented Dec 11, 2022

calling self.plex.library would make this request:

<!-- request: GET https://*.plex.direct:32400/library, response: 200 (460 bytes), created: 2022-12-11 23:27:11 EET, expires: N/A (fresh) -->
<?xml version='1.0' encoding='utf8'?>
<MediaContainer size="3" allowSync="0" art="/:/resources/library-art.png" content="" identifier="com.plexapp.plugins.library" mediaTagPrefix="/system/bundle/media/flags/" mediaTagVersion="1667296919" title1="Plex Library" title2="">
<Directory key="sections" title="Library Sections" />
<Directory key="recentlyAdded" title="Recently Added Content" />
<Directory key="onDeck" title="On Deck Content" />
</MediaContainer>

this doesn't seem to contain anything useful, so I could be cached forever.

but I'm wondering what is mediaTagVersion="1667296919", plexapi sources say "Unknown" and don't get much matches from google either.

It seems to match timestamp Tue Nov 1 12:01:59 2022 +0200, it doesn't match library update timestamp, nor pms startup time

@glensc
Copy link
Collaborator Author

glensc commented Dec 11, 2022

there's a problem, can't set rule for root URL (used for "version" and "updated_at" calls), as it will match everything

        # version, updated_at
        "*/": DO_NOT_CACHE,

from docs:

Patterns will match request base URLs without the protocol, so the pattern site.com/resource/ is equivalent to http*://site.com/resource/**

@glensc glensc force-pushed the cache-patterns-for-plex branch from c29b06c to d130b6c Compare December 11, 2022 22:59
@glensc
Copy link
Collaborator Author

glensc commented Dec 11, 2022

@simonc56
Copy link
Collaborator

Aren't login urls needed too ?

*.trakt.tv/oauth
plex.tv/api/resources
plex.tv/api/users
...

@glensc
Copy link
Collaborator Author

glensc commented Dec 16, 2022

Aren't login urls needed too ?

*.trakt.tv/oauth
plex.tv/api/resources
plex.tv/api/users
...
  1. I haven't finished urls about plex, but sure you can add your commits here
  2. do you have actual issue with trakt URLs, please submit PR to fix.

with trakt pull request, I went every method and looked what requests they make and converted @nocache to rule. so URLs that did not have @nocache before did not get rule added, so they should continue to behave like before.

there's slight possibility if some method under @nocache made multiple requests based on conditions, then those I might have missed.

# fetch_item
"*/library/sections/*/all?*": DO_NOT_CACHE,
"*/library/sections/*/collections": DO_NOT_CACHE,
"*/library/metadata/*": DO_NOT_CACHE,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think metadata.provider.plex.tv/library/metadata/* should be cached because there is no user data in response.
But metadata.provider.plex.tv/library/metadata/*/userState must remain not cached.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added dc07f64, but perhaps should have still some cache expiry rather NEVER_EXPIRE? how much?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could have a random value, that'd be perfect.
It would allow to spread out the requests.
E.g. 3 month +random(0-30days)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b667c0e, 5f2243b.

but how to test that? enable all watchlist related options and like some online movie?

Copy link
Collaborator

@simonc56 simonc56 Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's smart, but it's a value for the whole session.
At first run, all requests will have same expiration date. There is no spread out.

If impossible, i'd say NEVER_EXPIRE is good enough for metadata.provider.plex.tv/library/metadata/* .

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no metadata.provider.plex.tv/library/metadata/*/userState, but rather https://metadata.provider.plex.tv/library/metadata/*?*includeUserState=1 and it includes view counts etc.

but I don't think we use those values, only test for guids

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some other places were also uncacheable because session wasn't inherited:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glensc glensc force-pushed the cache-patterns-for-plex branch from 41e2fe9 to 29e507a Compare December 19, 2022 14:24
@glensc
Copy link
Collaborator Author

glensc commented Dec 20, 2022

Basically, all methods were manually verified and moved @nocache to URL patterns.

but could be that I didn't test all agent URLs, I'm still using Legacy Agent for shows due performance reasons.

PlexServerConnection.connect remains with @nocache because it's pattern would be "*", making it apply for all urls.

@glensc glensc force-pushed the cache-patterns-for-plex branch from a47279c to c5f0580 Compare December 20, 2022 12:43
PUT methods are not cached anyway
* Add plex cache policy patterns
* Add plex rule for fetch_item
* Add cache rule for item history
* Use DO_NOT_CACHE constant
* Add rule for version, updated_at
* Add cache rule for library_sections call
* Disable root url rule for now;
* Add rule for system_device methpd
* Add rule for system_account method
* Reorder to account for first match
* Add rule for ratings search
* Add patterns for plex playlists
* Add rules for metadata.provider.plex.tv/library/metadata patterns
* Add pattern for find_by_title
* Record fetch_items pattern
* Record rule for find_by_id
* Add LONG_EXPIRY for metadata.provider.plex.tv/library/metadata/*
* Add rule for /has_sessions
* Add rule for plex.tv/users/account
* Add rule for metadata.provider.plex.tv/library/sections/watchlist/all
* Update includeUserState pattern
* Add rule for len(PlexLibrarySection)
* Add pattern for reloads
* Add rule for PlexLibrarySection iteration
* Add rule for episodes fetch
* Add rules for mark played, mark unplayed
* Sort items
* Remove nocache for playlists
* Remove nocache for has_sessions
* Remove nocache for _plex_account
* Remove nocache from watchlist
* Remove nocache for mark played, mark unplayed
* Remove nocache for reset_show
* Cleanup @nocache for find_by_title
* Cleanup @nocache for find_by_id
* Cleanup @nocache for search
* Cleanup @nocache for fetch_items
* Cleanup @nocache for section length method
* Remove nocache for get uids
* Remove nocache for episodes
* Remove nocache for rating method
@glensc glensc force-pushed the cache-patterns-for-plex branch from c5f0580 to 6ebd0b7 Compare December 22, 2022 13:44
@glensc glensc marked this pull request as ready for review December 22, 2022 13:45
@glensc glensc merged commit 71a1e3a into Taxel:main Dec 22, 2022
@glensc glensc deleted the cache-patterns-for-plex branch December 22, 2022 13:46
@simonc56
Copy link
Collaborator

Heavily tested I assume 👼

@glensc
Copy link
Collaborator Author

glensc commented Dec 22, 2022

Heavily tested I assume 👼

It's not git tagged yet. but fingers crossed!

it already solved one issue for me that inspect <season_id> cached the result and episode.media.part.exists leading to wrong cached value.

@simonc56
Copy link
Collaborator

fingers crossed

Come on....
Don't tag before tests.

@glensc
Copy link
Collaborator Author

glensc commented Dec 22, 2022

what do you mean? I've been testing it and running it for a while. and I haven't made tag, that's what I said, it's not tagged yet.

@simonc56
Copy link
Collaborator

I thought you didn't test and only cross finger as a quality process.

@glensc
Copy link
Collaborator Author

glensc commented Dec 22, 2022

I wrote how I tested:

you didn't reply anything to it. so I assumed you were fine with this. and plexapi fixes got released too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants