jalle19/laravel-deferred-event-dispatcher
Composer 安装命令:
composer require jalle19/laravel-deferred-event-dispatcher
包简介
A deferring event dispatcher for the Laravel and Lumen frameworks
README 文档
README
A deferring event dispatcher for the Laravel and Lumen frameworks.
Motivation
Events are a powerful feature of any framework. It allows you to decouple your code so that the logic that triggers an event doesn't have to know what should happen in reaction to that event.
While the default implementation in Laravel/Lumen is perfectly adequate in many cases, it is fairly naive, which can lead to subpar performance. Consider the following scenario:
- You have your own
EventServiceProviderwhere you have registered a bunch of event subscribers. - You register this service provider in your bootstrap file
Under the hood, whenever an EventServiceProvider is registered, the EventDispatcher will also be constructed. The
event dispatcher in turn will go through all the event handlers you've defined and construct each of them
so that it knows which listeners to trigger in the case of an event.
While this doesn't sound that bad, it means that even for the simplest of requests (e.g. a route that only prints "OK", e.g. a health check route) will cause all your event subscribers to be constructed, which can pull in a lot of code that you don't need to fulfill the request (e.g. Doctrine and other "heavy" services").
This wouldn't be so bad if it weren't for the fact that the majority of your requests probably won't be triggering any events, so constructing and registering all your event handlers is pointless. This event dispatcher implementation aims to solve that problem.
Deferring the resolving of listeners and subscribers
A simple yet fairly effective solution is to defer the resolving of event handlers until an event is actually dispatched. This assures we don't do any unnecessary work in requests that don't trigger any events. But there's a problem...
A lot of services use events under the hood:
- Some service providers will dispatch events before and after they've booted to allow other service providers to hook into these and perform optional bootstraping logic
- The cache repository will dispatch
Illuminate/Cache/Events/CacheHitandIlluminate/Cache/Events/CacheMissedevents every time you attempt to retrieve something from your cache. Depending on your application, this is something that could happen during practically every request.
Even if we deferr the resolving of event handlers until an event is dispatched, chances are there is at least one event dispatched on every request anyway, defeating the whole point.
Deferring specific events
Chances are you don't care about whether a cache operation resulted in a hit or a miss, or whether a service provider has been booted or not. Ignoring these events would defer the resolving of event handlers even further, possibly for the duration of the request, which finally means we've accomplished our goal of not constructing all event handlers needlessly.
Requirements
- Laravel/Lumen >= 5.4
- PHP >= 7.1
Usage
- Install the library using Composer:
composer require jalle19/laravel-deferred-event-dispatcher
- Swap the default event dispatcher for this one:
// The event dispatcher must be a singleton $app->singleton(\Jalle19\Laravel\Events\DeferredEventDispatcher::class, function () use ($app) { return new \Jalle19\Laravel\Events\DeferredEventDispatcher($app, [ // Cache events Illuminate\Cache\Events\CacheHit::class, Illuminate\Cache\Events\CacheMissed::class, ]); }); // Swap the default implementation for this one. Some classes type-hint the interface, others simply use "events" $app->alias(\Jalle19\Laravel\Events\DeferredEventDispatcher::class, 'events'); $app->alias(\Jalle19\Laravel\Events\DeferredEventDispatcher::class, Illuminate\Contracts\Events\Dispatcher::class);
In this example we've decided to defer resolving of event handlers whenever a cache event is dispatched.
- Make sure your event service provider is registered somewhere after these lines
That's it, your event handlers will now be resolved only when an event that you haven't explicitly deferred is dispatched!
License
MIT
jalle19/laravel-deferred-event-dispatcher 适用场景与选型建议
jalle19/laravel-deferred-event-dispatcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.53k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jalle19/laravel-deferred-event-dispatcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jalle19/laravel-deferred-event-dispatcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 40.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-03