Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
refactor(RateLimit): Change last param of isRateLimitHit and rate lim…
Browse files Browse the repository at this point in the history
…it store Namespace
  • Loading branch information
Rhilip committed Aug 10, 2019
1 parent 7b20b2c commit 4dd571d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Refactor
- **Config:** Remove params `$throw` in Config()->get() (706cc9a)
- **View:** Make View extends BaseObject (0865cf9)
- **view:** Fix helper/username params (720f37e)


Expand Down
4 changes: 2 additions & 2 deletions apps/libraries/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public static function trackerTorrentContentByInfoHash($bin2hex_hash)
return 'Tracker:torrent_infohash_' . $bin2hex_hash . '_content:hash';
}

public static function rateLimitPool($uid,$action)
public static function rateLimitPool($pool, $action)
{
return 'Rate:user_' . $uid . '_action_' . $action . '_limit:zset';
return 'Rate:pool_' . $pool . '_action_' . $action . '_limit:zset';
}
}
14 changes: 5 additions & 9 deletions apps/models/form/Traits/actionRateLimitCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@


use apps\libraries\Constant;
use Redis;

/**
* Trait actionRateLimitCheckTrait
* @package apps\models\form\Traits
* @method buildCallbackFailMsg <-- Need this to prevent phpStorm's "member has protect access" inspection errors.
*/
trait actionRateLimitCheckTrait
{

Expand All @@ -26,12 +22,12 @@ protected function getRateLimitRules(): array
];
}

private function isRateLimitHit($action_key, $period, $max_count,$uid = null): bool
private function isRateLimitHit($action_key, $period, $max_count, $pool = null): bool
{
$uid = $uid ?? app()->site->getCurUser()->getId();
$key = Constant::rateLimitPool($uid, $action_key);
$pool = $pool ?? 'user_' . app()->site->getCurUser()->getId();
$key = Constant::rateLimitPool($pool, $action_key);
$now_ts = time() * 1000;
$pipe = app()->redis->multi(\Redis::PIPELINE);
$pipe = app()->redis->multi(Redis::PIPELINE);

$pipe->zAdd($key, $now_ts, $now_ts);
$pipe->zRemRangeByScore($key, 0, $now_ts - $period * 1000);
Expand Down
20 changes: 17 additions & 3 deletions framework/Utils/ClassValueCacheUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ final protected static function getStaticCacheValue($key, $closure, $ttl = 86400
$timenow = time();
if (array_key_exists($key, static::$_StaticCacheValue)) {
if ($timenow > static::$_StaticCacheValue[$key . ':expired_at']) {
unset(static::$_StaticCacheValue[$key]);
unset(static::$_StaticCacheValue[$key . ':expired_at']);
static::cleanStaticCacheValue($key);
} else {
return static::$_StaticCacheValue[$key];
}
Expand All @@ -55,9 +54,24 @@ final protected static function getStaticCacheValue($key, $closure, $ttl = 86400
app()->redis->set(static::getStaticCacheNameSpace() . ':' . $key, $value, $ttl);
}

static::setStaticCacheValue($key, $value, $ttl, false);
return $value;
}

final protected static function setStaticCacheValue($key, $value, $ttl, $clean_first = true)
{
$timenow = time();
if ($clean_first) static::cleanStaticCacheValue($key);
static::$_StaticCacheValue[$key] = $value;
static::$_StaticCacheValue[$key . ':expired_at'] = $timenow + $ttl;
}

return $value;
final protected static function cleanStaticCacheValue($key)
{
if (array_key_exists($key, static::$_StaticCacheValue)) {
unset(static::$_StaticCacheValue[$key]);
unset(static::$_StaticCacheValue[$key . ':expired_at']);
app()->redis->del(static::getStaticCacheNameSpace() . ':' . $key);
}
}
}

0 comments on commit 4dd571d

Please sign in to comment.