nhphero/throttle

一个简单的PHP包,用于节流和速率限制

1.0.4 2020-11-18 10:09 UTC

This package is auto-updated.

Last update: 2024-09-18 17:58:13 UTC


README

这是另一个PHP应用的节流库,提供了节流接口和灵活的API,用于实现自定义的节流算法(也称为提供者)和存储策略。

安装

$ composer require nhphero/throttle

基本用法

<?php

$storage = new NhpHero\Throttle\Store\RedisStore( 'throttle_storage_prefix::',[
            'scheme' => config('redis.scheme'),
            'host' => config('redis.host'),
            'port' => config('redis.port'),
            'password' => config('redis.password'),
            'timeout' => config('redis.timeout'),
        ]);
$throttle = new NhpHero\Throttle\Throttle($storage);
//Limit 10 request per 60 seconds
$limit = 10;
$time= 60;
$throttleKey = $_SERVER['REMOTE_ADDR'];
if ($throttle->attempt($throttleKey, $limit, $time)) {
    // allow
} else {
    // deny
}

存储策略

目前,仅支持Redis存储。

开发和测试

欢迎您通过修复bug、添加新提供者和存储策略等方式进行贡献。

要开始贡献,只需创建此存储库的分支,分支名应说明您所做的工作,编写您的解决方案,然后向我们发送拉取请求。

开发安装

$ composer install --dev

运行测试

$ ./vendor/bin/phpunit

文档

即将推出。

许可

此库使用MIT许可证