Skip to content
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
10 changes: 6 additions & 4 deletions src/StaticCaching/Cachers/FileCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
45 changes: 45 additions & 0 deletions tests/StaticCaching/FileCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -181,6 +183,49 @@ public function flushing_wipes_the_dedicated_static_cache_store()
$this->assertNull($cache->get('untracked'));
}

#[Test]
public function a_page_cached_while_flushing_keeps_its_nocache_regions()
{
config(['cache.stores.static_cache' => ['driver' => 'array']]);
$cache = Cache::store('static_cache');
$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]
public function invalidating_a_url_thats_not_cached_will_do_nothing()
{
Expand Down
Loading