Guava의 CacheBuilder와 CacheLoader 예제이다.
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections.MapUtils;
import org.junit.Test;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
public class GuavaTest {
CacheLoader<String, String> loader = new CacheLoader<String, String>() {
@Override
public String load(String key) {
return key.toUpperCase();
}
};
@Test
public void simpleTest() {
System.out.println("--- simple ---");
LoadingCache<String, String> cache = CacheBuilder.newBuilder().build(loader);
cache.getUnchecked("hello");
cache.getUnchecked("world");
MapUtils.debugPrint(System.out, "map", cache.asMap());
System.out.println(cache.size());
}
@Test
public void sizeLimitTest() {
System.out.println("--- size limit test ---");
LoadingCache<String, String> cache = CacheBuilder.newBuilder().maximumSize(3).build(loader);
cache.getUnchecked("a");
cache.getUnchecked("b");
cache.getUnchecked("c");
cache.getUnchecked("d");
MapUtils.debugPrint(System.out, "map", cache.asMap());
System.out.println(cache.size());
System.out.println(cache.getIfPresent("a")); // a는 evit 되었다.
System.out.println(cache.getIfPresent("d"));
}
@Test
public void timeLimitTest() throws InterruptedException {
System.out.println("--- time limit test ---");
LoadingCache<String, String> cache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.SECONDS).build(loader);
cache.getUnchecked("Samuel");
System.out.println(cache.getIfPresent("Samuel"));
Thread.sleep(800);
System.out.println(cache.getIfPresent("Samuel")); // a는 evit되었다.
}
@Test
public void preloadTest() {
System.out.println("-- preload test ---");
Map<String, String> map = new HashMap<String, String>();
map.put("samuel", "SAMUEL");
map.put("jason", "JASON");
LoadingCache<String, String> cache = CacheBuilder.newBuilder().build(loader);
cache.putAll(map);
MapUtils.debugPrint(System.out, "map", cache.asMap());
}
@Test
public void removeNotiTest() {
System.out.println("-- remove test ---");
RemovalListener<String, String> listener = new RemovalListener<String, String>() {
@Override
public void onRemoval(RemovalNotification<String, String> n){
if (n.wasEvicted()) {
String cause = n.getCause().name();
System.out.println("[removed] cause : " + cause + ", key : " + n.getKey());
}
}
};
LoadingCache<String, String> cache = CacheBuilder.newBuilder().maximumSize(2).removalListener(listener).build(loader);
cache.getUnchecked("samuel");
cache.getUnchecked("jason");
cache.getUnchecked("kalley");
}
@Test
public void etc() {
// LoadingCache<String, String> cache = CacheBuilder.newBuilder().refreshAfterWrite(1,TimeUnit.MINUTES).build(loader);
// LoadingCache<String, String> cache = CacheBuilder.newBuilder().weakKeys().build(loader);
// LoadingCache<String, String> cache = CacheBuilder.newBuilder().softKeys().build(loader);
}
}
결과
--- size limit test ---
map =
{
c = C java.lang.String
b = B java.lang.String
d = D java.lang.String
} com.google.common.cache.LocalCache
3
null
D
--- simple ---
map =
{
hello = HELLO java.lang.String
world = WORLD java.lang.String
} com.google.common.cache.LocalCache
2
-- preload test ---
map =
{
jason = JASON java.lang.String
samuel = SAMUEL java.lang.String
} com.google.common.cache.LocalCache
--- time limit test ---
SAMUEL
SAMUEL
-- remove test ---
[removed] cause : SIZE, key : samuel
'general java' 카테고리의 다른 글
[Guava] Futures 예제 (0) | 2015.04.17 |
---|---|
[Guava] Preconditions 예제 (0) | 2015.04.16 |
[Guava] Collections2.filter (0) | 2015.04.14 |
[Guava] Multimap 예제 (0) | 2015.04.11 |
[Guava] 자주 사용하는 Lists api (0) | 2015.04.10 |