Skip to content

Commit

Permalink
[5.x] Fix static cache locking (#10887)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
duncanmcclean and jasonvarga authored Oct 2, 2024
1 parent f2ab0de commit 346acb7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/StaticCaching/Cachers/FileCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class FileCacher extends AbstractCacher
*/
private $nocachePlaceholder;

/**
* @var bool
*/
private $logRewriteWarning = true;

/**
* @param array $config
*/
Expand Down Expand Up @@ -71,6 +76,11 @@ public function cachePage(Request $request, $content)
$this->cacheUrl($this->makeHash($url), ...$this->getPathAndDomain($url));
}

public function preventLoggingRewriteWarning()
{
$this->logRewriteWarning = false;
}

/**
* @return Page
*/
Expand All @@ -80,7 +90,7 @@ public function getCachedPage(Request $request)

$path = $this->getFilePath($url);

if (! $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.');
}

Expand Down
31 changes: 25 additions & 6 deletions src/StaticCaching/Middleware/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,12 +49,8 @@ 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->attemptToServeCachedResponse($request)) {
return $response;
}

$lock = $this->createLock($request);
Expand All @@ -67,6 +64,17 @@ 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();
}

if ($response = $this->attemptToServeCachedResponse($request)) {
return $response;
}

$response = $next($request);

if ($this->shouldBeCached($request, $response)) {
Expand Down Expand Up @@ -97,6 +105,17 @@ private function copyError($request, $response)
}
}

private function attemptToServeCachedResponse($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)) {
Expand Down

0 comments on commit 346acb7

Please sign in to comment.