Roberto Sánchez
2014-01-21 d7a35d13cd691e6821f774b624e4203a404e67d9
securis/src/main/java/net/curisit/securis/utils/CacheTTL.java
....@@ -1,7 +1,9 @@
11 package net.curisit.securis.utils;
22
3
+import java.util.ArrayList;
34 import java.util.Date;
4
-import java.util.Hashtable;
5
+import java.util.HashMap;
6
+import java.util.List;
57 import java.util.Map;
68
79 import javax.inject.Inject;
....@@ -25,7 +27,7 @@
2527 */
2628 private static int DEFAULT_CACHE_DURATION = 24 * 60 * 60;
2729
28
- private Map<String, CachedObject> data = new Hashtable<>();
30
+ private Map<String, CachedObject> data = new HashMap<>();
2931
3032 private Thread cleaningThread = null;
3133
....@@ -46,12 +48,17 @@
4648 }
4749 // log.info("Cheking expired objects " + new Date());
4850 Date now = new Date();
51
+ List<String> keysToRemove = new ArrayList<>();
4952 for (String key : CacheTTL.this.data.keySet()) {
5053 CachedObject co = CacheTTL.this.data.get(key);
5154 if (now.after(co.getExpireAt())) {
52
- CacheTTL.this.data.remove(key);
55
+ keysToRemove.add(key);
5356 }
5457 }
58
+ for (String key : keysToRemove) {
59
+ // If we try to remove directly in the previous loop an exception is thrown java.util.ConcurrentModificationException
60
+ CacheTTL.this.data.remove(key);
61
+ }
5562 }
5663 }
5764 });