문서의 이전 판입니다!
Cache.asMap().get(“key”)
로 값이 null인지 먼저 검사하면 된다.null
값 자체를 리턴할 필요가 있다면 Guava Optional을 사용한다.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; } });
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()); }