This commit is contained in:
RayzsYT committed 2024-05-12 00:33:53 +02:00
1 parent 5fc0d7c6ca
commit f181ecbc34
1 file changed
+56
@@ -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<T, K> {
private final Cache<Object, Object> cache;
public ExpireCache(long expireTime, TimeUnit timeUnit) {
cache = CacheBuilder.newBuilder().concurrencyLevel(2).expireAfterWrite(expireTime, timeUnit).build();
}
public ExpireCache(ExpireCache<T, K> 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<Object, Object> getCache() {
return cache;
}
public boolean contains(T t) {
return cache.getIfPresent(t) != null;
}
public boolean containsValue(K k) {
return cache.asMap().containsValue(k);
}
}