Skip to content

Commit bcb3f58

Browse files
authored
Fix missing setTags method on new cache flush events (#55405)
1 parent 5548f95 commit bcb3f58

File tree

7 files changed

+114
-4
lines changed

7 files changed

+114
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class CacheFlushFailed
6+
{
7+
/**
8+
* The name of the cache store.
9+
*
10+
* @var string|null
11+
*/
12+
public $storeName;
13+
14+
/**
15+
* The tags that were assigned to the key.
16+
*
17+
* @var array
18+
*/
19+
public $tags;
20+
21+
/**
22+
* Create a new event instance.
23+
*
24+
* @param string|null $storeName
25+
* @return void
26+
*/
27+
public function __construct($storeName, array $tags = [])
28+
{
29+
$this->storeName = $storeName;
30+
$this->tags = $tags;
31+
}
32+
33+
/**
34+
* Set the tags for the cache event.
35+
*
36+
* @param array $tags
37+
* @return $this
38+
*/
39+
public function setTags($tags)
40+
{
41+
$this->tags = $tags;
42+
43+
return $this;
44+
}
45+
}

src/Illuminate/Cache/Events/CacheFlushed.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,35 @@ class CacheFlushed
1111
*/
1212
public $storeName;
1313

14+
/**
15+
* The tags that were assigned to the key.
16+
*
17+
* @var array
18+
*/
19+
public $tags;
20+
1421
/**
1522
* Create a new event instance.
1623
*
1724
* @param string|null $storeName
1825
* @return void
1926
*/
20-
public function __construct($storeName)
27+
public function __construct($storeName, array $tags = [])
2128
{
2229
$this->storeName = $storeName;
30+
$this->tags = $tags;
31+
}
32+
33+
/**
34+
* Set the tags for the cache event.
35+
*
36+
* @param array $tags
37+
* @return $this
38+
*/
39+
public function setTags($tags)
40+
{
41+
$this->tags = $tags;
42+
43+
return $this;
2344
}
2445
}

src/Illuminate/Cache/Events/CacheFlushing.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,35 @@ class CacheFlushing
1111
*/
1212
public $storeName;
1313

14+
/**
15+
* The tags that were assigned to the key.
16+
*
17+
* @var array
18+
*/
19+
public $tags;
20+
1421
/**
1522
* Create a new event instance.
1623
*
1724
* @param string|null $storeName
1825
* @return void
1926
*/
20-
public function __construct($storeName)
27+
public function __construct($storeName, array $tags = [])
2128
{
2229
$this->storeName = $storeName;
30+
$this->tags = $tags;
31+
}
32+
33+
/**
34+
* Set the tags for the cache event.
35+
*
36+
* @param array $tags
37+
* @return $this
38+
*/
39+
public function setTags($tags)
40+
{
41+
$this->tags = $tags;
42+
43+
return $this;
2344
}
2445
}

src/Illuminate/Cache/RedisTaggedCache.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Illuminate\Cache;
44

5+
use Illuminate\Cache\Events\CacheFlushed;
6+
use Illuminate\Cache\Events\CacheFlushing;
7+
58
class RedisTaggedCache extends TaggedCache
69
{
710
/**
@@ -105,9 +108,13 @@ public function forever($key, $value)
105108
*/
106109
public function flush()
107110
{
111+
$this->event(new CacheFlushing($this->getName()));
112+
108113
$this->flushValues();
109114
$this->tags->flush();
110115

116+
$this->event(new CacheFlushed($this->getName()));
117+
111118
return true;
112119
}
113120

src/Illuminate/Cache/Repository.php

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Closure;
88
use DateTimeInterface;
99
use Illuminate\Cache\Events\CacheFlushed;
10+
use Illuminate\Cache\Events\CacheFlushFailed;
1011
use Illuminate\Cache\Events\CacheFlushing;
1112
use Illuminate\Cache\Events\CacheHit;
1213
use Illuminate\Cache\Events\CacheMissed;
@@ -584,6 +585,8 @@ public function clear(): bool
584585

585586
if ($result) {
586587
$this->event(new CacheFlushed($this->getName()));
588+
} else {
589+
$this->event(new CacheFlushFailed($this->getName()));
587590
}
588591

589592
return $result;

src/Illuminate/Cache/TaggedCache.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\Cache;
44

5+
use Illuminate\Cache\Events\CacheFlushed;
6+
use Illuminate\Cache\Events\CacheFlushing;
57
use Illuminate\Contracts\Cache\Store;
68

79
class TaggedCache extends Repository
@@ -77,8 +79,12 @@ public function decrement($key, $value = 1)
7779
*/
7880
public function flush()
7981
{
82+
$this->event(new CacheFlushing($this->getName()));
83+
8084
$this->tags->reset();
8185

86+
$this->event(new CacheFlushed($this->getName()));
87+
8288
return true;
8389
}
8490

@@ -104,7 +110,7 @@ public function taggedItemKey($key)
104110
/**
105111
* Fire an event for this cache instance.
106112
*
107-
* @param \Illuminate\Cache\Events\CacheEvent $event
113+
* @param object $event
108114
* @return void
109115
*/
110116
protected function event($event)

tests/Cache/CacheEventsTest.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Cache\ArrayStore;
66
use Illuminate\Cache\Events\CacheFlushed;
7+
use Illuminate\Cache\Events\CacheFlushFailed;
78
use Illuminate\Cache\Events\CacheFlushing;
89
use Illuminate\Cache\Events\CacheHit;
910
use Illuminate\Cache\Events\CacheMissed;
@@ -242,7 +243,7 @@ public function testFlushTriggersEvents()
242243
$this->assertTrue($repository->clear());
243244
}
244245

245-
public function testFlushFailureDoesNotDispatchEvent()
246+
public function testFlushFailureDoesDispatchEvent()
246247
{
247248
$dispatcher = $this->getDispatcher();
248249

@@ -258,6 +259,12 @@ public function testFlushFailureDoesNotDispatchEvent()
258259
'storeName' => 'array',
259260
])
260261
);
262+
263+
$dispatcher->shouldReceive('dispatch')->once()->with(
264+
$this->assertEventMatches(CacheFlushFailed::class, [
265+
'storeName' => 'array',
266+
])
267+
);
261268
$this->assertFalse($repository->clear());
262269
}
263270

0 commit comments

Comments
 (0)