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

Feature/cache last changed time #2530

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

cameronterry
Copy link

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:

Possible Drawbacks

None that I can foresee. The change is inconsequential to the general operation of ElasticPress.

Verification Process

Steps:

  1. Create / reuse a Multisite with two or more sites.
  2. Add a way to see the values from wp_cache_get_last_changed() - I use a dirty / hacky var_dump() as noted in the code snippet below.
  3. Index both sites.
  4. 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).
  5. Check the values after performing the following operations on one of the two sites:
  6. Create / modify / delete new post on Site A = both values on Site A will update. Site B will only update elasticpress-network.
  7. Reindex Site A = both values on Site A will update. Site B will only update elasticpress-network.
  8. Run wp elasticpress index --setup --network-wide in 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:

  • I have read the CONTRIBUTING document.
  • 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 😬

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).

@felipeelia
Copy link
Member

Hey @cameronterry, is this something that would be still beneficial for the plugin? Thanks!

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.

None yet

2 participants