fet / laminas-rate-limiting
Laminas MVC应用的速度限制模块。
0.3.0
2024-02-01 11:39 UTC
Requires
- php: ^8.1
- ext-redis: *
- laminas/laminas-eventmanager: ^3.2
- laminas/laminas-mvc: ^3.1
- laminas/laminas-servicemanager: ^3.4
Requires (Dev)
- mockery/mockery: ^1.6
- phpunit/phpunit: ^10.5
README
fet/laminas-rate-limiting
包为Laminas MVC应用提供了速度限制解决方案。它允许您在指定的时间窗口内设置某些路由的访问频率限制。
安装
要安装此包,请使用Composer
composer require fet/laminas-rate-limiting
在运行此命令之前,请确保您的系统已安装Composer。
在您的config/application.config.php
文件中启用Fet\LaminasRateLimiting
模块。
return [ // ... other modules ... 'Fet\\LaminasRateLimiting', ];
配置
在您的module.config.php
或全局配置文件中定义您的速度限制规则。
return [ 'rate_limiting' => [ 'enabled' => true, // indicates whether rate limiting is enabled 'max_requests' => 100, // max number of requests 'window' => 60, // time window in seconds 'routes' => [ // routes to apply rate limiting (wildcards allowed) 'home', 'api/*', 'admin/*', ], ], ];
如果您想限制所有路由,可以使用'*'作为路由名称。
存储
截至当前版本,此包仅支持Redis
作为后端存储。
return [ 'rate_limiting' => [ // ... other config options ... 'storage' => [ 'options' => [ 'host' => '127.0.0.1', 'port' => 6379 ], ], ], ];
自定义存储
如果您想提供自己的存储实现,请确保您的类符合Fet\LaminasRateLimiting\Storage\StorageInterface
契约。
use Acme\Storage\DatabaseStorage; return [ 'rate_limiting' => [ // ... other config options ... 'storage' => [ 'name' => DatabaseStorage::class, // this will be loaded via service manager 'options' => [], ], ], ];
测试
使用以下命令运行测试
composer test