Skip to content

Conversation

@spacedmonkey
Copy link
Member

@spacedmonkey spacedmonkey commented Apr 23, 2025

Create new helper functions wp_cache_*_salted, that improve cache invalidation, by re-using cache keys.

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.

@github-actions
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
@github-actions
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, peterwilsoncc, flixos90, sanchothefat, tillkruess, rmccue, mukesh27, adamsilverstein.

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!

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.
@spacedmonkey spacedmonkey changed the title Replace timestamp-based cache validation with last_changed check. New helper functions, wp_cache_*_salted Jul 22, 2025
…ion and update test cases for consistency.
Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

The source looks great, I've added a nit pick inline and some notes on the tests.

@peterwilsoncc
Copy link
Contributor

I've pushed a couple of minor changes for the following:

  • bd142f1 - CS alignment standards in various docs.
  • 12a45c7 - a functional change to return the results of setting the cache/s to match the existing APIs for wp_cache_set and wp_cache_set_multiple

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

This looks good to me/

I'll do some further manual testing but the test suite looks solid so I'll only come back if I discover any bugs while testing. If you don't hear from me assume it's good to go.

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 LGTM!

Only a few nit-picks on documentation, there are still obsolete mentions of the old "query data" approach in the descriptions on the new functions.

Preemptively approving, but we should fix those :)

peterwilsoncc and others added 2 commits August 18, 2025 10:16
Co-authored-by: Felix Arntz <flixos90@gmail.com>
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
@peterwilsoncc
Copy link
Contributor

peterwilsoncc commented Aug 18, 2025

I've committed @felixarntz and @mukeshpanchal27's suggestions and merged in trunk.

I'm still a little concerned about passing in the salt values rather than the last changed names.

It requires a developer remember to maintain order when generating the salted value, for example the following will result in different underlying cache keys being hit.

$salts = array(
   wp_cache_get_last_changed( 'group1' ),
   wp_cache_get_last_changed( 'group2' ),
);
$cached = wp_cache_get_salted( 'key', 'group', $salt );

$salts = array(
   wp_cache_get_last_changed( 'group2' ),
   wp_cache_get_last_changed( 'group1' ),
);
$cached = wp_cache_get_salted( 'key', 'group', $salt );

If instead we pass the group names for the $salt parameter, we can sort them prior to getting the last changed values.

sort( $salt );
$real_salt = array();
foreach ( $salt as $s ) {
   $real_salt[] = wp_cache_get_last_changed( $salt );
}

A compromise possibility may be to accept the salts with keys and do a ksort, using Query as an example

$salt = array(
   'posts' => wp_cache_get_last_changed( 'posts' ),
   'terms' => wp_cache_get_last_changed( 'terms' ),
);

I don't know that I am invested enough to consider this a blocker but if it's something we can optimise while working on this ticket, it would be preferable than doing so in the future.

@mukeshpanchal27
Copy link
Member

If instead we pass the group names for the $salt parameter, we can sort them prior to getting the last changed values.

+1

@tillkruss
Copy link
Member

@peterwilsoncc I can see an argument for a helper function to make the salt specifically for wp-core queries, but sorting $salt seems risky and passing group names to wp_cache_get_salted() seems incorrect to me. Just my ¢2.

$salt = wp_cache_get_last_changed_salt([ 'users', 'comments' ])

$cached = wp_cache_get_salted( 'some-query', 'user-queries', $salt );

function wp_cache_get_last_changed_salt( $groups ) {
    if (! is_array( $groups ) ) {
        $groups = [$groups];
    }

    sort( $groups );

    return array_map(function ( $group ) {
        return wp_cache_get_last_changed( $salt )
    });
}

Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

I can see an argument for a helper function to make the salt specifically for wp-core queries, but sorting $salt seems risky and passing group names to wp_cache_get_salted() seems incorrect to me. Just my ¢2.

I'm happy to listen to your two cents, you deal with these APIs far more frequently than I. @spacedmonkey, do you want to do the commit honours?

@spacedmonkey
Copy link
Member Author

I can see an argument for a helper function to make the salt specifically for wp-core queries, but sorting $salt seems risky and passing group names to wp_cache_get_salted() seems incorrect to me. Just my ¢2.

I think we might be over thinking this a little. My perf to just commit this and we can see key sort later if we feel it is needed.

If we do add keys to the salts, then we could just do this.

$salt = array(
   'posts' => wp_cache_get_last_changed( 'posts' ),
   'terms' => wp_cache_get_last_changed( 'terms' ),
);
wp_cache_get_salted( $cache_key, $group, $salt );
if( is_array( $salt ) ) {
     ksort( $salt );
     $salt = array_values( $salt );
     $salt = implode( ':', $salt );
}

Above could be adde later without any sort of break.

@spacedmonkey
Copy link
Member Author

Commited in r60697.

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.

9 participants