From 6810ad5f84224f9c4a88b197e15c7fb8463fe528 Mon Sep 17 00:00:00 2001 From: edalzell Date: Sat, 21 Nov 2026 12:32:08 -0700 Subject: [PATCH 1/2] Wipe the static cache store before deleting files on flush --- src/StaticCaching/Cachers/FileCacher.php | 10 ++++++---- tests/StaticCaching/FileCacherTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/StaticCaching/Cachers/FileCacher.php b/src/StaticCaching/Cachers/FileCacher.php index 66630c2ebf1..73c663dd0e9 100644 --- a/src/StaticCaching/Cachers/FileCacher.php +++ b/src/StaticCaching/Cachers/FileCacher.php @@ -128,13 +128,15 @@ public function getGeneratedAt(string $url): ?int */ public function flush() { - foreach ($this->getCachePaths() as $path) { - $this->writer->flush($path); - } - + // Wipe the store before the files, so any page written while the + // files are being deleted keeps the nocache regions it just stored. $this->flushUrls(); $this->flushDedicatedStore(); + + foreach ($this->getCachePaths() as $path) { + $this->writer->flush($path); + } } /** diff --git a/tests/StaticCaching/FileCacherTest.php b/tests/StaticCaching/FileCacherTest.php index d5679b37ed5..ecaa66b9376 100644 --- a/tests/StaticCaching/FileCacherTest.php +++ b/tests/StaticCaching/FileCacherTest.php @@ -181,6 +181,30 @@ public function flushing_wipes_the_dedicated_static_cache_store() $this->assertNull($cache->get('untracked')); } + #[Test] + public function flushing_wipes_the_store_before_deleting_the_files() + { + config(['cache.stores.static_cache' => ['driver' => 'array']]); + $cache = Cache::store('static_cache'); + $cache->forever('nocache::session.abc', ['regions' => ['one']]); + + $writer = \Mockery::mock(Writer::class); + $cacher = $this->fileCacher(['path' => 'test/path', 'base_url' => 'http://example.com'], $writer, $cache); + $cacher->cacheUrl('one', '/one'); + + $stateWhenDeletingFiles = null; + $writer->shouldReceive('flush')->once()->andReturnUsing(function () use ($cache, $cacher, &$stateWhenDeletingFiles) { + $stateWhenDeletingFiles = [ + 'session' => $cache->get('nocache::session.abc'), + 'urls' => $cacher->getUrls()->all(), + ]; + }); + + $cacher->flush(); + + $this->assertSame(['session' => null, 'urls' => []], $stateWhenDeletingFiles); + } + #[Test] public function invalidating_a_url_thats_not_cached_will_do_nothing() { From 890c28054ae9c73b0a8a263b12f100746ddaca6b Mon Sep 17 00:00:00 2001 From: edalzell Date: Sat, 21 Nov 2026 12:36:36 -0700 Subject: [PATCH 2/2] Test the flush race rather than the call order --- tests/StaticCaching/FileCacherTest.php | 57 ++++++++++++++++++-------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/tests/StaticCaching/FileCacherTest.php b/tests/StaticCaching/FileCacherTest.php index ecaa66b9376..c25805544b1 100644 --- a/tests/StaticCaching/FileCacherTest.php +++ b/tests/StaticCaching/FileCacherTest.php @@ -14,6 +14,8 @@ use Statamic\StaticCaching\Cacher; use Statamic\StaticCaching\Cachers\FileCacher; use Statamic\StaticCaching\Cachers\Writer; +use Statamic\StaticCaching\NoCache\Region; +use Statamic\StaticCaching\NoCache\Session; use Tests\TestCase; class FileCacherTest extends TestCase @@ -182,27 +184,46 @@ public function flushing_wipes_the_dedicated_static_cache_store() } #[Test] - public function flushing_wipes_the_store_before_deleting_the_files() + public function a_page_cached_while_flushing_keeps_its_nocache_regions() { config(['cache.stores.static_cache' => ['driver' => 'array']]); $cache = Cache::store('static_cache'); - $cache->forever('nocache::session.abc', ['regions' => ['one']]); - - $writer = \Mockery::mock(Writer::class); - $cacher = $this->fileCacher(['path' => 'test/path', 'base_url' => 'http://example.com'], $writer, $cache); - $cacher->cacheUrl('one', '/one'); - - $stateWhenDeletingFiles = null; - $writer->shouldReceive('flush')->once()->andReturnUsing(function () use ($cache, $cacher, &$stateWhenDeletingFiles) { - $stateWhenDeletingFiles = [ - 'session' => $cache->get('nocache::session.abc'), - 'urls' => $cacher->getUrls()->all(), - ]; - }); - - $cacher->flush(); - - $this->assertSame(['session' => null, 'urls' => []], $stateWhenDeletingFiles); + $path = sys_get_temp_dir().'/statamic-file-cacher-'.uniqid(); + $url = 'http://example.com/one'; + $regionKey = null; + + // Stands in for a request that finishes rendering while the flush is deleting files. + $writer = new class extends Writer + { + public $concurrentRequest; + + public function flush($path) + { + parent::flush($path); + + ($this->concurrentRequest)(); + } + }; + + $cacher = $this->fileCacher(['path' => $path, 'base_url' => 'http://example.com', 'lock_hold_length' => 0], $writer, $cache); + + $writer->concurrentRequest = function () use ($cacher, $url, &$regionKey) { + $session = new Session($url); + $regionKey = $session->pushRegion('Log in', [], 'antlers.html')->key(); + + $cacher->cachePage(Request::create($url), 'html'); + $session->write(); + }; + + try { + $cacher->flush(); + + $this->assertFileExists($path.'/one_.html'); + $this->assertEquals(['/one'], $cacher->getUrls()->values()->all()); + $this->assertInstanceOf(Region::class, (new Session($url))->restore()->region($regionKey)); + } finally { + File::delete($path); + } } #[Test]