enum OneWeigher implements Weigher<Object, Object> { INSTANCE; @Override public int weigh(Object key, Object value) { return 1; } }
.build(loader); one.getUnchecked(new Object()); assertEquals(1, one.size()); assertFalse(one.asMap().isEmpty()); LocalLoadingCache<Object, Object> two = SerializableTester.reserialize(one); assertEquals(0, two.size()); assertTrue(two.asMap().isEmpty());
@Override public V getUnchecked(K key) { try { return get(key); } catch (ExecutionException e) { throw new UncheckedExecutionException(e.getCause()); } }
.maximumSize(2); LocalLoadingCache<Object, Object> cache = makeCache(builder, identityLoader()); assertEquals(EMPTY_STATS, cache.stats()); cache.getUnchecked(one); CacheStats stats = cache.stats(); assertEquals(1, stats.requestCount()); assertEquals(0, stats.hitCount()); assertEquals(0, stats.evictionCount()); cache.getUnchecked(one); stats = cache.stats(); assertEquals(2, stats.requestCount()); assertEquals(1, stats.hitCount()); cache.getUnchecked(two); stats = cache.stats(); assertEquals(3, stats.requestCount()); assertEquals(1, stats.hitCount()); cache.getUnchecked(three); stats = cache.stats(); assertEquals(4, stats.requestCount()); assertEquals(1, stats.hitCount());
.build(loader); one.getUnchecked(new Object()); assertEquals(1, one.size()); assertFalse(one.asMap().isEmpty()); LocalLoadingCache<Object, Object> two = SerializableTester.reserialize(one); assertEquals(0, two.size()); assertTrue(two.asMap().isEmpty());
public void testAsMap() { CacheBuilder<Object, Object> builder = createCacheBuilder(); LocalLoadingCache<Object, Object> cache = makeCache(builder, identityLoader()); assertEquals(EMPTY_STATS, cache.stats()); Object three = new Object(); ConcurrentMap<Object, Object> map = cache.asMap(); assertNull(map.put(one, two)); assertSame(two, map.get(one)); assertEquals(0, map.size()); cache.getUnchecked(one); assertEquals(1, map.size()); assertSame(one, map.get(one)); assertEquals(0, map.size()); cache.getUnchecked(one); assertEquals(1, map.size()); assertFalse(map.remove(one, two)); assertEquals(0, map.size()); cache.getUnchecked(one); Map<Object, Object> newMap = ImmutableMap.of(one, one); assertEquals(newMap, map);
public void testRecordStats() { CacheBuilder<Object, Object> builder = createCacheBuilder() .recordStats() .concurrencyLevel(1) .maximumSize(2); LocalLoadingCache<Object, Object> cache = makeCache(builder, identityLoader()); assertEquals(0, cache.stats().hitCount()); assertEquals(0, cache.stats().missCount()); Object one = new Object(); cache.getUnchecked(one); assertEquals(0, cache.stats().hitCount()); assertEquals(1, cache.stats().missCount()); cache.getUnchecked(one); assertEquals(1, cache.stats().hitCount()); assertEquals(1, cache.stats().missCount()); Object two = new Object(); cache.getUnchecked(two); assertEquals(1, cache.stats().hitCount()); assertEquals(2, cache.stats().missCount()); Object three = new Object(); cache.getUnchecked(three); assertEquals(1, cache.stats().hitCount()); assertEquals(3, cache.stats().missCount()); }
/** * Lookups on the map view shouldn't impact the recency queue. */ public void testAsMapRecency() { CacheBuilder<Object, Object> builder = createCacheBuilder() .concurrencyLevel(1) .maximumSize(SMALL_MAX_SIZE); LocalLoadingCache<Object, Object> cache = makeCache(builder, identityLoader()); Segment<Object, Object> segment = cache.localCache.segments[0]; ConcurrentMap<Object, Object> map = cache.asMap(); Object one = new Object(); assertSame(one, cache.getUnchecked(one)); assertTrue(segment.recencyQueue.isEmpty()); assertSame(one, map.get(one)); assertSame(one, segment.recencyQueue.peek().getKey()); assertSame(one, cache.getUnchecked(one)); assertFalse(segment.recencyQueue.isEmpty()); }
public void testNoStats() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder() .concurrencyLevel(1) .maximumSize(2); LocalLoadingCache<Object, Object> cache = makeCache(builder, identityLoader()); assertEquals(EMPTY_STATS, cache.stats()); Object one = new Object(); cache.getUnchecked(one); assertEquals(EMPTY_STATS, cache.stats()); cache.getUnchecked(one); assertEquals(EMPTY_STATS, cache.stats()); Object two = new Object(); cache.getUnchecked(two); assertEquals(EMPTY_STATS, cache.stats()); Object three = new Object(); cache.getUnchecked(three); assertEquals(EMPTY_STATS, cache.stats()); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its loaded * value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its * loaded value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<K1, V1>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its * loaded value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<K1, V1>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its * loaded value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<K1, V1>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its * loaded value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<K1, V1>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its * loaded value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<K1, V1>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its * loaded value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<K1, V1>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its loaded * value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its * loaded value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<K1, V1>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its * loaded value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<K1, V1>(this, loader); }
/** * Builds a cache, which either returns an already-loaded value for a given key or atomically * computes or retrieves it using the supplied {@code CacheLoader}. If another thread is currently * loading the value for this key, simply waits for that thread to finish and returns its * loaded value. Note that multiple threads can concurrently load values for distinct keys. * * <p>This method does not alter the state of this {@code CacheBuilder} instance, so it can be * invoked again to create multiple independent caches. * * @param loader the cache loader used to obtain new values * @return a cache having the requested features */ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build( CacheLoader<? super K1, V1> loader) { checkWeightWithWeigher(); return new LocalCache.LocalLoadingCache<K1, V1>(this, loader); }