Roberto Sánchez
2014-09-19 8d5386be38db25a2a41c3bf6c876adee21ca26cc
securis/src/main/java/net/curisit/securis/utils/CacheTTL.java
....@@ -13,120 +13,122 @@
1313 import org.apache.logging.log4j.LogManager;
1414
1515 /**
16
- * Cache implementation with TTL (time To Live) The objects are removed from cache when TTL is reached.
16
+ * Cache implementation with TTL (time To Live) The objects are removed from
17
+ * cache when TTL is reached.
1718 *
1819 * @author roberto <roberto.sanchez@curisit.net>
1920 */
2021 @Singleton
2122 public class CacheTTL {
2223
23
- private static final Logger LOG = LogManager.getLogger(CacheTTL.class);
24
+ private static final Logger LOG = LogManager.getLogger(CacheTTL.class);
2425
25
- /**
26
- * Period before token expires, set in seconds.
27
- */
28
- private static int DEFAULT_CACHE_DURATION = 24 * 60 * 60;
26
+ /**
27
+ * Period before token expires, set in seconds.
28
+ */
29
+ private static int DEFAULT_CACHE_DURATION = 24 * 60 * 60;
2930
30
- private Map<String, CachedObject> data = new HashMap<>();
31
+ private Map<String, CachedObject> data = new HashMap<>();
3132
32
- private Thread cleaningThread = null;
33
+ private Thread cleaningThread = null;
3334
34
- @Inject
35
- public CacheTTL() {
36
- cleaningThread = new Thread(new Runnable() {
35
+ @Inject
36
+ public CacheTTL() {
37
+ cleaningThread = new Thread(new Runnable() {
3738
38
- @Override
39
- public void run() {
40
- while (CacheTTL.this.data != null) {
41
- try {
42
- // We check for expired object every 60 seconds
43
- Thread.sleep(60 * 1000);
44
- } catch (InterruptedException e) {
45
- LOG.error("Exiting from Cache Thread");
46
- data.clear();
47
- return;
48
- }
49
- // LOG.info("Cheking expired objects " + new Date());
50
- Date now = new Date();
51
- List<String> keysToRemove = new ArrayList<>();
52
- for (String key : CacheTTL.this.data.keySet()) {
53
- CachedObject co = CacheTTL.this.data.get(key);
54
- if (now.after(co.getExpireAt())) {
55
- keysToRemove.add(key);
56
- }
57
- }
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
- }
62
- }
63
- }
64
- });
65
- cleaningThread.start();
66
- }
39
+ @Override
40
+ public void run() {
41
+ while (CacheTTL.this.data != null) {
42
+ try {
43
+ // We check for expired object every 60 seconds
44
+ Thread.sleep(60 * 1000);
45
+ } catch (InterruptedException e) {
46
+ LOG.error("Exiting from Cache Thread");
47
+ data.clear();
48
+ return;
49
+ }
50
+ Date now = new Date();
51
+ List<String> keysToRemove = new ArrayList<>();
52
+ for (String key : CacheTTL.this.data.keySet()) {
53
+ CachedObject co = CacheTTL.this.data.get(key);
54
+ if (now.after(co.getExpireAt())) {
55
+ keysToRemove.add(key);
56
+ }
57
+ }
58
+ for (String key : keysToRemove) {
59
+ // If we try to remove directly in the previous loop an
60
+ // exception is thrown
61
+ // java.util.ConcurrentModificationException
62
+ CacheTTL.this.data.remove(key);
63
+ }
64
+ }
65
+ }
66
+ });
67
+ cleaningThread.start();
68
+ }
6769
68
- /**
69
- *
70
- * @param key
71
- * @param obj
72
- * @param ttl
73
- * Time To Live in seconds
74
- */
75
- public void set(String key, Object obj, int ttl) {
76
- Date expirationDate = new Date(new Date().getTime() + ttl * 1000);
77
- data.put(key, new CachedObject(expirationDate, obj));
78
- }
70
+ /**
71
+ *
72
+ * @param key
73
+ * @param obj
74
+ * @param ttl
75
+ * Time To Live in seconds
76
+ */
77
+ public void set(String key, Object obj, int ttl) {
78
+ Date expirationDate = new Date(new Date().getTime() + ttl * 1000);
79
+ data.put(key, new CachedObject(expirationDate, obj));
80
+ }
7981
80
- public void set(String key, Object obj) {
81
- set(key, obj, DEFAULT_CACHE_DURATION);
82
- }
82
+ public void set(String key, Object obj) {
83
+ set(key, obj, DEFAULT_CACHE_DURATION);
84
+ }
8385
84
- public Object get(String key) {
85
- CachedObject co = data.get(key);
86
- return co == null ? null : co.getObject();
87
- }
86
+ public Object get(String key) {
87
+ CachedObject co = data.get(key);
88
+ return co == null ? null : co.getObject();
89
+ }
8890
89
- public <T> T get(String key, Class<T> type) {
90
- CachedObject co = data.get(key);
91
- return co == null ? null : co.getObject(type);
92
- }
91
+ public <T> T get(String key, Class<T> type) {
92
+ CachedObject co = data.get(key);
93
+ return co == null ? null : co.getObject(type);
94
+ }
9395
94
- public <T> T remove(String key, Class<T> type) {
95
- CachedObject co = data.remove(key);
96
- return co == null ? null : co.getObject(type);
97
- }
96
+ public <T> T remove(String key, Class<T> type) {
97
+ CachedObject co = data.remove(key);
98
+ return co == null ? null : co.getObject(type);
99
+ }
98100
99
- public Object remove(String key) {
100
- CachedObject co = data.remove(key);
101
- return co == null ? null : co.getObject();
102
- }
101
+ public Object remove(String key) {
102
+ CachedObject co = data.remove(key);
103
+ return co == null ? null : co.getObject();
104
+ }
103105
104
- public void clear() {
105
- data.clear();
106
- }
106
+ public void clear() {
107
+ data.clear();
108
+ }
107109
108
- private class CachedObject {
109
- Date expireAt;
110
- Object object;
110
+ private class CachedObject {
111
+ Date expireAt;
112
+ Object object;
111113
112
- public CachedObject(Date date, Object obj) {
113
- expireAt = date;
114
- object = obj;
115
- }
114
+ public CachedObject(Date date, Object obj) {
115
+ expireAt = date;
116
+ object = obj;
117
+ }
116118
117
- public Date getExpireAt() {
118
- return expireAt;
119
- }
119
+ public Date getExpireAt() {
120
+ return expireAt;
121
+ }
120122
121
- public Object getObject() {
122
- return object;
123
- }
123
+ public Object getObject() {
124
+ return object;
125
+ }
124126
125
- @SuppressWarnings("unchecked")
126
- public <T> T getObject(Class<T> type) {
127
- return (T) object;
128
- }
127
+ @SuppressWarnings("unchecked")
128
+ public <T> T getObject(Class<T> type) {
129
+ return (T) object;
130
+ }
129131
130
- }
132
+ }
131133
132134 }