본문 바로가기

Programming!

Guava Cache

10초 유지


LoadingCache


private LoadingCache<Long, Optional<Integer>> loadingCache = CacheBuilder.newBuilder().maximumSize(20).expireAfterAccess(10, TimeUnit.SECONDS).build(

new CacheLoader<Long, Optional<Integer>>() {

public Optional<Integer> load(Long key) {

return Optional.fromNullable(get반환메소드(key));

}

});


public Integer getLoadingCache(final Long key) {

try {

return loadingCache.get(key).or(DEFAULT_QUANTITY);

} catch (ExecutionException e) {

...

}

return DEFAULT_QUANTITY;

}



Cache


private Cache<Long, Optional<Integer>> cachedXXXXX = CacheBuilder.newBuilder().maximumSize(최대갯수).expireAfterAccess(10, TimeUnit.SECONDS).build();



public Integer get...(final Long key) {

return cachedXXXXX.get(key, new Callable<Optional<Integer>>() {

@Override

public Optional<Integer> call() throws Exception {

return Optional.fromNullable(get반환메소드(key));

}

}).or(DEFAULT_QUANTITY);

...



https://code.google.com/p/guava-libraries/wiki/CachesExplained