GuavaUtil.java
2.63 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
86
87
88
package org.nafmii.common.utils;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.Synchronized;
import java.util.concurrent.TimeUnit;
/**
* @Author: zjh
* @Date: 2020/7/8 13:28
* @Description:
*/
public class GuavaUtil {
private static Cache<String, Object> guavaCache;
private static Cache<String, Object> guavaCacheForFaceData;
public GuavaUtil() {
}
/**
* 跟踪连缓存
* @return
*/
public static Cache<String, Object> getGuavaCache() {
if (guavaCache == null) {
guavaCache = CacheBuilder.newBuilder().concurrencyLevel(10)
// 存活时间 10 分钟
.expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(100000000).build();
}
return guavaCache;
}
/**
*
* @return
*/
public static Cache<String, Object> getGuavaCacheForFaceData() {
if (guavaCacheForFaceData == null) {
guavaCacheForFaceData = CacheBuilder.newBuilder().concurrencyLevel(10)
.expireAfterWrite(1, TimeUnit.MINUTES).maximumSize(100000000).build();
}
return guavaCacheForFaceData;
}
@Synchronized
public static Object putTrackGuava(String str, Object obj){
if (guavaCache == null) {
guavaCache = CacheBuilder.newBuilder().concurrencyLevel(10)
.expireAfterWrite(1, TimeUnit.MINUTES).maximumSize(100000000).build();
}
return guavaCache.asMap().put(str,obj);
}
public static Object getTrackGuava(String str){
if (guavaCache == null) {
guavaCache = CacheBuilder.newBuilder().build();
}
return guavaCache.asMap().get(str);
}
public static void deleteTrackGuava(String str){
guavaCache =CacheBuilder.newBuilder().build();
guavaCache.invalidate(str);
}
@Synchronized
public static Object putFaceGuava(String str, Object obj){
return guavaCacheForFaceData.asMap().put(str,obj);
}
public static Object getFaceGuava(String str){
return guavaCacheForFaceData.asMap().get(str);
}
public static void deleteFaceGuava(String str){
guavaCacheForFaceData.invalidate(str);
}
public static void main(String[] args) {
// Cache<String, Object> guavaCache = GuavaUtil.getGuavaCache();
//
// guavaCache.asMap().put("asd","asd");
// String asd = (String) guavaCache.asMap().get("asd");
// System.out.println(asd);
GuavaUtil.putTrackGuava("test","1");
String test = (String) GuavaUtil.getTrackGuava("test");
System.out.println(test);
}
}