Skip to content

Commit 6217fee

Browse files
authored
add cache events (#8117)
* add cache events * fix build
1 parent 310a89f commit 6217fee

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

en/appendices/5-3-migration-guide.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Cache
7777

7878
- Added Redis Cluster support to ``RedisEngine``. Configure the ``cluster`` option
7979
with an array of server addresses to enable cluster mode.
80+
- Several :ref:`cache-events` were added to allow monitoring the caching behavior.
8081

8182
Command
8283
-------

en/console-commands/commands.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ Grouping Commands
294294

295295
By default in the help output CakePHP will group commands into core, app, and
296296
plugin groups. You can customize the grouping of commands by implementing
297-
``getGroup()``:
297+
``getGroup()``::
298298

299299
class CleanupCommand extends Command
300300
{

en/core-libraries/caching.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,46 @@ The required API for a CacheEngine is
673673

674674
Increment a number under the key and return incremented value
675675

676+
.. _cache-events:
677+
678+
Cache Events
679+
============
680+
681+
.. versionadded:: 5.3.0
682+
683+
You can add event listeners to the following events:
684+
685+
* ``\Cake\Cache\Event\CacheBeforeGetEvent``
686+
* ``\Cake\Cache\Event\CacheAfterGetEvent``
687+
* ``\Cake\Cache\Event\CacheBeforeSetEvent``
688+
* ``\Cake\Cache\Event\CacheAfterSetEvent``
689+
* ``\Cake\Cache\Event\CacheBeforeAddEvent``
690+
* ``\Cake\Cache\Event\CacheAfterAddEvent``
691+
* ``\Cake\Cache\Event\CacheBeforeDecrementEvent``
692+
* ``\Cake\Cache\Event\CacheAfterDecrementEvent``
693+
* ``\Cake\Cache\Event\CacheBeforeDeleteEvent``
694+
* ``\Cake\Cache\Event\CacheAfterDeleteEvent``
695+
* ``\Cake\Cache\Event\CacheBeforeIncrementEvent``
696+
* ``\Cake\Cache\Event\CacheAfterIncrementEvent``
697+
* ``\Cake\Cache\Event\CacheClearedEvent``
698+
* ``\Cake\Cache\Event\CacheGroupClearEvent``
699+
700+
an example listener in your ``src/Application.php`` or plugin class would be::
701+
702+
public function events(EventManagerInterface $eventManager): EventManagerInterface
703+
{
704+
$eventManager->on(CacheAfterGetEvent::NAME, function (CacheAfterGetEvent $event): void {
705+
$key = $event->getKey();
706+
$value = $event->getValue();
707+
$success = $event->getResult();
708+
});
709+
710+
return $eventManager;
711+
}
712+
713+
Different events have different context, so please check the methods inside the custom event class
714+
if you are looking for certain data.
715+
676716
.. meta::
677717
:title lang=en: Caching
678718
:keywords lang=en: uniform api,cache engine,cache system,atomic operations,php class,disk storage,static methods,php extension,consistent manner,similar features,apcu,apc,memcache,queries,cakephp,elements,servers,memory

0 commit comments

Comments
 (0)