diff --git a/AssetManager.API/Middleware/RateLimitMiddleware.cs b/AssetManager.API/Middleware/RateLimitMiddleware.cs index e33e7f2..cc80b47 100644 --- a/AssetManager.API/Middleware/RateLimitMiddleware.cs +++ b/AssetManager.API/Middleware/RateLimitMiddleware.cs @@ -54,14 +54,11 @@ public class RateLimitMiddleware } // 计数+1,首次访问设置过期时间 - if (requestCount == 0) + var cacheEntryOptions = new MemoryCacheEntryOptions { - _cache.Set(cacheKey, 1, TimeSpan.FromSeconds(WindowSeconds)); - } - else - { - _cache.Set(cacheKey, requestCount + 1, _cache.GetEntry(cacheKey).AbsoluteExpirationRelativeToNow ?? TimeSpan.FromSeconds(WindowSeconds)); - } + AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(WindowSeconds) + }; + _cache.Set(cacheKey, requestCount + 1, cacheEntryOptions); await _next(context); }