Skip to content
Snippets Groups Projects
Commit 58bfdb6e authored by Erick Hitter's avatar Erick Hitter
Browse files

Add tracking of cache hits and misses, and the attendant function that the Debug Bar plugin uses.

parent 5355b183
No related branches found
No related tags found
No related merge requests found
...@@ -93,17 +93,6 @@ function wp_cache_get( $key, $group = '' ) { ...@@ -93,17 +93,6 @@ function wp_cache_get( $key, $group = '' ) {
return $wp_object_cache->get( $key, $group ); return $wp_object_cache->get( $key, $group );
} }
/**
* Get server pool statistics.
*
* @return array Array of server statistics, one entry per server.
*/
function wp_cache_get_stats() {
global $wp_object_cache;
return $wp_object_cache->stats();
}
/** /**
* Increment a numeric item's value. * Increment a numeric item's value.
* *
...@@ -235,6 +224,20 @@ class WP_Object_Cache { ...@@ -235,6 +224,20 @@ class WP_Object_Cache {
*/ */
public $blog_prefix = ''; public $blog_prefix = '';
/**
* Track how many requests were found in cache
*
* @var int
*/
public $cache_hits = 0;
/**
* Track how may requests were not cached
*
* @var int
*/
public $cache_misses = 0;
/** /**
* Instantiate the Redis class. * Instantiate the Redis class.
* *
...@@ -384,15 +387,19 @@ class WP_Object_Cache { ...@@ -384,15 +387,19 @@ class WP_Object_Cache {
if ( in_array( $group, $this->no_redis_groups ) ) { if ( in_array( $group, $this->no_redis_groups ) ) {
if ( isset( $this->cache[$derived_key] ) ) { if ( isset( $this->cache[$derived_key] ) ) {
$this->cache_hits++;
return is_object( $this->cache[$derived_key] ) ? clone $this->cache[$derived_key] : $this->cache[$derived_key]; return is_object( $this->cache[$derived_key] ) ? clone $this->cache[$derived_key] : $this->cache[$derived_key];
} elseif ( in_array( $group, $this->no_redis_groups ) ) { } else {
$this->cache_misses++;
return false; return false;
} }
} }
if ( $this->redis->exists( $derived_key ) ) { if ( $this->redis->exists( $derived_key ) ) {
$this->cache_hits++;
$value = $this->restore_value_from_redis( $this->redis->get( $derived_key ) ); $value = $this->restore_value_from_redis( $this->redis->get( $derived_key ) );
} else { } else {
$this->cache_misses;
return false; return false;
} }
...@@ -431,8 +438,6 @@ class WP_Object_Cache { ...@@ -431,8 +438,6 @@ class WP_Object_Cache {
$result = $this->redis->set( $derived_key, $this->prepare_value_for_redis( $value ) ); $result = $this->redis->set( $derived_key, $this->prepare_value_for_redis( $value ) );
} }
$this->add_to_internal_cache( $derived_key, $value );
return $result; return $result;
} }
...@@ -494,6 +499,26 @@ class WP_Object_Cache { ...@@ -494,6 +499,26 @@ class WP_Object_Cache {
return $result; return $result;
} }
/**
* Render data about current cache requests
*
* @return string
*/
public function stats() {
?><p>
<strong>Cache Hits:</strong> <?php echo number_format_i18n( $this->cache_hits ); ?><br />
<strong>Cache Misses:</strong> <?php echo number_format_i18n( $this->cache_misses ); ?><br />
</p>
<p>&nbsp;</p>
<p><strong>Caches Retrieved:</strong></p>
<ul>
<li><em>prefix:group:key - size in kilobytes</em></li>
<?php foreach ( $this->cache as $group => $cache ) : ?>
<li><?php echo esc_html( $group ); ?> - <?php echo number_format_i18n( strlen( serialize( $cache ) ) / 1024, 2 ); ?> kb</li>
<?php endforeach; ?>
</ul><?php
}
/** /**
* Builds a key for the cached object using the blog_id, key, and group values. * Builds a key for the cached object using the blog_id, key, and group values.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment