1 package net.sumaris.server.http.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.base.Preconditions;
26 import com.google.common.cache.Cache;
27 import com.google.common.cache.CacheBuilder;
28 import org.apache.commons.lang3.StringUtils;
29
30 import java.util.concurrent.TimeUnit;
31
32
33
34
35
36
37 public class ValidationExpiredCacheMap<T> {
38
39 private final Cache<String, T> cache;
40
41 public ValidationExpiredCacheMap(final int lifeTimeInSeconds) {
42 this.cache = CacheBuilder.newBuilder()
43 .expireAfterWrite(Math.max(lifeTimeInSeconds, 60 ), TimeUnit.SECONDS)
44 .build();
45 }
46
47 public boolean contains(String key) {
48 return StringUtils.isNotBlank(key) && get(key) != null;
49 }
50
51 public T get(String key) {
52 Preconditions.checkArgument(StringUtils.isNotBlank(key));
53 return cache.getIfPresent(key);
54 }
55
56 public void remove(String key) {
57 cache.invalidate(key);
58 }
59
60 public void add(String key, T data) {
61 cache.put(key, data);
62 }
63
64 public void clean() {
65 this.cache.invalidateAll();
66 }
67 }