Make WordPress Core


Ignore:
Timestamp:
05/02/2023 11:24:52 AM (3 years 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.