use of nl.basjes.parse.useragent.AbstractUserAgentAnalyzer.CacheInstantiator in project yauaa by nielsbasjes.
the class TestCaching method testCustomCacheImplementationInline.
@Test
void testCustomCacheImplementationInline() {
String userAgent = "Mozilla/5.0 (Linux; Android 8.1.0; Pixel Build/OPM4.171019.021.D1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.98 Mobile Safari/537.36";
UserAgentAnalyzer uaa = UserAgentAnalyzer.newBuilder().withCacheInstantiator(new CacheInstantiator() {
@Override
public Map<String, ImmutableUserAgent> instantiateCache(int cacheSize) {
// The Map MUST be synchronized
return Collections.synchronizedMap(// This is ONLY for this test.
new LRUMap<String, ImmutableUserAgent>(cacheSize) {
@Override
public ImmutableUserAgent get(Object key) {
LOG.info("Did a GET on {}", key);
return super.get(key);
}
@Override
public ImmutableUserAgent put(String key, ImmutableUserAgent value) {
LOG.info("Did a PUT on {}", key);
return super.put(key, value);
}
});
}
}).withCache(10).hideMatcherLoadStats().build();
// First time
UserAgent agent1 = uaa.parse(userAgent);
// Should come from cache
UserAgent agent2 = uaa.parse(userAgent);
// Both should be the same
assertEquals(agent1, agent2);
}
Aggregations