cache

Details

diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java b/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
index ff8c160..2c77200 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
@@ -14,7 +14,6 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
-import java.util.logging.Logger;
 import java.util.stream.Collectors;
 
 /**
@@ -29,7 +28,6 @@ public class MultiCache<K, V> implements Cache<K, V> {
     private final HashMap<K, V> map;
     private final ConcurrentHashMap<Optional<K>, Long> keyHasTTL;
     private final CachePerformance cachePerformance;
-    private static final Logger LOGGER = Logger.getLogger(Cache.class.getName());
 
     public MultiCache() {
         this(new CachePerformance());
@@ -44,15 +42,14 @@ public class MultiCache<K, V> implements Cache<K, V> {
         this.map = new HashMap<>();
         this.keyHasTTL = new ConcurrentHashMap<>();
 
-        ScheduledExecutorService timeToLiveUpdater = Executors.newScheduledThreadPool(1);
-        timeToLiveUpdater.scheduleAtFixedRate(() -> {
+        ScheduledExecutorService batchEntriesInvalidator = Executors.newScheduledThreadPool(1);
+        batchEntriesInvalidator.scheduleAtFixedRate(() -> {
             this.keyHasTTL.forEach((key, value) -> {
-                if (value == 0) {
+                if (!isValid(key.orElse(null))) {
                     invalidate(key.orElse(null));
                 }
-                this.keyHasTTL.put(key, value--);
             });
-        }, 1, 1, TimeUnit.MILLISECONDS);
+        }, 10, 10, TimeUnit.SECONDS);
     }
 
     public CachePerformance getCachePerformance() {
@@ -62,7 +59,7 @@ public class MultiCache<K, V> implements Cache<K, V> {
     @Override
     public void put(K key, V value, long timeToLive) {
         put(key, value);
-        this.keyHasTTL.put(Optional.ofNullable(key), timeToLive);
+        this.keyHasTTL.put(Optional.ofNullable(key), System.currentTimeMillis() + timeToLive);
     }
 
     @Override
@@ -80,7 +77,7 @@ public class MultiCache<K, V> implements Cache<K, V> {
 
     @Override
     public V get(K key) throws KeyNotFoundException {
-        if (!this.map.containsKey(key)) {
+        if (!containsKey(key)) {
             this.cachePerformance.registerEvent(EventType.MISS);
             throw new KeyNotFoundException();
         }
@@ -108,19 +105,31 @@ public class MultiCache<K, V> implements Cache<K, V> {
         }
     }
 
+    private boolean isValid(K key) {
+        try {
+            return System.currentTimeMillis() <= this.keyHasTTL.get(Optional.ofNullable(key));
+        } catch (NullPointerException ex) {
+            return false;
+        }
+    }
+
     public Set<Map.Entry<K, V>> entrySet() {
         return this.map.entrySet().stream()
+                .filter(entry -> isValid(entry.getKey()))
                 .collect(Collectors.
                         toMap(entry -> entry.getKey(), entry -> entry.getValue())
                 ).entrySet();
     }
 
     public List<V> values() {
-        return this.map.values().stream().collect(Collectors.toList());
+        return this.map.entrySet().stream()
+                .filter(entry -> isValid(entry.getKey()))
+                .map(entry -> entry.getValue())
+                .collect(Collectors.toList());
     }
 
     public boolean containsKey(K key) {
-        return this.map.containsKey(key);
+        return this.map.containsKey(key) && isValid(key);
     }
 
     private String getIdentifier(V value) {