-
Notifications
You must be signed in to change notification settings - Fork 3k
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
base: trunk
Are you sure you want to change the base?
Conversation
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
LGTM! I'd approve this if I'd had the permissions. |
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 Unlinked AccountsThe 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:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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.
@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.
Excellent idea! |
src/wp-includes/functions.php
Outdated
if ( $cache_value['last_changed'] !== $last_changed ) { | ||
return false; | ||
} |
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.
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.
cd7f5cb
to
e440510
Compare
…ery` for enhanced query cache handling.
…and consistency.
src/wp-includes/functions.php
Outdated
/** | ||
* 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']; | ||
} |
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.
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 );
}
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 am not sure I agree this. This can be done in your own code. I don't see a place for this in core.
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.
Okay, I can always open a separate PR that will ensure a uniform format for these.
src/wp-includes/functions.php
Outdated
return false; | ||
} | ||
|
||
if ( ! isset( $cache['last_changed'], $cache['data'] ) || $last_changed !== $cache['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.
I would recommend
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.
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.
@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.
Co-authored-by: Peter Wilson <[email protected]>
This update introduces a
wp_is_valid_last_changed()
function to validate cache data against thelast_changed
value, improving cache reliability and removing timestamp dependency. Cache keys have been simplified by removinglast_changed
from their structure, and the cache values now storelast_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.