rob-lester-jr04 / call-forwarding
一种减少数据库写入并提高入站请求处理的方法
1.0.3
2024-04-19 13:58 UTC
Requires
- opis/closure: ^3.6
- predis/predis: ^2.2
Requires (Dev)
- laravel/pint: ^1.15
- orchestra/testbench: ^6.23|^7.6|^8.0|^9.0
- phpunit/phpunit: ^10.2
README
我们的创新性“CallForwarding”包通过在eloquent模型上智能排队数据库写入,优化了高流量场景下的数据库性能。通过将这写入操作合并为单一操作,它减轻了数据库资源的压力,即使在高峰时段也能确保平稳运行。使用ForwardSync,您的系统将获得效率和弹性,让您可以专注于提供卓越的用户体验,而无需担心数据库限制。
目录
安装
下载到您的项目目录,添加README.md
,并提交
composer require rob-lester-jr04/call-forwarding
用法
对于您想要实施调用转发的每个模型,包含特性和接口。
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Lester\Forwarding\ReceivesCalls; use Lester\Forwarding\ShouldForward; class PageVisit extends Model implements ShouldForward { use ReceivesCalls; }
然后,在调用持久化方法之前,调用callForwarding()
。示例
$visit = new PageVisit; $visit->hits = 15; $visit-> forwarded()->save(); // This will return false. That is expected.
现在默认情况下,每分钟,通过这种方式排队的所有内容都会被持久化。
高级配置
更改驱动程序
该包内置了3个用于队列的驱动程序。默认为file
,但还包括redis
和memcached
。
您可以选择使用File或Redis。要使用Redis选项,将其添加到您的.env
文件中
CF_DRIVER=redis
写入回调
如果您想在写入发生后执行代码,可以使用闭包作为参数与afterForward
方法链式调用。示例。
$model = new PageVisit; $visit->afterForward(function($attrs) { // Do something else with the attributes. })->forwarded()->save();
发布配置文件。
php artisan vendor:publish --provider="Lester\Forwarding\CallForwardingServiceProvider"
自定义驱动程序
要生成自己的驱动程序,创建一个实现了以下方法的类:putItem
、getAllItems
在配置文件中的handler数组下注册自定义类,并使用您定义的slug作为处理程序。
'handler' => 'my-custom-handler', 'handlers' => [ // ... 'my-custom-handler' => \App\My\Handler::class, ],
<?php namespace App\Handlers; // or whatever namespace you want to use use Lester\Forwarding\Contracts\CallForwardingDriver; use Illuminate\Support\Collection; class MyHandler implements CallForwardingDriver { // Worth noting, $key is only ever 'update' or 'insert' public function putItem($key, $data): void { // Store the db write. } public function getAllItems($key, $purge = false): Collection { // Retrieve the collection of stored db writes // Purge each item if the $purge flag is true. } }
支持
请提交问题以获得支持。
贡献
请使用Github Flow进行贡献。创建分支,添加提交,并提交拉取请求。