사용자 도구

사이트 도구


java:guava:cachebuilder

문서의 이전 판입니다!


Guava CacheBuilder

LoadingCache

  • LoadingCache 인스턴스는 객체 build시에 키에 해당하는 값을 로딩하는 로더를 지정해줘야한다. 권장.
LoadingCache<Key,Value> loadingCache = CacheBuilder.newBuilder()
  .some settings
  .build(new CacheLoader<Key, Value>() {
    @Override
    public Value load(Key cacheKey) throws Exception {
       ... blah blarh..
       return value;
    }
  });

com.google.common.cache.Cache

  • 그냥 Cache는 build시에 CacheLoader를 지정하지 않고, get할 때 값을 가져오는 로직을 넣을 수 있다.
Cache<Key, Value> cache = CacheBuilder.newBuilder()
    .maximumSize(1000)
    .build(); // look Ma, no CacheLoader
...
try {
  // If the key wasn't in the "easy to compute" group, we need to
  // do things the hard way.
  cache.get(key, new Callable<Value>() {
    @Override
    public Value call() throws AnyException {
      return doThingsTheHardWay(key);
    }
  });
} catch (ExecutionException e) {
  throw new OtherException(e.getCause());
}
java/guava/cachebuilder.1482760609.txt.gz · 마지막으로 수정됨: 2016/12/26 22:26 저자 kwon37xi