Feature/cache last changed time
Description of the Change
This PR implements the ability to make use of wp_cache_get_last_changed() for ElasticPress, identifying the last time a modification was made to an index. Both for the current blog / site and the entire network / Multisite. This is borrowed from WordPress Core's use of wp_cache_get_last_changed( 'posts' ) (and others) which can be a good way of identifying whenever a post, irrespective of post type or type of update, has occurred.
Alternate Designs
None were considered, as the source / inspiration seem a foundational feature being introduced to ElasticPress from WordPress Core.
Benefits
This is useful in scenarios of creating cache entries where the contents are sensitive to data changes on indices, especially involving a query spanning multiple sites within a Multisite.
This is currently needed on a project to better enhance Block-level caching of HTML, where the block features a number of posts from a number of websites across a Multisite. The underlying WP_Query is powered by ElasticPress. Currently for these blocks I am setting a lower cache time - i.e. 15 minutes - to ensure it updates quickly for new content across the sites.
For Blocks which are purely WP_Query / database driven, the cache time can be set much higher as I can utilise wp_cache_get_last_changed( 'posts' ) to determine when content updates have occurred. This PR provides a way of doing the same with ElasticPress across multiple sites.
The alternative would be to query each site individually to see the latest content updates, however this would negate any meaningful performance benefit of caching the HTML output of the block.
A quick note on posts and ElasticPress last changed cache:
The following lines may appear identical in purpose.
wp_cache_get_last_changed( 'posts' );
wp_cache_get_last_changed( 'elasticpress' );
However there is a slight difference: wp_cache_get_last_changed( 'posts' ) will not update when the index is deleted.
Useful Links:
wp_cache_get_last_changed()- WordPress Code Reference- Example of WordPress Core using last changed for posts and terms.
- Example use case of
wp_cache_get_last_changed()in WordPress Core.
Possible Drawbacks
None that I can foresee. The change is inconsequential to the general operation of ElasticPress.
Verification Process
Steps:
- Create / reuse a Multisite with two or more sites.
- Add a way to see the values from
wp_cache_get_last_changed()- I use a dirty / hackyvar_dump()as noted in the code snippet below. - Index both sites.
- Check the values -
wp_cache_get_last_changed( 'elasticpress-network' )will be identical on both sites,wp_cache_get_last_changed( 'elasticpress' ) )will be different (as the sites were indexed at separate times). - Check the values after performing the following operations on one of the two sites:
- Create / modify / delete new post on Site A = both values on Site A will update. Site B will only update
elasticpress-network. - Reindex Site A = both values on Site A will update. Site B will only update
elasticpress-network. - Run
wp elasticpress index --setup --network-widein the terminal - both sites will have identical values.
Apologies for the dirty / hacky way of checking, but it was simpler than creating a custom block and much easier to explain when you can just see a value on the screen.
add_action( 'wp_head', function () {
var_dump( 'ep - site - ' . wp_cache_get_last_changed( 'elasticpress' ) );
var_dump( 'ep - network - ' . wp_cache_get_last_changed( 'elasticpress-network' ) );
} );
Checklist:
- [x] I have read the CONTRIBUTING document.
- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my change.
- [ ] All new and existing tests passed.
Applicable Issues
My apologies, got this far down the PR before realising that there is suppose to be issue created :grimacing:
Changelog Entry
Added: ability to detect changes to Elasticsearch indices using Object Cache with `wp_cache_get_last_changed( 'elasticpress' )` (for the current site) or `wp_cache_get_last_changed( 'elasticpress-network' )` (for changes across the Multisite).
Hey @cameronterry, is this something that would be still beneficial for the plugin? Thanks!