Make WordPress Core

Changeset 38849


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

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

Legend:

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

    r38768 r38849  
    394394                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    395395                $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
    396                 $last_changed = wp_cache_get( 'last_changed', 'comment' );
    397                 if ( ! $last_changed ) {
    398                         $last_changed = microtime();
    399                         wp_cache_set( 'last_changed', $last_changed, 'comment' );
    400                 }
     396                $last_changed = wp_cache_get_last_changed( 'comment' );
     397
    401398
    402399                $cache_key   = "get_comments:$key:$last_changed";
     
    973970
    974971                $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
    975                 $last_changed = wp_cache_get( 'last_changed', 'comment' );
    976                 if ( ! $last_changed ) {
    977                         $last_changed = microtime();
    978                         wp_cache_set( 'last_changed', $last_changed, 'comment' );
    979                 }
     972                $last_changed = wp_cache_get_last_changed( 'comment' );
    980973
    981974                // Fetch an entire level of the descendant tree at a time.
  • trunk/src/wp-includes/class-wp-network-query.php

    r38768 r38849  
    210210                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    211211                $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
    212                 $last_changed = wp_cache_get( 'last_changed', 'networks' );
    213                 if ( ! $last_changed ) {
    214                         $last_changed = microtime();
    215                         wp_cache_set( 'last_changed', $last_changed, 'networks' );
    216                 }
     212                $last_changed = wp_cache_get_last_changed( 'networks' );
    217213
    218214                $cache_key = "get_network_ids:$key:$last_changed";
  • trunk/src/wp-includes/class-wp-site-query.php

    r38768 r38849  
    246246                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    247247                $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
    248                 $last_changed = wp_cache_get( 'last_changed', 'sites' );
    249                 if ( ! $last_changed ) {
    250                         $last_changed = microtime();
    251                         wp_cache_set( 'last_changed', $last_changed, 'sites' );
    252                 }
     248                $last_changed = wp_cache_get_last_changed( 'sites' );
    253249
    254250                $cache_key = "get_sites:$key:$last_changed";
  • trunk/src/wp-includes/class-wp-term-query.php

    r38784 r38849  
    674674                // $args can be anything. Only use the args defined in defaults to compute the key.
    675675                $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request );
    676                 $last_changed = wp_cache_get( 'last_changed', 'terms' );
    677                 if ( ! $last_changed ) {
    678                         $last_changed = microtime();
    679                         wp_cache_set( 'last_changed', $last_changed, 'terms' );
    680                 }
     676                $last_changed = wp_cache_get_last_changed( 'terms' );
    681677                $cache_key = "get_terms:$key:$last_changed";
    682678                $cache = wp_cache_get( $cache_key, 'terms' );
  • 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}
  • trunk/src/wp-includes/general-template.php

    r38826 r38849  
    17141714        $output = '';
    17151715
    1716         $last_changed = wp_cache_get( 'last_changed', 'posts' );
    1717         if ( ! $last_changed ) {
    1718                 $last_changed = microtime();
    1719                 wp_cache_set( 'last_changed', $last_changed, 'posts' );
    1720         }
     1716        $last_changed = wp_cache_get_last_changed( 'posts' );
    17211717
    17221718        $limit = $r['limit'];
  • 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.