fet/laminas-rate-limiting

Laminas MVC应用的速度限制模块。

0.3.0 2024-02-01 11:39 UTC

This package is auto-updated.

Last update: 2024-09-11 14:12:59 UTC


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