diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 195b96bb00..f6a17d7687 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/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/src/wp-includes/class-wp-network-query.php b/src/wp-includes/class-wp-network-query.php index ffff041c4d..1bf38ad7e2 100644 --- a/src/wp-includes/class-wp-network-query.php +++ b/src/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/src/wp-includes/class-wp-site-query.php b/src/wp-includes/class-wp-site-query.php index 3ef9a38f7e..8f52372d3b 100644 --- a/src/wp-includes/class-wp-site-query.php +++ b/src/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/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index f0df73f906..64b5578543 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/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/src/wp-includes/functions.php b/src/wp-includes/functions.php index 1c86dc37ca..fb732e6590 100644 --- a/src/wp-includes/functions.php +++ b/src/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/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 5b36272270..c6fc86a09d 100644 --- a/src/wp-includes/general-template.php +++ b/src/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/src/wp-includes/post.php b/src/wp-includes/post.php index bd0c4bf6ef..6376e90bfd 100644 --- a/src/wp-includes/post.php +++ b/src/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' ) ) {