Skip to content

Replace timestamp-based cache validation with last_changed check. #8728

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

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from

Conversation

spacedmonkey
Copy link
Member

@spacedmonkey spacedmonkey commented Apr 23, 2025

This update introduces a wp_is_valid_last_changed() function to validate cache data against the last_changed value, improving cache reliability and removing timestamp dependency. Cache keys have been simplified by removing last_changed from their structure, and the cache values now store last_changed for consistency. Additionally, functions across core query classes are updated to implement these changes.

Trac ticket: https://core.trac.wordpress.org/ticket/59592


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@tillkruss
Copy link
Member

LGTM! I'd approve this if I'd had the permissions.

@spacedmonkey spacedmonkey marked this pull request as ready for review April 26, 2025 18:59
Copy link

github-actions bot commented Apr 26, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @jonnynews.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core Committers: Use this line as a base for the props when committing in SVN:

Props spacedmonkey, flixos90, sanchothefat, tillkruess, rmccue, peterwilsoncc.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@spacedmonkey @tillkruss I really like where this is going.

I think we should consider implementing two helper functions to read and write cache keys with $last_changed awareness, so that that logic is abstracted and doesn't need to be duplicated in all these places. Doing so would make the logic updates needed in each query class minimal.

@tillkruss
Copy link
Member

I think we should consider implementing two helper functions to read and write cache keys with $last_changed awareness, so that that logic is abstracted and doesn't need to be duplicated in all these places.

Excellent idea!

Comment on lines 8131 to 8133
if ( $cache_value['last_changed'] !== $last_changed ) {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

We basically just do a string compare, not a time comparison?

$last_changed = wp_cache_get_last_changed( 'posts' );

if ( ! empty( $this->tax_query->queries ) ) {
    $last_changed .= wp_cache_get_last_changed( 'terms' );
}

Introduced `wp_cache_get_query_data` and `wp_cache_set_query_data` to handle cached query data with freshness validation. Refactored various classes, functions, and queries to use these standardized methods for improved readability and maintainability while ensuring cache consistency.
Comment on lines 8064 to 8084
/**
* Retrieves cached query data if valid and unchanged.
*
* @param string $cache_key The cache key used for storage and retrieval.
* @param string $group The cache group used for organizing data.
* @param string $last_changed The timestamp of the last modification to the cache group.
* @return mixed|false The cached data if valid, or false if the cache does not exist or is outdated.
*/
function wp_cache_get_query_data( $cache_key, $group, $last_changed ) {
$cache = wp_cache_get( $cache_key, $group );

if ( ! is_array( $cache ) ) {
return false;
}

if ( ! isset( $cache['last_changed'], $cache['data'] ) || $last_changed !== $cache['last_changed'] ) {
return false;
}

return $cache['data'];
}
Copy link
Member

Choose a reason for hiding this comment

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

The poor type really bothers me. I'm aware that string type is locked in for wp_cache_(g|s)et_last_changed() functions, but since these new functions don't overlap, I wonder if we could not carry over the poor type.

I'm thinking string as well as array of strings, so multiple values can be parsed and handled better by cache implementations.

Accepting string|string[] as the @param for $last_changed and adding an implode if it's an array.

if ( is_array( $last_changed ) ) {
    $last_changed = implode( ':', $last_changed );
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure I agree this. This can be done in your own code. I don't see a place for this in core.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, I can always open a separate PR that will ensure a uniform format for these.

return false;
}

if ( ! isset( $cache['last_changed'], $cache['data'] ) || $last_changed !== $cache['last_changed'] ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would recommend

Suggested change
if ( ! isset( $cache['last_changed'], $cache['data'] ) || $last_changed !== $cache['last_changed'] ) {
if ( ( ! isset( $cache['last_changed'] ) && ! isset( $cache['data'] ) ) || $last_changed !== $cache['last_changed'] ) {

The behaviour of multiple parameters to isset is non-obvious, this makes it more explicit.

Copy link
Member Author

Choose a reason for hiding this comment

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

@rmccue Is this a style thing or is the a code reason you want this change?

…_multiple_query_data`, and introduce unit tests for query cache functions.
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.

7 participants