From 8636be9a48a506fdaba50a9d1e19a11967643659 Mon Sep 17 00:00:00 2001 From: nagocoler Date: Thu, 22 Feb 2024 23:24:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DRedis=E9=99=90=E6=B5=81?= =?UTF-8?q?=E4=B8=ADincrement=E6=A6=82=E7=8E=87=E6=80=A7=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E4=B8=80=E4=B8=AA=E6=97=A0=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E7=9A=84=E6=96=B0=E9=94=AE=E5=80=BC=E5=AF=B9?= =?UTF-8?q?=EF=BC=88=E5=88=9A=E5=A5=BD=E8=BF=87=E6=9C=9F=EF=BC=89=E6=AD=A4?= =?UTF-8?q?=E6=97=B6=E4=BC=9A=E5=AF=BC=E8=87=B4=E7=94=A8=E6=88=B7=E6=B0=B8?= =?UTF-8?q?=E4=B9=85=E8=A2=AB=E5=B0=81=E7=A6=81=EF=BC=88=E6=B0=B8=E8=BF=9C?= =?UTF-8?q?=E8=BF=94=E5=9B=9E403=E6=88=96=E6=98=AF401=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/example/utils/FlowUtils.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/my-project-backend/src/main/java/com/example/utils/FlowUtils.java b/my-project-backend/src/main/java/com/example/utils/FlowUtils.java index f5b0af6..9be85b4 100644 --- a/my-project-backend/src/main/java/com/example/utils/FlowUtils.java +++ b/my-project-backend/src/main/java/com/example/utils/FlowUtils.java @@ -72,8 +72,12 @@ public class FlowUtils { * @return 是否通过限流检查 */ private boolean internalCheck(String key, int frequency, int period, LimitAction action){ - if (Boolean.TRUE.equals(template.hasKey(key))) { - Long value = Optional.ofNullable(template.opsForValue().increment(key)).orElse(0L); + String count = template.opsForValue().get(key); + if (count != null) { + long value = Optional.ofNullable(template.opsForValue().increment(key)).orElse(0L); + int c = Integer.parseInt(count); + if(value != c + 1) + template.expire(key, period, TimeUnit.SECONDS); return action.run(value > frequency); } else { template.opsForValue().set(key, "1", period, TimeUnit.SECONDS);