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.



git-svn-id: https://develop.svn.wordpress.org/trunk@38849 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Aaron Jorbin
2016-10-21 02:53:19 +00:00
parent ce21d8210a
commit a8aa7b0ca2
7 changed files with 29 additions and 40 deletions

View File

@@ -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' ) ) {