Make WordPress Core

Changeset 55702


Ignore:
Timestamp:
05/02/2023 11:24:52 AM (14 months ago)
Author:
spacedmonkey
Message:

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.

Location:
trunk/src/wp-includes
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r55641 r55702  
    39193919 */
    39203920function wp_cache_set_comments_last_changed() {
    3921     wp_cache_set( 'last_changed', microtime(), 'comment' );
     3921    wp_cache_set_last_changed( 'comment' );
    39223922}
    39233923
  • trunk/src/wp-includes/functions.php

    r55642 r55702  
    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 );
    7686     }
    7687 
    7688     return $last_changed;
     7683    if ( $last_changed ) {
     7684        return $last_changed;
     7685    }
     7686
     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 */
     7698function 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
  • trunk/src/wp-includes/ms-network.php

    r54637 r55702  
    9797    }
    9898
    99     wp_cache_set( 'last_changed', microtime(), 'networks' );
     99    wp_cache_set_last_changed( 'networks' );
    100100}
    101101
  • trunk/src/wp-includes/ms-site.php

    r55526 r55702  
    12821282 */
    12831283function wp_cache_set_sites_last_changed() {
    1284     wp_cache_set( 'last_changed', microtime(), 'sites' );
     1284    wp_cache_set_last_changed( 'sites' );
    12851285}
    12861286
  • trunk/src/wp-includes/post.php

    r55701 r55702  
    78067806 */
    78077807function wp_cache_set_posts_last_changed() {
    7808     wp_cache_set( 'last_changed', microtime(), 'posts' );
     7808    wp_cache_set_last_changed( 'posts' );
    78097809}
    78107810
  • trunk/src/wp-includes/taxonomy.php

    r55693 r55702  
    50395039 */
    50405040function wp_cache_set_terms_last_changed() {
    5041     wp_cache_set( 'last_changed', microtime(), 'terms' );
     5041    wp_cache_set_last_changed( 'terms' );
    50425042}
    50435043
  • trunk/src/wp-includes/user.php

    r55657 r55702  
    50255025 */
    50265026function wp_cache_set_users_last_changed() {
    5027     wp_cache_set( 'last_changed', microtime(), 'users' );
    5028 }
     5027    wp_cache_set_last_changed( 'users' );
     5028}
Note: See TracChangeset for help on using the changeset viewer.