Skip to content

Commit 260dfff

Browse files
authored
[5.x] Allow stache stores to be excluded from warming and clearing (#11830)
1 parent ab793dd commit 260dfff

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/Stache/Stache.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Stache
2222
protected $lockFactory;
2323
protected $locks = [];
2424
protected $duplicates;
25+
protected $exclude = [];
2526

2627
public function __construct()
2728
{
@@ -60,6 +61,13 @@ public function registerStores($stores)
6061
return $this;
6162
}
6263

64+
public function exclude(string $store)
65+
{
66+
$this->exclude[] = $store;
67+
68+
return $this;
69+
}
70+
6371
public function stores()
6472
{
6573
return $this->stores;
@@ -88,7 +96,7 @@ public function generateId()
8896

8997
public function clear()
9098
{
91-
$this->stores()->reverse()->each->clear();
99+
$this->stores()->except($this->exclude)->reverse()->each->clear();
92100

93101
$this->duplicates()->clear();
94102

@@ -110,7 +118,7 @@ public function warm()
110118

111119
$this->startTimer();
112120

113-
$this->stores()->each->warm();
121+
$this->stores()->except($this->exclude)->each->warm();
114122

115123
$this->stopTimer();
116124

tests/Stache/StacheTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use Illuminate\Support\Collection;
77
use PHPUnit\Framework\Attributes\DataProvider;
88
use PHPUnit\Framework\Attributes\Test;
9+
use Statamic\Stache\NullLockStore;
910
use Statamic\Stache\Stache;
1011
use Statamic\Stache\Stores\ChildStore;
1112
use Statamic\Stache\Stores\CollectionsStore;
1213
use Statamic\Stache\Stores\EntriesStore;
14+
use Symfony\Component\Lock\LockFactory;
1315
use Tests\TestCase;
1416

1517
class StacheTest extends TestCase
@@ -61,6 +63,29 @@ public function stores_can_be_registered()
6163
});
6264
}
6365

66+
#[Test]
67+
public function stores_can_be_excluded_from_warming_and_clearing()
68+
{
69+
$this->stache->sites(['en']); // store expects the stache to have site(s)
70+
$this->assertTrue($this->stache->stores()->isEmpty());
71+
72+
$mockStore = $this->mock(CollectionsStore::class, function ($mock) {
73+
$mock->shouldReceive('warm')->never();
74+
$mock->shouldReceive('clear')->never();
75+
$mock->shouldReceive('key')->andReturn('collections');
76+
});
77+
78+
$this->stache->registerStore($mockStore);
79+
80+
$return = $this->stache->exclude('collections');
81+
82+
$this->assertEquals($this->stache, $return);
83+
84+
$this->stache->setLockFactory(new LockFactory(new NullLockStore()));
85+
$this->stache->warm();
86+
$this->stache->clear();
87+
}
88+
6489
#[Test]
6590
public function multiple_stores_can_be_registered_at_once()
6691
{

0 commit comments

Comments
 (0)