Skip to content

Commit 96eeea7

Browse files
[11.x] Add get, write and forget cache events (#51560)
* Add `get`, `write` and `forget` cache events * formatting * formatting * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent af3648f commit 96eeea7

File tree

10 files changed

+256
-5
lines changed

10 files changed

+256
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class ForgettingKey extends CacheEvent
6+
{
7+
//
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class KeyForgetFailed extends CacheEvent
6+
{
7+
//
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class KeyWriteFailed extends CacheEvent
6+
{
7+
/**
8+
* The value that would have been written.
9+
*
10+
* @var mixed
11+
*/
12+
public $value;
13+
14+
/**
15+
* The number of seconds the key should have been valid.
16+
*
17+
* @var int|null
18+
*/
19+
public $seconds;
20+
21+
/**
22+
* Create a new event instance.
23+
*
24+
* @param string|null $storeName
25+
* @param string $key
26+
* @param mixed $value
27+
* @param int|null $seconds
28+
* @param array $tags
29+
* @return void
30+
*/
31+
public function __construct($storeName, $key, $value, $seconds = null, $tags = [])
32+
{
33+
parent::__construct($storeName, $key, $tags);
34+
35+
$this->value = $value;
36+
$this->seconds = $seconds;
37+
}
38+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class RetrievingKey extends CacheEvent
6+
{
7+
//
8+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class RetrievingManyKeys extends CacheEvent
6+
{
7+
/**
8+
* The keys that are being retrieved.
9+
*
10+
* @var array
11+
*/
12+
public $keys;
13+
14+
/**
15+
* Create a new event instance.
16+
*
17+
* @param string|null $storeName
18+
* @param array $keys
19+
* @param array $tags
20+
* @return void
21+
*/
22+
public function __construct($storeName, $keys, array $tags = [])
23+
{
24+
parent::__construct($storeName, $keys[0] ?? '', $tags);
25+
26+
$this->keys = $keys;
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class WritingKey extends CacheEvent
6+
{
7+
/**
8+
* The value that will be written.
9+
*
10+
* @var mixed
11+
*/
12+
public $value;
13+
14+
/**
15+
* The number of seconds the key should be valid.
16+
*
17+
* @var int|null
18+
*/
19+
public $seconds;
20+
21+
/**
22+
* Create a new event instance.
23+
*
24+
* @param string|null $storeName
25+
* @param string $key
26+
* @param mixed $value
27+
* @param int|null $seconds
28+
* @param array $tags
29+
* @return void
30+
*/
31+
public function __construct($storeName, $key, $value, $seconds = null, $tags = [])
32+
{
33+
parent::__construct($storeName, $key, $tags);
34+
35+
$this->value = $value;
36+
$this->seconds = $seconds;
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Illuminate\Cache\Events;
4+
5+
class WritingManyKeys extends CacheEvent
6+
{
7+
/**
8+
* The keys that are being written.
9+
*
10+
* @var mixed
11+
*/
12+
public $keys;
13+
14+
/**
15+
* The value that is being written.
16+
*
17+
* @var mixed
18+
*/
19+
public $values;
20+
21+
/**
22+
* The number of seconds the keys should be valid.
23+
*
24+
* @var int|null
25+
*/
26+
public $seconds;
27+
28+
/**
29+
* Create a new event instance.
30+
*
31+
* @param string|null $storeName
32+
* @param array $keys
33+
* @param array $values
34+
* @param int|null $seconds
35+
* @param array $tags
36+
* @return void
37+
*/
38+
public function __construct($storeName, $keys, $values, $seconds = null, $tags = [])
39+
{
40+
parent::__construct($storeName, $keys[0], $tags);
41+
42+
$this->keys = $keys;
43+
$this->values = $values;
44+
$this->seconds = $seconds;
45+
}
46+
}

src/Illuminate/Cache/Repository.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88
use DateTimeInterface;
99
use Illuminate\Cache\Events\CacheHit;
1010
use Illuminate\Cache\Events\CacheMissed;
11+
use Illuminate\Cache\Events\ForgettingKey;
12+
use Illuminate\Cache\Events\KeyForgetFailed;
1113
use Illuminate\Cache\Events\KeyForgotten;
14+
use Illuminate\Cache\Events\KeyWriteFailed;
1215
use Illuminate\Cache\Events\KeyWritten;
16+
use Illuminate\Cache\Events\RetrievingKey;
17+
use Illuminate\Cache\Events\RetrievingManyKeys;
18+
use Illuminate\Cache\Events\WritingKey;
19+
use Illuminate\Cache\Events\WritingManyKeys;
1320
use Illuminate\Contracts\Cache\Repository as CacheContract;
1421
use Illuminate\Contracts\Cache\Store;
1522
use Illuminate\Contracts\Events\Dispatcher;
@@ -104,6 +111,8 @@ public function get($key, $default = null): mixed
104111
return $this->many($key);
105112
}
106113

114+
$this->event(new RetrievingKey($this->getName(), $key));
115+
107116
$value = $this->store->get($this->itemKey($key));
108117

109118
// If we could not find the cache value, we will fire the missed event and get
@@ -130,6 +139,8 @@ public function get($key, $default = null): mixed
130139
*/
131140
public function many(array $keys)
132141
{
142+
$this->event(new RetrievingManyKeys($this->getName(), $keys));
143+
133144
$values = $this->store->many(collect($keys)->map(function ($value, $key) {
134145
return is_string($key) ? $key : $value;
135146
})->values()->all());
@@ -222,10 +233,14 @@ public function put($key, $value, $ttl = null)
222233
return $this->forget($key);
223234
}
224235

236+
$this->event(new WritingKey($this->getName(), $key, $value, $seconds));
237+
225238
$result = $this->store->put($this->itemKey($key), $value, $seconds);
226239

227240
if ($result) {
228241
$this->event(new KeyWritten($this->getName(), $key, $value, $seconds));
242+
} else {
243+
$this->event(new KeyWriteFailed($this->getName(), $key, $value, $seconds));
229244
}
230245

231246
return $result;
@@ -260,11 +275,15 @@ public function putMany(array $values, $ttl = null)
260275
return $this->deleteMultiple(array_keys($values));
261276
}
262277

278+
$this->event(new WritingManyKeys($this->getName(), array_keys($values), array_values($values), $seconds));
279+
263280
$result = $this->store->putMany($values, $seconds);
264281

265-
if ($result) {
266-
foreach ($values as $key => $value) {
282+
foreach ($values as $key => $value) {
283+
if ($result) {
267284
$this->event(new KeyWritten($this->getName(), $key, $value, $seconds));
285+
} else {
286+
$this->event(new KeyWriteFailed($this->getName(), $key, $value, $seconds));
268287
}
269288
}
270289

@@ -372,10 +391,14 @@ public function decrement($key, $value = 1)
372391
*/
373392
public function forever($key, $value)
374393
{
394+
$this->event(new WritingKey($this->getName(), $key, $value));
395+
375396
$result = $this->store->forever($this->itemKey($key), $value);
376397

377398
if ($result) {
378399
$this->event(new KeyWritten($this->getName(), $key, $value));
400+
} else {
401+
$this->event(new KeyWriteFailed($this->getName(), $key, $value));
379402
}
380403

381404
return $result;
@@ -456,9 +479,13 @@ public function rememberForever($key, Closure $callback)
456479
*/
457480
public function forget($key)
458481
{
482+
$this->event(new ForgettingKey($this->getName(), $key));
483+
459484
return tap($this->store->forget($this->itemKey($key)), function ($result) use ($key) {
460485
if ($result) {
461486
$this->event(new KeyForgotten($this->getName(), $key));
487+
} else {
488+
$this->event(new KeyForgetFailed($this->getName(), $key));
462489
}
463490
});
464491
}

0 commit comments

Comments
 (0)