Make WordPress Core


Ignore:
Timestamp:
10/21/2016 02:53:19 AM (9 years ago)
Author:
jorbin
Message:

Cache API: introduce wp_cache_get_last_changed to improve DRY

One thing fairly common to the cache groups is a block of code to look to see when the cache was last changed, and if there isn't one, to set it for the current microtime(). It appears in 8 different places in core. This adds a new helper wp_cache_get_last_changed to DRY things up a bit.

Since wp-includes/cache.php isn't guaranteed to be loaded, this new function is in wp-includes/functions.php

Props spacedmonkey, desrosj.
Fixes #37464.

File:
1 edited

Legend:

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

    r38832 r38849  
    55545554    );
    55555555}
     5556
     5557/**
     5558 * Get last changed date for the specified cache group.
     5559 *
     5560 * @since 4.7.0
     5561 *
     5562 * @param $group Where the cache contents are grouped.
     5563 *
     5564 * @return string $last_changed UNIX timestamp with microseconds representing when the group was last changed.
     5565 */
     5566function wp_cache_get_last_changed( $group ) {
     5567    $last_changed = wp_cache_get( 'last_changed', $group );
     5568
     5569    if ( ! $last_changed ) {
     5570        $last_changed = microtime();
     5571        wp_cache_set( 'last_changed', $last_changed, $group );
     5572    }
     5573
     5574    return $last_changed;
     5575}
Note: See TracChangeset for help on using the changeset viewer.