From 82911b175649828c8d4e31a2becb32e99478baaa Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Fri, 21 Oct 2016 02:54:34 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@38849 git-svn-id: http://core.svn.wordpress.org/trunk@38792 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-comment-query.php | 13 +++---------- wp-includes/class-wp-network-query.php | 6 +----- wp-includes/class-wp-site-query.php | 6 +----- wp-includes/class-wp-term-query.php | 6 +----- wp-includes/functions.php | 20 ++++++++++++++++++++ wp-includes/general-template.php | 6 +----- wp-includes/post.php | 12 ++---------- wp-includes/version.php | 2 +- 8 files changed, 30 insertions(+), 41 deletions(-) diff --git a/wp-includes/class-wp-comment-query.php b/wp-includes/class-wp-comment-query.php index 195b96bb00..f6a17d7687 100644 --- a/wp-includes/class-wp-comment-query.php +++ b/wp-includes/class-wp-comment-query.php @@ -393,11 +393,8 @@ class WP_Comment_Query { // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); - $last_changed = wp_cache_get( 'last_changed', 'comment' ); - if ( ! $last_changed ) { - $last_changed = microtime(); - wp_cache_set( 'last_changed', $last_changed, 'comment' ); - } + $last_changed = wp_cache_get_last_changed( 'comment' ); + $cache_key = "get_comments:$key:$last_changed"; $cache_value = wp_cache_get( $cache_key, 'comment' ); @@ -972,11 +969,7 @@ class WP_Comment_Query { } $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); - $last_changed = wp_cache_get( 'last_changed', 'comment' ); - if ( ! $last_changed ) { - $last_changed = microtime(); - wp_cache_set( 'last_changed', $last_changed, 'comment' ); - } + $last_changed = wp_cache_get_last_changed( 'comment' ); // Fetch an entire level of the descendant tree at a time. $level = 0; diff --git a/wp-includes/class-wp-network-query.php b/wp-includes/class-wp-network-query.php index ffff041c4d..1bf38ad7e2 100644 --- a/wp-includes/class-wp-network-query.php +++ b/wp-includes/class-wp-network-query.php @@ -209,11 +209,7 @@ class WP_Network_Query { // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); - $last_changed = wp_cache_get( 'last_changed', 'networks' ); - if ( ! $last_changed ) { - $last_changed = microtime(); - wp_cache_set( 'last_changed', $last_changed, 'networks' ); - } + $last_changed = wp_cache_get_last_changed( 'networks' ); $cache_key = "get_network_ids:$key:$last_changed"; $cache_value = wp_cache_get( $cache_key, 'networks' ); diff --git a/wp-includes/class-wp-site-query.php b/wp-includes/class-wp-site-query.php index 3ef9a38f7e..8f52372d3b 100644 --- a/wp-includes/class-wp-site-query.php +++ b/wp-includes/class-wp-site-query.php @@ -245,11 +245,7 @@ class WP_Site_Query { // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); - $last_changed = wp_cache_get( 'last_changed', 'sites' ); - if ( ! $last_changed ) { - $last_changed = microtime(); - wp_cache_set( 'last_changed', $last_changed, 'sites' ); - } + $last_changed = wp_cache_get_last_changed( 'sites' ); $cache_key = "get_sites:$key:$last_changed"; $cache_value = wp_cache_get( $cache_key, 'sites' ); diff --git a/wp-includes/class-wp-term-query.php b/wp-includes/class-wp-term-query.php index f0df73f906..64b5578543 100644 --- a/wp-includes/class-wp-term-query.php +++ b/wp-includes/class-wp-term-query.php @@ -673,11 +673,7 @@ class WP_Term_Query { // $args can be anything. Only use the args defined in defaults to compute the key. $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request ); - $last_changed = wp_cache_get( 'last_changed', 'terms' ); - if ( ! $last_changed ) { - $last_changed = microtime(); - wp_cache_set( 'last_changed', $last_changed, 'terms' ); - } + $last_changed = wp_cache_get_last_changed( 'terms' ); $cache_key = "get_terms:$key:$last_changed"; $cache = wp_cache_get( $cache_key, 'terms' ); if ( false !== $cache ) { diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 1c86dc37ca..fb732e6590 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -5553,3 +5553,23 @@ function wp_generate_uuid4() { mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) ); } + +/** + * Get last changed date for the specified cache group. + * + * @since 4.7.0 + * + * @param $group Where the cache contents are grouped. + * + * @return string $last_changed UNIX timestamp with microseconds representing when the group was last changed. + */ +function wp_cache_get_last_changed( $group ) { + $last_changed = wp_cache_get( 'last_changed', $group ); + + if ( ! $last_changed ) { + $last_changed = microtime(); + wp_cache_set( 'last_changed', $last_changed, $group ); + } + + return $last_changed; +} \ No newline at end of file diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 5b36272270..c6fc86a09d 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -1713,11 +1713,7 @@ function wp_get_archives( $args = '' ) { $output = ''; - $last_changed = wp_cache_get( 'last_changed', 'posts' ); - if ( ! $last_changed ) { - $last_changed = microtime(); - wp_cache_set( 'last_changed', $last_changed, 'posts' ); - } + $last_changed = wp_cache_get_last_changed( 'posts' ); $limit = $r['limit']; diff --git a/wp-includes/post.php b/wp-includes/post.php index bd0c4bf6ef..6376e90bfd 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -4194,11 +4194,7 @@ function get_page( $page, $output = OBJECT, $filter = 'raw') { function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { global $wpdb; - $last_changed = wp_cache_get( 'last_changed', 'posts' ); - if ( false === $last_changed ) { - $last_changed = microtime(); - wp_cache_set( 'last_changed', $last_changed, 'posts' ); - } + $last_changed = wp_cache_get_last_changed( 'posts' ); $hash = md5( $page_path . serialize( $post_type ) ); $cache_key = "get_page_by_path:$hash:$last_changed"; @@ -4540,11 +4536,7 @@ function get_pages( $args = array() ) { // $args can be whatever, only use the args defined in defaults to compute the key. $key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) ); - $last_changed = wp_cache_get( 'last_changed', 'posts' ); - if ( ! $last_changed ) { - $last_changed = microtime(); - wp_cache_set( 'last_changed', $last_changed, 'posts' ); - } + $last_changed = wp_cache_get_last_changed( 'posts' ); $cache_key = "get_pages:$key:$last_changed"; if ( $cache = wp_cache_get( $cache_key, 'posts' ) ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index dab5826e77..be946801f9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-alpha-38848'; +$wp_version = '4.7-alpha-38849'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.