简体   繁体   English

如何在larvel 8中动态设置速率限制器

[英]How to set the rate limiter dynamically in larvel 8

I need to set the rate limiter dynamically in laravel 8 based on the combination of parameters stored in database table我需要根据存储在数据库表中的参数组合在 laravel 8 中动态设置速率限制器

S.No编号 Key钥匙 Param 1参数 1 Param 2参数 2 Param 3参数 3 Rate limit速率限制
1 1 adx广告 param_1_a param_1_a param_2_d param_2_d param_3_x param_3_x 20 20
2 2 buz嗡嗡声 param_1_b param_1_b param_2_u param_2_u param_3_z param_3_z 30 30
3 3 cfy cfy param_1_c param_1_c param_2_f param_2_f param_3_y param_3_y 40 40

and Rate limiter code will be as follows和速率限制器代码如下

protected function configureRateLimiting()
{
   RateLimiter::for('api', function (Request $request) {
      return [
        Limit::perMinute(rateLimit)->by(RateLimitKey)->response(function () {
        ...
        }),
        Limit::perMinute(rateLimit1)->by(RateLimitKey1)->response(function () {
        ...
        }),
      ];
   });
}

I need to add the rate limiter to the array returned in the above code我需要将速率限制器添加到上面代码中返回的数组中

RateLimit value will be the value of the column "Rate Limit" from database table RateLimit 值将是数据库表中“Rate Limit”列的值

RateLimitKey will the value with combination of the columns Key, Param 1, Param 2, Param 3 (eg, key_param1_param2_param_3) RateLimitKey 将值与列 Key、Param 1、Param 2、Param 3 的组合(例如,key_param1_param2_param_3)

Need to add the rate limiter dynamically using the data retrieved from the database table需要使用从数据库表中检索到的数据动态添加速率限制器

I am not sure about how to proceed on adding the rate limiter in laravel 8我不确定如何继续在 laravel 8 中添加速率限制器

You have the request in the context, so from here it is just about getting out the data from the DB .您在上下文中有请求,因此从这里开始只是从DB中取出数据。 I'm assuming they are query parameters.我假设它们是查询参数。 Remember that this can be null, and needs to have a fallback.请记住,这可以是 null,并且需要有一个后备。

DB::table('rate_limiting_table')
    ->where('key', $request->query('key'))
    ->where('param_1', $request->query('param_1'))
    ->where('param_2', $request->query('param_2'))
    ->where('param_3', $request->query('param_3'))
    ->first();

Since this would be executed on each HTTP request, i would suggest caching it.由于这将在每个 HTTP 请求上执行,我建议缓存它。

$cacheKey = $request->query('key') . $request->query('param_1') . $request->query('param_2') . $request->query('param_3');
cache()->remember($cacheKey, 14400, function () { // cached for 4 hours
    return ... // your query
});

Putting all this logic together, it could look like this.将所有这些逻辑放在一起,它可能看起来像这样。 With some minor clean code improvements.有一些小的干净代码改进。

const DEFAULT_RATE_LIMITING = 60;

protected function configureRateLimiting()
{
    RateLimiter::for('global', function (Request $request) {
        $rateLimiting = $this->getCacheRateLimiting();

        return $rateLimiting ? $rateLimiting->rate_limit : static::DEFAULT_RATE_LIMITING;
    });
}

private function getRateLimitingDatabase(): ?object 
{
    DB::table('rate_limiting_table')
        ->where('key', $request->query('key'))
        ->where('param_1', $request->query('param_1'))
        ->where('param_2', $request->query('param_2'))
        ->where('param_3', $request->query('param_3'))
        ->first();
}

private function getCacheRateLimiting(): ?object 
{
    // cached for 4 hours
    return cache()->remember($this->getRateLimitingCacheKey(), 14400, function () {
        return  $this->getRateLimitingDatabase();
    });
}

private function getRateLimitingCacheKey(): string
{
    return $request->query('key')
        . $request->query('param_1')
        . $request->query('param_2')
        . $request->query('param_3');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM