Skip to content

Introduce a caching layer in bu_navigation_get_posts#48

Open
philcable wants to merge 8 commits intomasterfrom
phil/get-posts-caching
Open

Introduce a caching layer in bu_navigation_get_posts#48
philcable wants to merge 8 commits intomasterfrom
phil/get-posts-caching

Conversation

@philcable
Copy link

@philcable philcable commented Sep 26, 2019

https://trello.com/c/eU9qfTAa/30-ca-6-bu-navigation

This approach leverages keys stored in a new cache group - bu-navigation-persistent - as bu-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 the bu_navigation_get_posts function. This is stored with a key that uses the arguments passed to the function, JSON encoded and wrapped in md5, 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 of last_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.

philcable added 2 commits September 26, 2019 11:47
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.
@philcable philcable self-assigned this Sep 26, 2019
philcable added 4 commits September 26, 2019 12:28
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.
@acketon acketon requested review from alektrefi, hirozed, jayshreepraveen, jdub233 and toddmilliken and removed request for jayshreepraveen October 9, 2019 17:26
Copy link

@toddmilliken toddmilliken left a 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!


// 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' );

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 😆


// 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' );

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

Copy link
Author

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!

philcable and others added 2 commits October 9, 2019 15:17
Uses the working setup from bu-custom-roles which was pulled from latest wp scaffold plugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants