| .. | .. |
|---|
| 13 | 13 | import org.apache.logging.log4j.LogManager; |
|---|
| 14 | 14 | |
|---|
| 15 | 15 | /** |
|---|
| 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. |
|---|
| 17 | 18 | * |
|---|
| 18 | 19 | * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 19 | 20 | */ |
|---|
| 20 | 21 | @Singleton |
|---|
| 21 | 22 | public class CacheTTL { |
|---|
| 22 | 23 | |
|---|
| 23 | | - private static final Logger LOG = LogManager.getLogger(CacheTTL.class); |
|---|
| 24 | + private static final Logger LOG = LogManager.getLogger(CacheTTL.class); |
|---|
| 24 | 25 | |
|---|
| 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; |
|---|
| 29 | 30 | |
|---|
| 30 | | - private Map<String, CachedObject> data = new HashMap<>(); |
|---|
| 31 | + private Map<String, CachedObject> data = new HashMap<>(); |
|---|
| 31 | 32 | |
|---|
| 32 | | - private Thread cleaningThread = null; |
|---|
| 33 | + private Thread cleaningThread = null; |
|---|
| 33 | 34 | |
|---|
| 34 | | - @Inject |
|---|
| 35 | | - public CacheTTL() { |
|---|
| 36 | | - cleaningThread = new Thread(new Runnable() { |
|---|
| 35 | + @Inject |
|---|
| 36 | + public CacheTTL() { |
|---|
| 37 | + cleaningThread = new Thread(new Runnable() { |
|---|
| 37 | 38 | |
|---|
| 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 | + } |
|---|
| 67 | 69 | |
|---|
| 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 | + } |
|---|
| 79 | 81 | |
|---|
| 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 | + } |
|---|
| 83 | 85 | |
|---|
| 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 | + } |
|---|
| 88 | 90 | |
|---|
| 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 | + } |
|---|
| 93 | 95 | |
|---|
| 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 | + } |
|---|
| 98 | 100 | |
|---|
| 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 | + } |
|---|
| 103 | 105 | |
|---|
| 104 | | - public void clear() { |
|---|
| 105 | | - data.clear(); |
|---|
| 106 | | - } |
|---|
| 106 | + public void clear() { |
|---|
| 107 | + data.clear(); |
|---|
| 108 | + } |
|---|
| 107 | 109 | |
|---|
| 108 | | - private class CachedObject { |
|---|
| 109 | | - Date expireAt; |
|---|
| 110 | | - Object object; |
|---|
| 110 | + private class CachedObject { |
|---|
| 111 | + Date expireAt; |
|---|
| 112 | + Object object; |
|---|
| 111 | 113 | |
|---|
| 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 | + } |
|---|
| 116 | 118 | |
|---|
| 117 | | - public Date getExpireAt() { |
|---|
| 118 | | - return expireAt; |
|---|
| 119 | | - } |
|---|
| 119 | + public Date getExpireAt() { |
|---|
| 120 | + return expireAt; |
|---|
| 121 | + } |
|---|
| 120 | 122 | |
|---|
| 121 | | - public Object getObject() { |
|---|
| 122 | | - return object; |
|---|
| 123 | | - } |
|---|
| 123 | + public Object getObject() { |
|---|
| 124 | + return object; |
|---|
| 125 | + } |
|---|
| 124 | 126 | |
|---|
| 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 | + } |
|---|
| 129 | 131 | |
|---|
| 130 | | - } |
|---|
| 132 | + } |
|---|
| 131 | 133 | |
|---|
| 132 | 134 | } |
|---|