diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/Caffeine.java b/src/main/java/br/ufrgs/inf/prosoft/cache/Caffeine.java
index ef65574..89869c6 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/Caffeine.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/Caffeine.java
@@ -5,19 +5,22 @@
*/
package br.ufrgs.inf.prosoft.cache;
-import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
/**
*
* @author root
+ * @param <K>
+ * @param <V>
*/
public class Caffeine<K, V> implements Cache<K, V> {
private static final boolean CACHE_EVALUATE_PERFORMANCE = System.getenv("CACHE_EVENTS") == null || !System.getenv("CACHE_EVENTS").equals("false");
private static final boolean CACHE_REGISTER_SIZE = System.getenv("CACHE_REGISTER_SIZE") != null && System.getenv("CACHE_REGISTER_SIZE").equals("true");
- private final com.github.benmanes.caffeine.cache.Cache<K, V> cache;
+ private final com.github.benmanes.caffeine.cache.Cache<Optional<K>, Optional<V>> cache;
private final CachePerformance cachePerformance;
public Caffeine() {
@@ -80,24 +83,24 @@ public class Caffeine<K, V> implements Cache<K, V> {
@Override
public void put(K key, V value) {
- this.cache.put(key, value);
+ this.cache.put(Optional.ofNullable(key), Optional.ofNullable(value));
registerEvent(EventType.ADDITION, value);
}
@Override
public V get(K key) throws KeyNotFoundException {
- V get = this.cache.getIfPresent(key);
+ Optional<V> get = this.cache.getIfPresent(Optional.ofNullable(key));
if (get == null) {
registerEvent(EventType.MISS, key);
throw new KeyNotFoundException();
}
registerEvent(EventType.HIT, get);
- return get;
+ return get.orElse(null);
}
@Override
public void invalidate(K key) {
- this.cache.invalidate(key);
+ this.cache.invalidate(Optional.ofNullable(key));
registerEvent(EventType.INVALIDATION, null);
}
@@ -121,6 +124,8 @@ public class Caffeine<K, V> implements Cache<K, V> {
}
public List<V> values() {
- return new ArrayList<>(this.cache.asMap().values());
+ return this.cache.asMap().values().stream()
+ .map(value -> value.orElse(null))
+ .collect(Collectors.toList());
}
}
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/CaffeineGetterCache.java b/src/main/java/br/ufrgs/inf/prosoft/cache/CaffeineGetterCache.java
index 5d3f2c4..e036f6c 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/CaffeineGetterCache.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/CaffeineGetterCache.java
@@ -12,7 +12,7 @@ import java.util.function.Supplier;
* @author root
* @param <V>
*/
-public class CaffeineGetterCache<V> extends CaffeineSingleCache<String, V> {
+public class CaffeineGetterCache<V> extends CaffeineSingleCache<Void, V> {
public CaffeineGetterCache(String name) {
super(name);
@@ -31,27 +31,27 @@ public class CaffeineGetterCache<V> extends CaffeineSingleCache<String, V> {
}
public void put(V value, long timeToLive) {
- super.put("", value, timeToLive);
+ super.put(null, value, timeToLive);
}
public void put(V value) {
- super.put("", value);
+ super.put(null, value);
}
public V get() throws KeyNotFoundException {
- return super.get("");
+ return super.get(null);
}
public void invalidate() {
- super.invalidate("");
+ super.invalidate(null);
}
public V computeIfAbsent(Supplier<V> supplier, long timeToLive) {
- return super.computeIfAbsent("", supplier, timeToLive);
+ return super.computeIfAbsent(null, supplier, timeToLive);
}
public V computeIfAbsent(Supplier<V> supplier) {
- return super.computeIfAbsent("", supplier);
+ return super.computeIfAbsent(null, supplier);
}
}