Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Fix static cache locking #10887

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading