From 37fa343b53cb5bab57a08c83e4eb01eed5fa05e9 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 2 Oct 2024 12:36:24 +0100 Subject: [PATCH 1/6] Fix static cache locking --- src/StaticCaching/Middleware/Cache.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index 3312ddab49..75ca9f482b 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -59,9 +59,13 @@ public function handle($request, Closure $next) $lock = $this->createLock($request); try { - return $lock->block($this->lockFor, fn () => $this->handleRequest($request, $next)); + $lock->block($this->lockFor / 2); + + return $this->handleRequest($request, $next); } catch (LockTimeoutException $e) { return $this->outputRefreshResponse($request); + } finally { + $lock->release(); } } From e9b59a5d1f088b34e1f5d09812324bcaa6fed3d5 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 2 Oct 2024 15:28:57 +0100 Subject: [PATCH 2/6] Check if response has been cached in previous lock iteration --- src/StaticCaching/Middleware/Cache.php | 29 ++++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index 75ca9f482b..25bf27b73e 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -48,29 +48,25 @@ public function __construct(Cacher $cacher, Session $nocache) */ public function handle($request, Closure $next) { - try { - if ($response = $this->attemptToGetCachedResponse($request)) { - return $response; - } - } catch (RegionNotFound $e) { - Log::debug("Static cache region [{$e->getRegion()}] not found on [{$request->fullUrl()}]. Serving uncached response."); + if ($response = $this->serveFromCache($request)) { + return $response; } $lock = $this->createLock($request); try { - $lock->block($this->lockFor / 2); - - return $this->handleRequest($request, $next); + return $lock->block($this->lockFor, fn () => $this->handleRequest($request, $next)); } catch (LockTimeoutException $e) { return $this->outputRefreshResponse($request); - } finally { - $lock->release(); } } private function handleRequest($request, Closure $next) { + if ($response = $this->serveFromCache($request)) { + return $response; + } + $response = $next($request); if ($this->shouldBeCached($request, $response)) { @@ -101,6 +97,17 @@ private function copyError($request, $response) } } + private function serveFromCache($request) + { + try { + if ($response = $this->attemptToGetCachedResponse($request)) { + return $response; + } + } catch (RegionNotFound $e) { + Log::debug("Static cache region [{$e->getRegion()}] not found on [{$request->fullUrl()}]. Serving uncached response."); + } + } + private function attemptToGetCachedResponse($request) { if ($this->canBeCached($request) && $this->cacher->hasCachedPage($request)) { From dd2bce432a30ed7347578ccd531f746a66dea61e Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 2 Oct 2024 15:36:09 +0100 Subject: [PATCH 3/6] Prevent logging "Static cache loaded" when we're intentionally loading from the cache --- src/StaticCaching/Cachers/FileCacher.php | 14 +++++++++++++- src/StaticCaching/Middleware/Cache.php | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/StaticCaching/Cachers/FileCacher.php b/src/StaticCaching/Cachers/FileCacher.php index 7be32839a5..071f30701e 100644 --- a/src/StaticCaching/Cachers/FileCacher.php +++ b/src/StaticCaching/Cachers/FileCacher.php @@ -36,6 +36,11 @@ class FileCacher extends AbstractCacher */ private $nocachePlaceholder; + /** + * @var bool + */ + private $shouldLog = true; + /** * @param array $config */ @@ -71,6 +76,11 @@ public function cachePage(Request $request, $content) $this->cacheUrl($this->makeHash($url), ...$this->getPathAndDomain($url)); } + public function preventLogging() + { + $this->shouldLog = false; + } + /** * @return Page */ @@ -80,7 +90,9 @@ public function getCachedPage(Request $request) $path = $this->getFilePath($url); - if (! $this->isLongQueryStringPath($path)) { + ray($this->shouldLog); + + if ($this->shouldLog && ! $this->isLongQueryStringPath($path)) { Log::debug('Static cache loaded ['.$url.'] If you are seeing this, your server rewrite rules have not been set up correctly.'); } diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index 25bf27b73e..75594e8543 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -14,6 +14,7 @@ use Statamic\Statamic; use Statamic\StaticCaching\Cacher; use Statamic\StaticCaching\Cachers\ApplicationCacher; +use Statamic\StaticCaching\Cachers\FileCacher; use Statamic\StaticCaching\Cachers\NullCacher; use Statamic\StaticCaching\NoCache\RegionNotFound; use Statamic\StaticCaching\NoCache\Session; @@ -63,6 +64,10 @@ public function handle($request, Closure $next) private function handleRequest($request, Closure $next) { + if ($this->cacher instanceof FileCacher) { + $this->cacher->preventLogging(); + } + if ($response = $this->serveFromCache($request)) { return $response; } From f2faa358d6ea8d2ec6651785ca04dc566c381f3d Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 2 Oct 2024 10:38:01 -0400 Subject: [PATCH 4/6] remove ray --- src/StaticCaching/Cachers/FileCacher.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/StaticCaching/Cachers/FileCacher.php b/src/StaticCaching/Cachers/FileCacher.php index 071f30701e..4f5bbac336 100644 --- a/src/StaticCaching/Cachers/FileCacher.php +++ b/src/StaticCaching/Cachers/FileCacher.php @@ -90,8 +90,6 @@ public function getCachedPage(Request $request) $path = $this->getFilePath($url); - ray($this->shouldLog); - if ($this->shouldLog && ! $this->isLongQueryStringPath($path)) { Log::debug('Static cache loaded ['.$url.'] If you are seeing this, your server rewrite rules have not been set up correctly.'); } From 76bb2c45a81ff549f1a8fe6d92e5b1df44810dd5 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 2 Oct 2024 10:48:25 -0400 Subject: [PATCH 5/6] nitpick --- src/StaticCaching/Cachers/FileCacher.php | 8 ++++---- src/StaticCaching/Middleware/Cache.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/StaticCaching/Cachers/FileCacher.php b/src/StaticCaching/Cachers/FileCacher.php index 4f5bbac336..0a0a594b81 100644 --- a/src/StaticCaching/Cachers/FileCacher.php +++ b/src/StaticCaching/Cachers/FileCacher.php @@ -39,7 +39,7 @@ class FileCacher extends AbstractCacher /** * @var bool */ - private $shouldLog = true; + private $logRewriteWarning = true; /** * @param array $config @@ -76,9 +76,9 @@ public function cachePage(Request $request, $content) $this->cacheUrl($this->makeHash($url), ...$this->getPathAndDomain($url)); } - public function preventLogging() + public function preventLoggingRewriteWarning() { - $this->shouldLog = false; + $this->logRewriteWarning = false; } /** @@ -90,7 +90,7 @@ public function getCachedPage(Request $request) $path = $this->getFilePath($url); - if ($this->shouldLog && ! $this->isLongQueryStringPath($path)) { + if ($this->logRewriteWarning && ! $this->isLongQueryStringPath($path)) { Log::debug('Static cache loaded ['.$url.'] If you are seeing this, your server rewrite rules have not been set up correctly.'); } diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index 75594e8543..ddca77a362 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -49,7 +49,7 @@ public function __construct(Cacher $cacher, Session $nocache) */ public function handle($request, Closure $next) { - if ($response = $this->serveFromCache($request)) { + if ($response = $this->attemptToServeCachedResponse($request)) { return $response; } @@ -65,10 +65,10 @@ public function handle($request, Closure $next) private function handleRequest($request, Closure $next) { if ($this->cacher instanceof FileCacher) { - $this->cacher->preventLogging(); + $this->cacher->preventLoggingRewriteWarning(); } - if ($response = $this->serveFromCache($request)) { + if ($response = $this->attemptToServeCachedResponse($request)) { return $response; } @@ -102,7 +102,7 @@ private function copyError($request, $response) } } - private function serveFromCache($request) + private function attemptToServeCachedResponse($request) { try { if ($response = $this->attemptToGetCachedResponse($request)) { From 4ed36bafec882b4bd55dc393696c66d77dc4b8f7 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 2 Oct 2024 10:48:34 -0400 Subject: [PATCH 6/6] comment --- src/StaticCaching/Middleware/Cache.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index ddca77a362..77ed71fe5d 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -64,6 +64,9 @@ public function handle($request, Closure $next) private function handleRequest($request, Closure $next) { + // When the file driver loads a cached page, it logs a debug message explaining + // that it's being serving via PHP and rewrite rules are not set up correctly. + // Since we're intentionally doing it here, we should prevent that warning. if ($this->cacher instanceof FileCacher) { $this->cacher->preventLoggingRewriteWarning(); }