Skip to content

[12.x] Add CacheFlushed Event #55117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
8 changes: 8 additions & 0 deletions src/Illuminate/Cache/Events/CacheFlushed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Cache\Events;

class CacheFlushed extends CacheEvent
{
//
}
8 changes: 8 additions & 0 deletions src/Illuminate/Cache/Events/CacheFlushing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Cache\Events;

class CacheFlushing extends CacheEvent
{
//
}
12 changes: 11 additions & 1 deletion src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use BadMethodCallException;
use Closure;
use DateTimeInterface;
use Illuminate\Cache\Events\CacheFlushed;
use Illuminate\Cache\Events\CacheFlushing;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Cache\Events\ForgettingKey;
Expand Down Expand Up @@ -575,7 +577,15 @@ public function deleteMultiple($keys): bool
*/
public function clear(): bool
{
return $this->store->flush();
$this->event(new CacheFlushing($this->getName(), ''));

$result = $this->store->flush();

if ($result) {
$this->event(new CacheFlushed($this->getName(), ''));
}

return $result;
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/Cache/CacheEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Cache;

use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Events\CacheFlushed;
use Illuminate\Cache\Events\CacheFlushing;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Cache\Events\ForgettingKey;
Expand Down Expand Up @@ -221,6 +223,44 @@ public function testForgetDoesTriggerFailedEventOnFailure()
$this->assertFalse($repository->forget('baz'));
}

public function testFlushTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);

$dispatcher->shouldReceive('dispatch')->once()->with(
$this->assertEventMatches(CacheFlushing::class, [
'storeName' => 'array',
])
);

$dispatcher->shouldReceive('dispatch')->once()->with(
$this->assertEventMatches(CacheFlushed::class, [
'storeName' => 'array',
])
);
$this->assertTrue($repository->clear());
}

public function testFlushFailureDoesNotDispatchEvent()
{
$dispatcher = $this->getDispatcher();

// Create a store that fails to flush
$failingStore = m::mock(Store::class);
$failingStore->shouldReceive('flush')->andReturn(false);

$repository = new Repository($failingStore, ['store' => 'array']);
$repository->setEventDispatcher($dispatcher);

$dispatcher->shouldReceive('dispatch')->once()->with(
$this->assertEventMatches(CacheFlushing::class, [
'storeName' => 'array',
])
);
$this->assertFalse($repository->clear());
}

protected function assertEventMatches($eventClass, $properties = [])
{
return m::on(function ($event) use ($eventClass, $properties) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportFacadesEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Support;

use Illuminate\Cache\CacheManager;
use Illuminate\Cache\Events\CacheFlushed;
use Illuminate\Cache\Events\CacheFlushing;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Cache\Events\RetrievingKey;
use Illuminate\Config\Repository as ConfigRepository;
Expand Down Expand Up @@ -87,6 +89,17 @@ public function testFakeSwapsDispatchersInResolvedCacheRepositories()
Event::assertDispatched(CacheMissed::class);
}

public function testCacheFlushDispatchesEvent()
{
$arrayRepository = Cache::store('array');
Event::fake();

$arrayRepository->clear();

Event::assertDispatched(CacheFlushing::class);
Event::assertDispatched(CacheFlushed::class);
}

protected function getCacheConfig()
{
return [
Expand Down
Loading