Skip to content

Commit 73dbbe5

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@55702 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9ef194b commit 73dbbe5

7 files changed

Lines changed: 40 additions & 10 deletions

File tree

‎src/wp-includes/comment.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3918,7 +3918,7 @@ function wp_comments_personal_data_eraser( $email_address, $page = 1 ) {
39183918
* @since 5.0.0
39193919
*/
39203920
function wp_cache_set_comments_last_changed() {
3921-
wp_cache_set( 'last_changed', microtime(), 'comment' );
3921+
wp_cache_set_last_changed( 'comment' );
39223922
}
39233923

39243924
/**

‎src/wp-includes/functions.php‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7680,12 +7680,42 @@ function wp_unique_id( $prefix = '' ) {
76807680
function wp_cache_get_last_changed( $group ) {
76817681
$last_changed = wp_cache_get( 'last_changed', $group );
76827682

7683-
if ( ! $last_changed ) {
7684-
$last_changed = microtime();
7685-
wp_cache_set( 'last_changed', $last_changed, $group );
7683+
if ( $last_changed ) {
7684+
return $last_changed;
76867685
}
76877686

7688-
return $last_changed;
7687+
return wp_cache_set_last_changed( $group );
7688+
}
7689+
7690+
/**
7691+
* Sets last changed date for the specified cache group to now.
7692+
*
7693+
* @since 6.3.0
7694+
*
7695+
* @param string $group Where the cache contents are grouped.
7696+
* @return string UNIX timestamp when the group was last changed.
7697+
*/
7698+
function wp_cache_set_last_changed( $group ) {
7699+
$previous_time = wp_cache_get( 'last_changed', $group );
7700+
7701+
$time = microtime();
7702+
7703+
wp_cache_set( 'last_changed', $time, $group );
7704+
7705+
/**
7706+
* Fires after a cache group `last_changed` time is updated.
7707+
* This may occur multiple times per page load and registered
7708+
* actions must be performant.
7709+
*
7710+
* @since 6.3.0
7711+
*
7712+
* @param string $group The cache group name.
7713+
* @param int $time The new last changed time.
7714+
* @param int|false $previous_time The previous last changed time. False if not previously set.
7715+
*/
7716+
do_action( 'wp_cache_set_last_changed', $group, $time, $previous_time );
7717+
7718+
return $time;
76897719
}
76907720

76917721
/**

‎src/wp-includes/ms-network.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function clean_network_cache( $ids ) {
9696
do_action( 'clean_network_cache', $id );
9797
}
9898

99-
wp_cache_set( 'last_changed', microtime(), 'networks' );
99+
wp_cache_set_last_changed( 'networks' );
100100
}
101101

102102
/**

‎src/wp-includes/ms-site.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ function wp_update_blog_public_option_on_site_update( $site_id, $is_public ) {
12811281
* @since 5.1.0
12821282
*/
12831283
function wp_cache_set_sites_last_changed() {
1284-
wp_cache_set( 'last_changed', microtime(), 'sites' );
1284+
wp_cache_set_last_changed( 'sites' );
12851285
}
12861286

12871287
/**

‎src/wp-includes/post.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7805,7 +7805,7 @@ function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
78057805
* @since 5.0.0
78067806
*/
78077807
function wp_cache_set_posts_last_changed() {
7808-
wp_cache_set( 'last_changed', microtime(), 'posts' );
7808+
wp_cache_set_last_changed( 'posts' );
78097809
}
78107810

78117811
/**

‎src/wp-includes/taxonomy.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5038,7 +5038,7 @@ function is_term_publicly_viewable( $term ) {
50385038
* @since 5.0.0
50395039
*/
50405040
function wp_cache_set_terms_last_changed() {
5041-
wp_cache_set( 'last_changed', microtime(), 'terms' );
5041+
wp_cache_set_last_changed( 'terms' );
50425042
}
50435043

50445044
/**

‎src/wp-includes/user.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5024,5 +5024,5 @@ function wp_register_persisted_preferences_meta() {
50245024
* @since 6.3.0
50255025
*/
50265026
function wp_cache_set_users_last_changed() {
5027-
wp_cache_set( 'last_changed', microtime(), 'users' );
5027+
wp_cache_set_last_changed( 'users' );
50285028
}

0 commit comments

Comments
 (0)