在 Laravel 6 中,基于事件订阅者的重构
1、现阶段实现如下,在 EventServiceProvider 的 listen 属性中注册事件和监听器。/app/Providers/EventServiceProvider.php
use Illuminate\Cache\Events\CacheHit; use Illuminate\Cache\Events\CacheMissed; use Illuminate\Cache\Events\KeyForgotten; use Illuminate\Cache\Events\KeyWritten; use App\Listeners\Cache\LogCacheHitListener; use App\Listeners\Cache\LogCacheMissedListener; use App\Listeners\Cache\LogKeyForgottenListener; use App\Listeners\Cache\LogKeyWrittenListener; class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ CacheHit::class => [ LogCacheHitListener::class ], CacheMissed::class => [ LogCacheMissedListener::class ], KeyForgotten::class => [ LogKeyForgottenListener::class, ], KeyWritten::class => [ LogKeyWrittenListener::class, ], ];
2、其中一个监听器:LogKeyWrittenListener 实现如下,/app/Listeners/Cache/LogKeyWrittenListener.php
<?php namespace App\Listeners\Cache; use Illuminate\Cache\Events\KeyWritten; use Illuminate\Support\Facades\Log; use Modules\OnlineStoreThemeGraphQL\Preview\ThemePreviewInterface; class LogKeyWrittenListener { /** * 处理事件 * * @param KeyWritten $event * @return void */ public function handle(KeyWritten $event) { if (config('app.debug') === true && strpos($event->key, ThemePreviewInterface::THEME_EDITOR_SESSION_PREFIX) !== false) { Log::debug('theme_editor_session_key_written', [ 'key' => $event->key, 'tags' => $event->tags, 'value' => $event->value, 'seconds' => $event->seconds ]); } } }
3、其中一个监听器:LogCacheHitListener 实现如下,/app/Listeners/Cache/LogCacheHitListener.php
<?php namespace App\Listeners\Cache; use Illuminate\Cache\Events\CacheHit; use Illuminate\Support\Facades\Log; use Modules\OnlineStoreThemeGraphQL\Preview\ThemePreviewInterface; class LogCacheHitListener { /** * 处理事件 * * @param CacheHit $event * @return void */ public function handle(CacheHit $event) { if (config('app.debug') === true && strpos($event->key, ThemePreviewInterface::THEME_EDITOR_SESSION_PREFIX) !== false) { Log::debug('theme_editor_session_cache_hit', [ 'key' => $event->key, 'tags' => $event->tags, 'value' => $event->value ]); } } }
4、决定基于事件订阅者重构。编写事件订阅者 CacheEventSubscriber,将 4 个监听器文件合并为 1 个。
<?php namespace App\Listeners; use Illuminate\Events\Dispatcher; use Illuminate\Cache\Events\CacheHit; use Illuminate\Cache\Events\CacheMissed; use Illuminate\Cache\Events\KeyForgotten; use Illuminate\Cache\Events\KeyWritten; use Illuminate\Support\Facades\Log; use Modules\OnlineStoreThemeGraphQL\Preview\ThemePreviewInterface; class CacheEventSubscriber { /** * 处理缓存命中事件 * @param $event */ public function handleLogCacheHit($event) { if ($this->isThemeEditorSessionLogDebug($event)) { Log::debug('theme_editor_session_cache_hit', [ 'key' => $event->key, 'tags' => $event->tags, 'value' => $event->value ]); } } /** * 处理缓存未命中事件 */ public function handleLogCacheMissed($event) { if ($this->isThemeEditorSessionLogDebug($event)) { Log::debug('theme_editor_session_cache_missed', [ 'key' => $event->key, 'tags' => $event->tags ]); } } /** * 处理缓存删除事件 */ public function handleLogKeyForgotten($event) { if ($this->isThemeEditorSessionLogDebug($event)) { Log::debug('theme_editor_session_key_forgotten', [ 'key' => $event->key, 'tags' => $event->tags ]); } } /** * 处理缓存写入事件 */ public function handleLogKeyWritten($event) { if ($this->isThemeEditorSessionLogDebug($event)) { Log::debug('theme_editor_session_key_written', [ 'key' => $event->key, 'tags' => $event->tags, 'value' => $event->value, 'seconds' => $event->seconds ]); } } /** * 是否记录主题编辑器 Session 日志 * @return bool */ private function isThemeEditorSessionLogDebug($event) { return config('app.debug') === true && strpos($event->key, ThemePreviewInterface::THEME_EDITOR_SESSION_PREFIX) !== false; } /** * 为订阅者注册监听器 * * @param Dispatcher $events */ public function subscribe($events) { $events->listen( CacheHit::class, 'App\Listeners\CacheEventSubscriber@handleLogCacheHit' ); $events->listen( CacheMissed::class, 'App\Listeners\CacheEventSubscriber@handleLogCacheMissed' ); $events->listen( KeyForgotten::class, 'App\Listeners\CacheEventSubscriber@handleLogKeyForgotten' ); $events->listen( KeyWritten::class, 'App\Listeners\CacheEventSubscriber@handleLogKeyWritten' ); } }
5、在 EventServiceProvider 的 listen 属性中删除事件和监听器,然后在 subscribe 属性中添加订阅者
class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ ]; protected $subscribe = [ \App\Listeners\CacheEventSubscriber::class ];
6、经过测试,4个事件,其中使用 flush 方法删除缓存数据时,KeyForgotten 未被触发。使用 forget 方法从缓存中删除缓存数据时,KeyForgotten 会被触发。
Cache::put(ThemePreviewInterface::THEME_EDITOR_SESSION_PREFIX . '20220921', '20220921.value', ThemePreviewInterface::TTL); Cache::forget(ThemePreviewInterface::THEME_EDITOR_SESSION_PREFIX . '20220921');
[2022-09-21 10:11:12] local.DEBUG: theme_editor_session_key_written { "key": "theme_editor_session:20220921", "tags": [], "value": "20220921.value", "seconds": 86400 } [2022-09-21 10:14:19] local.DEBUG: theme_editor_session_key_forgotten { "key": "theme_editor_session:20220921", "tags": [] }
近期评论