From ac20f9ed019409d1e0518a110e7129b7f662e23b Mon Sep 17 00:00:00 2001 From: Ajinkya Date: Thu, 2 Jan 2025 14:32:15 +0530 Subject: [PATCH] setting redis ttl while sync to redis instead of increment op --- .../detection/cache/RedisBackedCounterCache.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/apps/threat-detection/src/main/java/com/akto/threat/detection/cache/RedisBackedCounterCache.java b/apps/threat-detection/src/main/java/com/akto/threat/detection/cache/RedisBackedCounterCache.java index 42de770b62..2182be09c6 100644 --- a/apps/threat-detection/src/main/java/com/akto/threat/detection/cache/RedisBackedCounterCache.java +++ b/apps/threat-detection/src/main/java/com/akto/threat/detection/cache/RedisBackedCounterCache.java @@ -5,7 +5,9 @@ import io.lettuce.core.ExpireArgs; import io.lettuce.core.RedisClient; import io.lettuce.core.api.StatefulRedisConnection; +import java.util.HashSet; import java.util.Optional; +import java.util.Set; import java.util.concurrent.*; public class RedisBackedCounterCache implements CounterCache { @@ -39,8 +41,7 @@ public long getValue() { public RedisBackedCounterCache(RedisClient redisClient, String prefix) { this.prefix = prefix; this.redis = redisClient.connect(new LongValueCodec()); - this.localCache = - Caffeine.newBuilder().maximumSize(100000).expireAfterWrite(3, TimeUnit.HOURS).build(); + this.localCache = Caffeine.newBuilder().maximumSize(100000).expireAfterWrite(3, TimeUnit.HOURS).build(); ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); executor.scheduleAtFixedRate(this::syncToRedis, 60, 5, TimeUnit.SECONDS); @@ -63,8 +64,6 @@ public void incrementBy(String key, long val) { String _key = addPrefixToKey(key); localCache.asMap().merge(_key, val, Long::sum); pendingIncOps.add(new Op(_key, val)); - - this.setExpiryIfNotSet(_key, 3 * 60 * 60); // added 3 hours expiry for now } @Override @@ -93,6 +92,7 @@ private void setExpiryIfNotSet(String key, long seconds) { } private void syncToRedis() { + Set _keys = new HashSet<>(); while (!pendingIncOps.isEmpty()) { Op op = pendingIncOps.poll(); String key = op.getKey(); @@ -111,12 +111,16 @@ private void syncToRedis() { ex.printStackTrace(); } + _keys.add(key); + if (result != null) { localCache.asMap().put(key, result); } }); } + _keys.forEach(key -> setExpiryIfNotSet(key, 3 * 60 * 60)); + this.deletedKeys.clear(); } }