Skip to content

[12.x] Fix adding setTags method on new cache flush events #55405

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

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

namespace Illuminate\Cache\Events;

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

/**
* The tags that were assigned to the key.
*
* @var array
*/
public $tags;

/**
* Create a new event instance.
*
* @param string|null $storeName
* @return void
*/
public function __construct($storeName, array $tags = [])
{
$this->storeName = $storeName;
$this->tags = $tags;
}

/**
* Set the tags for the cache event.
*
* @param array $tags
* @return $this
*/
public function setTags($tags)
{
$this->tags = $tags;

return $this;
}
}
23 changes: 22 additions & 1 deletion src/Illuminate/Cache/Events/CacheFlushed.php
Original file line number Diff line number Diff line change
@@ -11,14 +11,35 @@ class CacheFlushed
*/
public $storeName;

/**
* The tags that were assigned to the key.
*
* @var array
*/
public $tags;

/**
* Create a new event instance.
*
* @param string|null $storeName
* @return void
*/
public function __construct($storeName)
public function __construct($storeName, array $tags = [])
{
$this->storeName = $storeName;
$this->tags = $tags;
}

/**
* Set the tags for the cache event.
*
* @param array $tags
* @return $this
*/
public function setTags($tags)
{
$this->tags = $tags;

return $this;
}
}
23 changes: 22 additions & 1 deletion src/Illuminate/Cache/Events/CacheFlushing.php
Original file line number Diff line number Diff line change
@@ -11,14 +11,35 @@ class CacheFlushing
*/
public $storeName;

/**
* The tags that were assigned to the key.
*
* @var array
*/
public $tags;

/**
* Create a new event instance.
*
* @param string|null $storeName
* @return void
*/
public function __construct($storeName)
public function __construct($storeName, array $tags = [])
{
$this->storeName = $storeName;
$this->tags = $tags;
}

/**
* Set the tags for the cache event.
*
* @param array $tags
* @return $this
*/
public function setTags($tags)
{
$this->tags = $tags;

return $this;
}
}
7 changes: 7 additions & 0 deletions src/Illuminate/Cache/RedisTaggedCache.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@

namespace Illuminate\Cache;

use Illuminate\Cache\Events\CacheFlushed;
use Illuminate\Cache\Events\CacheFlushing;

class RedisTaggedCache extends TaggedCache
{
/**
@@ -105,9 +108,13 @@ public function forever($key, $value)
*/
public function flush()
{
$this->event(new CacheFlushing($this->getName()));

$this->flushValues();
$this->tags->flush();

$this->event(new CacheFlushed($this->getName()));

return true;
}

3 changes: 3 additions & 0 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
use Closure;
use DateTimeInterface;
use Illuminate\Cache\Events\CacheFlushed;
use Illuminate\Cache\Events\CacheFlushFailed;
use Illuminate\Cache\Events\CacheFlushing;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Cache\Events\CacheMissed;
@@ -584,6 +585,8 @@ public function clear(): bool

if ($result) {
$this->event(new CacheFlushed($this->getName()));
} else {
$this->event(new CacheFlushFailed($this->getName()));
}

return $result;
8 changes: 7 additions & 1 deletion src/Illuminate/Cache/TaggedCache.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

namespace Illuminate\Cache;

use Illuminate\Cache\Events\CacheFlushed;
use Illuminate\Cache\Events\CacheFlushing;
use Illuminate\Contracts\Cache\Store;

class TaggedCache extends Repository
@@ -77,8 +79,12 @@ public function decrement($key, $value = 1)
*/
public function flush()
{
$this->event(new CacheFlushing($this->getName()));

$this->tags->reset();

$this->event(new CacheFlushed($this->getName()));

return true;
}

@@ -104,7 +110,7 @@ public function taggedItemKey($key)
/**
* Fire an event for this cache instance.
*
* @param \Illuminate\Cache\Events\CacheEvent $event
* @param object $event
* @return void
*/
protected function event($event)
9 changes: 8 additions & 1 deletion tests/Cache/CacheEventsTest.php
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Events\CacheFlushed;
use Illuminate\Cache\Events\CacheFlushFailed;
use Illuminate\Cache\Events\CacheFlushing;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Cache\Events\CacheMissed;
@@ -242,7 +243,7 @@ public function testFlushTriggersEvents()
$this->assertTrue($repository->clear());
}

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

@@ -258,6 +259,12 @@ public function testFlushFailureDoesNotDispatchEvent()
'storeName' => 'array',
])
);

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