-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathConcurrentCachingParser.java
More file actions
127 lines (109 loc) · 4.39 KB
/
ConcurrentCachingParser.java
File metadata and controls
127 lines (109 loc) · 4.39 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package ua_parser;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
/**
* This class is concurrent version of CachingParser.
*
* @author kyryloholodnov
*/
public class ConcurrentCachingParser extends Parser {
private static long cacheKeyExpiresAfterAccessMs = 24 * 60L * 60 * 1000; // 24 hours
private static long cacheMaximumSize = 1000;
private final LoadingCache<String, Client> cacheClient = CacheBuilder.newBuilder()
.expireAfterAccess(cacheKeyExpiresAfterAccessMs, TimeUnit.MILLISECONDS)
.maximumSize(cacheMaximumSize)
.build(new CacheLoader<String, Client>() {
@Override
@ParametersAreNonnullByDefault
public Client load(String agentString) throws Exception {
return ConcurrentCachingParser.super.parse(agentString);
}
});
private final LoadingCache<String, UserAgent> cacheUserAgent = CacheBuilder.newBuilder()
.expireAfterAccess(cacheKeyExpiresAfterAccessMs, TimeUnit.MILLISECONDS)
.maximumSize(cacheMaximumSize)
.build(new CacheLoader<String, UserAgent>() {
@Override
@ParametersAreNonnullByDefault
public UserAgent load(String agentString) throws Exception {
return ConcurrentCachingParser.super.parseUserAgent(agentString);
}
});
private final LoadingCache<String, Device> cacheDevice = CacheBuilder.newBuilder()
.expireAfterAccess(cacheKeyExpiresAfterAccessMs, TimeUnit.MILLISECONDS)
.maximumSize(cacheMaximumSize)
.build(new CacheLoader<String, Device>() {
@Override
@ParametersAreNonnullByDefault
public Device load(String agentString) throws Exception {
return ConcurrentCachingParser.super.parseDevice(agentString);
}
});
private final LoadingCache<String, OS> cacheOS = CacheBuilder.newBuilder()
.expireAfterAccess(cacheKeyExpiresAfterAccessMs, TimeUnit.MILLISECONDS)
.maximumSize(cacheMaximumSize)
.build(new CacheLoader<String, OS>() {
@Override
@ParametersAreNonnullByDefault
public OS load(String agentString) throws Exception {
return ConcurrentCachingParser.super.parseOS(agentString);
}
});
public ConcurrentCachingParser() throws IOException {
super();
}
public ConcurrentCachingParser(InputStream regexYaml) {
super(regexYaml);
}
@Override
public Client parse(String agentString) {
if (agentString == null) {
return null;
}
return cacheClient.getUnchecked(agentString);
}
@Override
public UserAgent parseUserAgent(String agentString) {
if (agentString == null) {
return null;
}
return cacheUserAgent.getUnchecked(agentString);
}
@Override
public Device parseDevice(String agentString) {
if (agentString == null) {
return null;
}
return cacheDevice.getUnchecked(agentString);
}
@Override
public OS parseOS(String agentString) {
if (agentString == null) {
return null;
}
return cacheOS.getUnchecked(agentString);
}
public static long getCacheKeyExpiresAfterAccessMs() {
return cacheKeyExpiresAfterAccessMs;
}
public static long getCacheMaximumSize() {
return cacheMaximumSize;
}
public static void setCacheMaximumSize(long cacheMaximumSize) {
if (cacheMaximumSize < 1) {
throw new IllegalArgumentException("Cache size should be positive value");
}
ConcurrentCachingParser.cacheMaximumSize = cacheMaximumSize;
}
public static void setCacheKeyExpiresAfterAccessMs(long cacheKeyExpiresAfterAccessMs) {
if (cacheKeyExpiresAfterAccessMs < 1) {
throw new IllegalArgumentException("Cache key expiration should be positive value");
}
ConcurrentCachingParser.cacheKeyExpiresAfterAccessMs = cacheKeyExpiresAfterAccessMs;
}
}