-
Notifications
You must be signed in to change notification settings - Fork 7
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
Introduce a caching layer in bu_navigation_get_posts
#48
base: master
Are you sure you want to change the base?
Conversation
This opts to create a new cache group - `bu-navigation-persistent` - due to the `bu-navigation` group being set as non-persistent (see https://github.com/bu-ist/bu-navigation/blob/master/bu-navigation.php#L86). A copy of core's `last_changed` cache value is stored in the new cache group, and will leveraged for resetting the cache of the query results.
The previous approach had a significant flaw in that it could potentially never update the cache value for some calls to `bu_navigation_get_posts`, as the now-removed BU Navigation-specific `last_changed` key would be updated the first time the function was called.
Sorry...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense for caching the results!
includes/library.php
Outdated
@@ -428,6 +428,27 @@ function bu_navigation_get_posts( $args = '' ) { | |||
); | |||
$r = wp_parse_args( $args, $defaults ); | |||
|
|||
// Get the `last_changed` key from the WP core `posts` cache group. | |||
// This key is updated by core in `clean_post_cache`. | |||
$last_changed = wp_cache_get( 'last_changed', 'posts' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can do something like wp_cache_get_last_changed( 'posts' )
? https://developer.wordpress.org/reference/functions/wp_cache_get_last_changed/
More or less the same amount of typing 😆
includes/library.php
Outdated
|
||
// If WP's `last_changed` key has no value, set it using the current time. | ||
if ( ! $last_changed ) { | ||
wp_cache_set( 'last_changed', microtime(), 'posts' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the above function would mean you wouldn't have to do this if im reading the source code correctly for wp_cache_get_last_changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks right to me, too! Thanks for the suggestion!
Uses the working setup from bu-custom-roles which was pulled from latest wp scaffold plugin
https://trello.com/c/eU9qfTAa/30-ca-6-bu-navigation
This approach leverages keys stored in a new cache group -
bu-navigation-persistent
- asbu-navigation
is set as non-persistent.A copy of the
last_changed
cache key value from WP core is stored along with the results of the query made in thebu_navigation_get_posts
function. This is stored with a key that uses the arguments passed to the function, JSON encoded and wrapped inmd5
, so that multiple unique calls to the function will retrieve their respective results.If the
last_changed
value stored with the results of the query don't match the current WP core value oflast_changed
, the cache for that call is reset.WP core's
last_changed
key is also reset whenever changes to the menu are saved.I think this approach makes sense, but I appreciate that my understanding of the plugin as a whole may not be complete enough, and I could be missing something.