From f181ecbc342c48324192c75dc300080f03fc44bf Mon Sep 17 00:00:00 2001 From: RayzsYT <51991564+RayzsYT@users.noreply.github.com> Date: Sun, 12 May 2024 00:33:53 +0200 Subject: [PATCH] Add --- .../java/de/rayzs/pat/utils/ExpireCache.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/de/rayzs/pat/utils/ExpireCache.java diff --git a/src/main/java/de/rayzs/pat/utils/ExpireCache.java b/src/main/java/de/rayzs/pat/utils/ExpireCache.java new file mode 100644 index 00000000..b4587cc3 --- /dev/null +++ b/src/main/java/de/rayzs/pat/utils/ExpireCache.java @@ -0,0 +1,56 @@ +package de.rayzs.pat.utils; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + +import java.util.concurrent.TimeUnit; + +public class ExpireCache { + + private final Cache cache; + + public ExpireCache(long expireTime, TimeUnit timeUnit) { + cache = CacheBuilder.newBuilder().concurrencyLevel(2).expireAfterWrite(expireTime, timeUnit).build(); + } + + public ExpireCache(ExpireCache expireList) { + cache = expireList.getCache(); + } + + public boolean put(T t, K k) { + if(contains(t)) return false; + cache.put(t, k); + return true; + } + + public K putAndGet(T t, K k) { + put(t, k); + return k; + } + + public void putIgnoreIfContains(T t, K k) { + cache.put(t, k); + } + + public K get(T t) { + Object result = cache.getIfPresent(t);; + return result == null ? null : (K) cache.getIfPresent(t); + } + + public int getSize() { + cache.cleanUp(); + return (int) cache.size(); + } + + public Cache getCache() { + return cache; + } + + public boolean contains(T t) { + return cache.getIfPresent(t) != null; + } + + public boolean containsValue(K k) { + return cache.asMap().containsValue(k); + } +} \ No newline at end of file