Make WordPress Core


Ignore:
Timestamp:
10/21/2016 02:53:19 AM (8 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/post.php

    r38832 r38849  
    41954195    global $wpdb;
    41964196
    4197     $last_changed = wp_cache_get( 'last_changed', 'posts' );
    4198     if ( false === $last_changed ) {
    4199         $last_changed = microtime();
    4200         wp_cache_set( 'last_changed', $last_changed, 'posts' );
    4201     }
     4197    $last_changed = wp_cache_get_last_changed( 'posts' );
    42024198
    42034199    $hash = md5( $page_path . serialize( $post_type ) );
     
    45414537    // $args can be whatever, only use the args defined in defaults to compute the key.
    45424538    $key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) );
    4543     $last_changed = wp_cache_get( 'last_changed', 'posts' );
    4544     if ( ! $last_changed ) {
    4545         $last_changed = microtime();
    4546         wp_cache_set( 'last_changed', $last_changed, 'posts' );
    4547     }
     4539    $last_changed = wp_cache_get_last_changed( 'posts' );
    45484540
    45494541    $cache_key = "get_pages:$key:$last_changed";
Note: See TracChangeset for help on using the changeset viewer.