Cache API: Add helper function `wp_cache_set_last_changed`.

Add a helper function called `wp_cache_set_last_changed` to set the last changed value for cache groups. This function has a new action called `wp_cache_set_last_changed`, allowing for developers to cache invalidate when last changed value is changed. 

Props tillkruess, spacedmonkey, peterwilsoncc, mukesh27, johnjamesjacoby. 
Fixes #57905.
Built from https://develop.svn.wordpress.org/trunk@55702


git-svn-id: http://core.svn.wordpress.org/trunk@55214 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-05-02 11:26:24 +00:00
parent 4bdeb57ac5
commit c1facaa4ce
8 changed files with 41 additions and 11 deletions

View File

@ -3918,7 +3918,7 @@ function wp_comments_personal_data_eraser( $email_address, $page = 1 ) {
* @since 5.0.0 * @since 5.0.0
*/ */
function wp_cache_set_comments_last_changed() { function wp_cache_set_comments_last_changed() {
wp_cache_set( 'last_changed', microtime(), 'comment' ); wp_cache_set_last_changed( 'comment' );
} }
/** /**

View File

@ -7680,12 +7680,42 @@ function wp_unique_id( $prefix = '' ) {
function wp_cache_get_last_changed( $group ) { function wp_cache_get_last_changed( $group ) {
$last_changed = wp_cache_get( 'last_changed', $group ); $last_changed = wp_cache_get( 'last_changed', $group );
if ( ! $last_changed ) { if ( $last_changed ) {
$last_changed = microtime(); return $last_changed;
wp_cache_set( 'last_changed', $last_changed, $group );
} }
return $last_changed; return wp_cache_set_last_changed( $group );
}
/**
* Sets last changed date for the specified cache group to now.
*
* @since 6.3.0
*
* @param string $group Where the cache contents are grouped.
* @return string UNIX timestamp when the group was last changed.
*/
function wp_cache_set_last_changed( $group ) {
$previous_time = wp_cache_get( 'last_changed', $group );
$time = microtime();
wp_cache_set( 'last_changed', $time, $group );
/**
* Fires after a cache group `last_changed` time is updated.
* This may occur multiple times per page load and registered
* actions must be performant.
*
* @since 6.3.0
*
* @param string $group The cache group name.
* @param int $time The new last changed time.
* @param int|false $previous_time The previous last changed time. False if not previously set.
*/
do_action( 'wp_cache_set_last_changed', $group, $time, $previous_time );
return $time;
} }
/** /**

View File

@ -96,7 +96,7 @@ function clean_network_cache( $ids ) {
do_action( 'clean_network_cache', $id ); do_action( 'clean_network_cache', $id );
} }
wp_cache_set( 'last_changed', microtime(), 'networks' ); wp_cache_set_last_changed( 'networks' );
} }
/** /**

View File

@ -1281,7 +1281,7 @@ function wp_update_blog_public_option_on_site_update( $site_id, $is_public ) {
* @since 5.1.0 * @since 5.1.0
*/ */
function wp_cache_set_sites_last_changed() { function wp_cache_set_sites_last_changed() {
wp_cache_set( 'last_changed', microtime(), 'sites' ); wp_cache_set_last_changed( 'sites' );
} }
/** /**

View File

@ -7805,7 +7805,7 @@ function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
* @since 5.0.0 * @since 5.0.0
*/ */
function wp_cache_set_posts_last_changed() { function wp_cache_set_posts_last_changed() {
wp_cache_set( 'last_changed', microtime(), 'posts' ); wp_cache_set_last_changed( 'posts' );
} }
/** /**

View File

@ -5038,7 +5038,7 @@ function is_term_publicly_viewable( $term ) {
* @since 5.0.0 * @since 5.0.0
*/ */
function wp_cache_set_terms_last_changed() { function wp_cache_set_terms_last_changed() {
wp_cache_set( 'last_changed', microtime(), 'terms' ); wp_cache_set_last_changed( 'terms' );
} }
/** /**

View File

@ -5024,5 +5024,5 @@ function wp_register_persisted_preferences_meta() {
* @since 6.3.0 * @since 6.3.0
*/ */
function wp_cache_set_users_last_changed() { function wp_cache_set_users_last_changed() {
wp_cache_set( 'last_changed', microtime(), 'users' ); wp_cache_set_last_changed( 'users' );
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.3-alpha-55701'; $wp_version = '6.3-alpha-55702';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.