From 9b0ebc72f436101c9a2c666136301125ecd8a2f1 Mon Sep 17 00:00:00 2001 From: Erick Hitter <ehitter@gmail.com> Date: Wed, 26 Feb 2014 19:55:48 -0800 Subject: [PATCH] Provide option to display generation stats. Fixes #2. --- index-wp-redis.php | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/index-wp-redis.php b/index-wp-redis.php index 6078c12..8e7beb4 100644 --- a/index-wp-redis.php +++ b/index-wp-redis.php @@ -11,6 +11,7 @@ global $wp_redis_cache_config; $wp_redis_cache_config = array( 'debug' => false, 'debug_messages' => '', + 'stats' => false, 'cache' => false, 'server_ip' => '127.0.0.1', 'redis_server' => '127.0.0.1', @@ -37,7 +38,7 @@ $wp_redis_cache_config['current_url'] = wp_redis_cache_get_clean_url( $wp_redis_ $wp_redis_cache_config['redis_key'] = md5( $wp_redis_cache_config['current_url'] ); // Start the timer so we can track the page load time -if ( $wp_redis_cache_config['debug'] ) { +if ( $wp_redis_cache_config['debug'] || $wp_redis_cache_config['stats'] ) { $start = microtime(); } @@ -60,6 +61,18 @@ function wp_redis_cache_get_micro_time( $time ) { return ( (float) $usec + (float) $sec ); } +/** + * Count seconds elapsed between two microtime() timestampes + * + * @param string $start + * @param string $end + * @param int $precision + * @return float + */ +function wp_redis_cache_time_elapsed( $start, $end ) { + return round( @wp_redis_cache_get_micro_time( $end ) - @wp_redis_cache_get_micro_time( $start ), 5 ); +} + /** * Is the current request a refresh request with the correct secret key? * @@ -271,6 +284,12 @@ try { $wp_redis_cache_config['cached'] = true; echo trim( $redis->get( $wp_redis_cache_config['redis_key'] ) ); + + // Display generation stats if requested + if ( $wp_redis_cache_config['stats'] ) { + echo "\n<!-- Page cached via Redis using the WP Redis Cache plugin. -->"; + echo "\n<!-- Retrieved from cache in " . wp_redis_cache_time_elapsed( $start, microtime() ) . " seconds. -->"; + } // If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page } elseif ( $_SERVER['REMOTE_ADDR'] != $wp_redis_cache_config['server_ip'] ) { if ( false === strstr( $wp_redis_cache_config['current_url'], 'preview=true' ) ) { @@ -289,6 +308,12 @@ try { $markup_to_cache = trim( ob_get_clean() ); echo $markup_to_cache; + // Display generation stats if requested + if ( $wp_redis_cache_config['stats'] ) { + echo "\n<!-- Page NOT cached via Redis using the WP Redis Cache plugin. -->"; + echo "\n<!-- Generated and cached in " . wp_redis_cache_time_elapsed( $start, microtime() ) . " seconds. -->"; + } + // Cache rendered page if appropriate if ( ! is_404() && ! is_search() ) { // Is unlimited cache life requested? @@ -335,8 +360,8 @@ try { */ if ( $wp_redis_cache_config['debug'] ) { $end = microtime(); - $time = @wp_redis_cache_get_micro_time( $end ) - @wp_redis_cache_get_micro_time( $start ); - $wp_redis_cache_config['debug_messages'] .= "<!-- Cache system by Erick Hitter. Page generated in " . round($time, 5) . " seconds. -->\n"; + $time = wp_redis_cache_time_elapsed( $start, $end ); + $wp_redis_cache_config['debug_messages'] .= "<!-- WP Redis Cache by Erick Hitter. Page generated in " . $time . " seconds. -->\n"; $wp_redis_cache_config['debug_messages'] .= "<!-- Site was cached = " . $wp_redis_cache_config['cached'] . " -->\n"; if ( isset( $wp_redis_cache_config['cache_duration'] ) ) { $wp_redis_cache_config['debug_messages'] .= "<!-- wp-redis-cache-seconds = " . $wp_redis_cache_config['cache_duration'] . " -->\n"; -- GitLab