Skip to content
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

[12.x] Add CacheFlushed Event #55142

Merged
merged 7 commits into from
Mar 24, 2025
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
24 changes: 24 additions & 0 deletions src/Illuminate/Cache/Events/CacheFlushed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Cache\Events;

class CacheFlushed
{
/**
* The name of the cache store.
*
* @var string|null
*/
public $storeName;

/**
* Create a new event instance.
*
* @param string|null $storeName
* @return void
*/
public function __construct($storeName)
{
$this->storeName = $storeName;
}
}
24 changes: 24 additions & 0 deletions src/Illuminate/Cache/Events/CacheFlushing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Cache\Events;

class CacheFlushing
{
/**
* The name of the cache store.
*
* @var string|null
*/
public $storeName;

/**
* Create a new event instance.
*
* @param string|null $storeName
* @return void
*/
public function __construct($storeName)
{
$this->storeName = $storeName;
}
}
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()));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CacheFlushFailed event missing, look at

if ($result) {
$this->event(new KeyForgotten($this->getName(), $key));
} else {
$this->event(new KeyForgetFailed($this->getName(), $key));
}

if ($result) {
$this->event(new KeyWritten($this->getName(), $key, $value, $seconds));
} else {
$this->event(new KeyWriteFailed($this->getName(), $key, $value, $seconds));
}

if (is_null($value)) {
$this->event(new CacheMissed($this->getName(), $key));
$value = value($default);
} else {
$this->event(new CacheHit($this->getName(), $key, $value));
}

if ($result) {
    $this->event(new CacheFlushed($this->getName()));
} else {
    $this->event(new CacheFlushFailed($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