From befe5ab048faf7f9aa23b9d67e613794bdb3e8f3 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Mon, 29 Mar 2021 13:35:23 +0200 Subject: [PATCH 1/2] Optimize genlocks usage Currently many requests unnecesarily get no batcache serving or headers set due to over-generic genlocks. At the moment a single genlock goves a url path, like `/wp/v2/posts`, which means any requests to `/wp/v2/posts?per_page=1` will not get cached, or send cache headers while the first request to `/wp/v2/posts` is processing. I see no reason why genlocks and versions should not be tracked against the exact batcache cache key, rather than URL-only. --- advanced-cache.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/advanced-cache.php b/advanced-cache.php index 3679a55..f360484 100644 --- a/advanced-cache.php +++ b/advanced-cache.php @@ -551,11 +551,14 @@ function add_debug_html_to_output( $debug_html ) { // Recreate the permalink from the URL $batcache->permalink = 'http://' . $batcache->keys['host'] . $batcache->keys['path'] . ( isset($batcache->keys['query']['p']) ? "?p=" . $batcache->keys['query']['p'] : '' ); -$batcache->url_key = md5($batcache->permalink); +$batcache->generate_keys(); +// Use the whole batcache key as the "url_key". The original version of Batcache would only track the genlock and version +// against the current URL path, rather than the whole batcache variant. This would mean that many requests to a single path +// (like a REST API endpoint) would all share a genlock. +$batcache->url_key = $batcache->key; $batcache->configure_groups(); $batcache->url_version = (int) wp_cache_get("{$batcache->url_key}_version", $batcache->group); $batcache->do_variants(); -$batcache->generate_keys(); // Get the batcache $batcache->cache = wp_cache_get($batcache->key, $batcache->group); @@ -690,6 +693,11 @@ function add_debug_html_to_output( $debug_html ) { die($batcache->cache['output']); } +// If we were not able to get a genlock, +if ( $batcache->do && ! $batcache->genlock && $batcache->add_hit_status_header ) { + header( 'X-Batcache: BYPASS' ); + header( 'X-Batcache-Reason: No Genlock' ); +} // Didn't meet the minimum condition? if ( ! $batcache->do || ! $batcache->genlock ) return; From 5e8fbb7cb676c1f9feb3e5dabc9c9be6876d4fa5 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Thu, 1 Apr 2021 14:06:00 +0200 Subject: [PATCH 2/2] Switch genlock cache key rather than url_key --- advanced-cache.php | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/advanced-cache.php b/advanced-cache.php index f360484..7a978d8 100644 --- a/advanced-cache.php +++ b/advanced-cache.php @@ -174,7 +174,7 @@ function ob($output) { header( 'X-Batcache: BYPASS' ); header( 'X-Batcache-Reason: Canceled' ); } - wp_cache_delete( "{$this->url_key}_genlock", $this->group ); + wp_cache_delete( "{$this->key}_genlock", $this->group ); return $output; } @@ -186,7 +186,7 @@ function ob($output) { $this->configure_groups(); if ( $this->cancel !== false ) { - wp_cache_delete( "{$this->url_key}_genlock", $this->group ); + wp_cache_delete( "{$this->key}_genlock", $this->group ); return $output; } @@ -204,7 +204,7 @@ function ob($output) { header( 'X-Batcache-Reason: No content' ); } - wp_cache_delete( "{$this->url_key}_genlock", $this->group ); + wp_cache_delete( "{$this->key}_genlock", $this->group ); return; } @@ -220,7 +220,7 @@ function ob($output) { header( 'X-Batcache-Reason: Bad status code' ); } - wp_cache_delete( "{$this->url_key}_genlock", $this->group ); + wp_cache_delete( "{$this->key}_genlock", $this->group ); return $output; } @@ -262,7 +262,7 @@ function ob($output) { header( 'X-Batcache-Reason: Set-Cookie' ); } - wp_cache_delete( "{$this->url_key}_genlock", $this->group ); + wp_cache_delete( "{$this->key}_genlock", $this->group ); return $output; } @@ -276,7 +276,7 @@ function ob($output) { wp_cache_set($this->key, $this->cache, $this->group, $this->max_age + $this->seconds + 30); // Unlock regeneration - wp_cache_delete("{$this->url_key}_genlock", $this->group); + wp_cache_delete("{$this->key}_genlock", $this->group); if ( $this->cache_control ) { // Don't clobber Last-Modified header if already set, e.g. by WP::send_headers() @@ -551,14 +551,11 @@ function add_debug_html_to_output( $debug_html ) { // Recreate the permalink from the URL $batcache->permalink = 'http://' . $batcache->keys['host'] . $batcache->keys['path'] . ( isset($batcache->keys['query']['p']) ? "?p=" . $batcache->keys['query']['p'] : '' ); -$batcache->generate_keys(); -// Use the whole batcache key as the "url_key". The original version of Batcache would only track the genlock and version -// against the current URL path, rather than the whole batcache variant. This would mean that many requests to a single path -// (like a REST API endpoint) would all share a genlock. -$batcache->url_key = $batcache->key; +$batcache->url_key = md5($batcache->permalink); $batcache->configure_groups(); $batcache->url_version = (int) wp_cache_get("{$batcache->url_key}_version", $batcache->group); $batcache->do_variants(); +$batcache->generate_keys(); // Get the batcache $batcache->cache = wp_cache_get($batcache->key, $batcache->group); @@ -589,7 +586,7 @@ function add_debug_html_to_output( $debug_html ) { // Obtain cache generation lock if ( $batcache->do ) { - $batcache->genlock = wp_cache_add("{$batcache->url_key}_genlock", 1, $batcache->group, 10); + $batcache->genlock = wp_cache_add("{$batcache->key}_genlock", 1, $batcache->group, 10); } if ( isset( $batcache->cache['time'] ) && // We have cache