diff --git a/src/Stache/Stache.php b/src/Stache/Stache.php index 1eb76aec62d..e130ae8d7a6 100644 --- a/src/Stache/Stache.php +++ b/src/Stache/Stache.php @@ -22,6 +22,7 @@ class Stache protected $lockFactory; protected $locks = []; protected $duplicates; + protected $exclude = []; public function __construct() { @@ -60,6 +61,13 @@ public function registerStores($stores) return $this; } + public function exclude(string $store) + { + $this->exclude[] = $store; + + return $this; + } + public function stores() { return $this->stores; @@ -88,7 +96,7 @@ public function generateId() public function clear() { - $this->stores()->reverse()->each->clear(); + $this->stores()->except($this->exclude)->reverse()->each->clear(); $this->duplicates()->clear(); @@ -110,7 +118,7 @@ public function warm() $this->startTimer(); - $this->stores()->each->warm(); + $this->stores()->except($this->exclude)->each->warm(); $this->stopTimer(); diff --git a/tests/Stache/StacheTest.php b/tests/Stache/StacheTest.php index f8be258d5e9..721b51208be 100644 --- a/tests/Stache/StacheTest.php +++ b/tests/Stache/StacheTest.php @@ -6,10 +6,12 @@ use Illuminate\Support\Collection; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use Statamic\Stache\NullLockStore; use Statamic\Stache\Stache; use Statamic\Stache\Stores\ChildStore; use Statamic\Stache\Stores\CollectionsStore; use Statamic\Stache\Stores\EntriesStore; +use Symfony\Component\Lock\LockFactory; use Tests\TestCase; class StacheTest extends TestCase @@ -61,6 +63,29 @@ public function stores_can_be_registered() }); } + #[Test] + public function stores_can_be_excluded_from_warming_and_clearing() + { + $this->stache->sites(['en']); // store expects the stache to have site(s) + $this->assertTrue($this->stache->stores()->isEmpty()); + + $mockStore = $this->mock(CollectionsStore::class, function ($mock) { + $mock->shouldReceive('warm')->never(); + $mock->shouldReceive('clear')->never(); + $mock->shouldReceive('key')->andReturn('collections'); + }); + + $this->stache->registerStore($mockStore); + + $return = $this->stache->exclude('collections'); + + $this->assertEquals($this->stache, $return); + + $this->stache->setLockFactory(new LockFactory(new NullLockStore())); + $this->stache->warm(); + $this->stache->clear(); + } + #[Test] public function multiple_stores_can_be_registered_at_once() {