-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCache.java
More file actions
85 lines (63 loc) · 1.95 KB
/
Cache.java
File metadata and controls
85 lines (63 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package br.com.brasilapi;
import java.util.HashMap;
import java.util.Map;
class Cache {
private static boolean enableCache = false;
private static Long startTime = System.currentTimeMillis();
private static Long cacheTime = 600000L;
private static Map<Class<?>, Map<String, Object>> mapCache = new HashMap<>();
protected Cache() {
}
protected static void setEnableCache(boolean enableCache) {
Cache.enableCache = enableCache;
}
protected static boolean getEnableCache() {
return Cache.enableCache;
}
protected static void setCacheTime(Long time) {
Cache.enableCache = true;
Cache.cacheTime = time;
}
protected static Long getCacheTime() {
return Cache.cacheTime;
}
protected static void setCache(Class<?> classAPIModel, String code, Object obj) {
updateCache();
if (!mapCache.containsKey(classAPIModel)) {
Map<String, Object> mapObj = new HashMap<>();
mapCache.put(classAPIModel, mapObj);
Log.setConsole("Salvo em Cache.");
}
mapCache.get(classAPIModel).put(code, obj);
}
protected static Object getCache(Class<?> classAPIModel, String code) {
updateCache();
if (!mapCache.containsKey(classAPIModel)) {
return null;
}
Object obj = mapCache.get(classAPIModel).get(code);
if (obj != null) {
Log.setConsole("Obtido do Cache.");
return obj;
}
return null;
}
/**
* Verifica e atualiza o Cache limpando e redefinido o tempo atual.
*/
private static void updateCache() {
// Caso o tempo do Cache definido do Cache tenha excedido
if (!mapCache.isEmpty() && System.currentTimeMillis() - startTime > cacheTime) {
// Log do Cache a ser limpo
Log.setConsole("Cache atual a ser limpo: " + mapCache.toString());
// Limpar Cache
mapCache.clear();
// Atualizar tempo atual
startTime = System.currentTimeMillis();
// Log do Cache atual
Log.setConsole("Tempo de " + cacheTime
+ " milissegundos excedido.\nCache limpo. Cache atual: "
+ mapCache.toString());
}
}
}