perf(限流服务): 优化Redis查询以减少命令数量
使用zcount替代zremrangebyscore和zcard组合查询,减少一次Redis操作
This commit is contained in:
@@ -292,11 +292,10 @@ class RateLimiterService:
|
|||||||
key = f"rate_limit:qps:{api_key_id}"
|
key = f"rate_limit:qps:{api_key_id}"
|
||||||
|
|
||||||
async with self.redis.pipeline() as pipe:
|
async with self.redis.pipeline() as pipe:
|
||||||
pipe.zremrangebyscore(key, 0, window_start)
|
pipe.zcount(key, window_start, "+inf")
|
||||||
pipe.zcard(key)
|
|
||||||
results = await pipe.execute()
|
results = await pipe.execute()
|
||||||
|
|
||||||
current = results[1]
|
current = results[0]
|
||||||
|
|
||||||
if current >= limit:
|
if current >= limit:
|
||||||
return False, {
|
return False, {
|
||||||
|
|||||||
Reference in New Issue
Block a user