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}"
|
||||
|
||||
async with self.redis.pipeline() as pipe:
|
||||
pipe.zremrangebyscore(key, 0, window_start)
|
||||
pipe.zcard(key)
|
||||
pipe.zcount(key, window_start, "+inf")
|
||||
results = await pipe.execute()
|
||||
|
||||
current = results[1]
|
||||
current = results[0]
|
||||
|
||||
if current >= limit:
|
||||
return False, {
|
||||
|
||||
Reference in New Issue
Block a user