@ SuppressWarnings ( { "unchecked" , "rawtypes" } ) public static < K , V > V getValue ( ConcurrentMap < K , CacheEntry < V > > map , K key , long timeoutMillis , Callable < V > updater , Callable < ? extends CacheEntry > cacheEntryFactory , boolean returnExpiredWhileUpdating , Object cacheRequestObject ) { CacheEntry < V > cacheEntry = map . get ( key ) ; if ( cacheEntry == null ) { try { cacheEntry = cacheEntryFactory . call ( ) ; } catch ( Exception e ) { throw new UpdateException ( e ) ; } CacheEntry < V > previousEntry = map . putIfAbsent ( key , cacheEntry ) ; if ( previousEntry != null ) { cacheEntry = previousEntry ; } } try { return cacheEntry . getValue ( timeoutMillis , updater , returnExpiredWhileUpdating , cacheRequestObject ) ; } catch ( UpdateException e ) { e . rethrowRuntimeException ( ) ; // make compiler happy return null ; } }
Gets a value from cache . If the key doesn t exist it will create the value using the updater callback Prevents cache storms with a lock