Skip to content

Commit ef3e8c2

Browse files
authored
[12.x] Add CacheFlushed Event (#55142)
* [12.x] Fix: Added CacheFlushed Event * [12.x] StyleCLI Resolved * [12.x] StyleCLI Resolved * [12.x] CacheFlushing Event added * [12.x] Created New Class for Key free CacheEvent for Flush * [12.x] Deleted New CacheFlush Event * [12.x] PR comments resolved
1 parent 873cdba commit ef3e8c2

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class CacheFlushed
6+
{
7+
/**
8+
* The name of the cache store.
9+
*
10+
* @var string|null
11+
*/
12+
public $storeName;
13+
14+
/**
15+
* Create a new event instance.
16+
*
17+
* @param string|null $storeName
18+
* @return void
19+
*/
20+
public function __construct($storeName)
21+
{
22+
$this->storeName = $storeName;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class CacheFlushing
6+
{
7+
/**
8+
* The name of the cache store.
9+
*
10+
* @var string|null
11+
*/
12+
public $storeName;
13+
14+
/**
15+
* Create a new event instance.
16+
*
17+
* @param string|null $storeName
18+
* @return void
19+
*/
20+
public function __construct($storeName)
21+
{
22+
$this->storeName = $storeName;
23+
}
24+
}

src/Illuminate/Cache/Repository.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use BadMethodCallException;
77
use Closure;
88
use DateTimeInterface;
9+
use Illuminate\Cache\Events\CacheFlushed;
10+
use Illuminate\Cache\Events\CacheFlushing;
911
use Illuminate\Cache\Events\CacheHit;
1012
use Illuminate\Cache\Events\CacheMissed;
1113
use Illuminate\Cache\Events\ForgettingKey;
@@ -576,7 +578,15 @@ public function deleteMultiple($keys): bool
576578
*/
577579
public function clear(): bool
578580
{
579-
return $this->store->flush();
581+
$this->event(new CacheFlushing($this->getName()));
582+
583+
$result = $this->store->flush();
584+
585+
if ($result) {
586+
$this->event(new CacheFlushed($this->getName()));
587+
}
588+
589+
return $result;
580590
}
581591

582592
/**

tests/Cache/CacheEventsTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Illuminate\Tests\Cache;
44

55
use Illuminate\Cache\ArrayStore;
6+
use Illuminate\Cache\Events\CacheFlushed;
7+
use Illuminate\Cache\Events\CacheFlushing;
68
use Illuminate\Cache\Events\CacheHit;
79
use Illuminate\Cache\Events\CacheMissed;
810
use Illuminate\Cache\Events\ForgettingKey;
@@ -221,6 +223,44 @@ public function testForgetDoesTriggerFailedEventOnFailure()
221223
$this->assertFalse($repository->forget('baz'));
222224
}
223225

226+
public function testFlushTriggersEvents()
227+
{
228+
$dispatcher = $this->getDispatcher();
229+
$repository = $this->getRepository($dispatcher);
230+
231+
$dispatcher->shouldReceive('dispatch')->once()->with(
232+
$this->assertEventMatches(CacheFlushing::class, [
233+
'storeName' => 'array',
234+
])
235+
);
236+
237+
$dispatcher->shouldReceive('dispatch')->once()->with(
238+
$this->assertEventMatches(CacheFlushed::class, [
239+
'storeName' => 'array',
240+
])
241+
);
242+
$this->assertTrue($repository->clear());
243+
}
244+
245+
public function testFlushFailureDoesNotDispatchEvent()
246+
{
247+
$dispatcher = $this->getDispatcher();
248+
249+
// Create a store that fails to flush
250+
$failingStore = m::mock(Store::class);
251+
$failingStore->shouldReceive('flush')->andReturn(false);
252+
253+
$repository = new Repository($failingStore, ['store' => 'array']);
254+
$repository->setEventDispatcher($dispatcher);
255+
256+
$dispatcher->shouldReceive('dispatch')->once()->with(
257+
$this->assertEventMatches(CacheFlushing::class, [
258+
'storeName' => 'array',
259+
])
260+
);
261+
$this->assertFalse($repository->clear());
262+
}
263+
224264
protected function assertEventMatches($eventClass, $properties = [])
225265
{
226266
return m::on(function ($event) use ($eventClass, $properties) {

tests/Support/SupportFacadesEventTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Illuminate\Tests\Support;
44

55
use Illuminate\Cache\CacheManager;
6+
use Illuminate\Cache\Events\CacheFlushed;
7+
use Illuminate\Cache\Events\CacheFlushing;
68
use Illuminate\Cache\Events\CacheMissed;
79
use Illuminate\Cache\Events\RetrievingKey;
810
use Illuminate\Config\Repository as ConfigRepository;
@@ -87,6 +89,17 @@ public function testFakeSwapsDispatchersInResolvedCacheRepositories()
8789
Event::assertDispatched(CacheMissed::class);
8890
}
8991

92+
public function testCacheFlushDispatchesEvent()
93+
{
94+
$arrayRepository = Cache::store('array');
95+
Event::fake();
96+
97+
$arrayRepository->clear();
98+
99+
Event::assertDispatched(CacheFlushing::class);
100+
Event::assertDispatched(CacheFlushed::class);
101+
}
102+
90103
protected function getCacheConfig()
91104
{
92105
return [

0 commit comments

Comments
 (0)