@GwtCompatible(emulated=true) class LocalCache<K,V> extends java.util.AbstractMap<K,V> implements java.util.concurrent.ConcurrentMap<K,V>
CacheBuilder
.
This implementation is heavily derived from revision 1.96 of ConcurrentHashMap.java.
Modifier and Type | Class and Description |
---|---|
(package private) class |
LocalCache.AbstractCacheSet<T> |
(package private) static class |
LocalCache.AbstractReferenceEntry<K,V> |
(package private) static class |
LocalCache.AccessQueue<K,V>
A custom queue for managing access order.
|
(package private) static class |
LocalCache.EntryFactory
Creates new entries.
|
(package private) class |
LocalCache.EntryIterator |
(package private) class |
LocalCache.EntrySet |
(package private) class |
LocalCache.HashIterator<T> |
(package private) class |
LocalCache.KeyIterator |
(package private) class |
LocalCache.KeySet |
(package private) static class |
LocalCache.LoadingSerializationProxy<K,V>
Serializes the configuration of a LocalCache, reconsitituting it as an LoadingCache using
CacheBuilder upon deserialization.
|
(package private) static class |
LocalCache.LoadingValueReference<K,V> |
(package private) static class |
LocalCache.LocalLoadingCache<K,V> |
(package private) static class |
LocalCache.LocalManualCache<K,V> |
(package private) static class |
LocalCache.ManualSerializationProxy<K,V>
Serializes the configuration of a LocalCache, reconsitituting it as a Cache using
CacheBuilder upon deserialization.
|
private static class |
LocalCache.NullEntry |
(package private) static interface |
LocalCache.ReferenceEntry<K,V>
An entry in a reference map.
|
(package private) static class |
LocalCache.Segment<K,V>
Segments are specialized versions of hash tables.
|
(package private) static class |
LocalCache.SoftValueReference<K,V>
References a soft value.
|
(package private) static class |
LocalCache.Strength |
(package private) static class |
LocalCache.StrongAccessEntry<K,V> |
(package private) static class |
LocalCache.StrongAccessWriteEntry<K,V> |
(package private) static class |
LocalCache.StrongEntry<K,V>
Used for strongly-referenced keys.
|
(package private) static class |
LocalCache.StrongValueReference<K,V>
References a strong value.
|
(package private) static class |
LocalCache.StrongWriteEntry<K,V> |
(package private) class |
LocalCache.ValueIterator |
(package private) static interface |
LocalCache.ValueReference<K,V>
A reference to a value.
|
(package private) class |
LocalCache.Values |
(package private) static class |
LocalCache.WeakAccessEntry<K,V> |
(package private) static class |
LocalCache.WeakAccessWriteEntry<K,V> |
(package private) static class |
LocalCache.WeakEntry<K,V>
Used for weakly-referenced keys.
|
(package private) static class |
LocalCache.WeakValueReference<K,V>
References a weak value.
|
(package private) static class |
LocalCache.WeakWriteEntry<K,V> |
(package private) static class |
LocalCache.WeightedSoftValueReference<K,V>
References a soft value.
|
(package private) static class |
LocalCache.WeightedStrongValueReference<K,V>
References a strong value.
|
(package private) static class |
LocalCache.WeightedWeakValueReference<K,V>
References a weak value.
|
(package private) static class |
LocalCache.WriteQueue<K,V>
A custom queue for managing eviction order.
|
(package private) class |
LocalCache.WriteThroughEntry
Custom Entry class used by EntryIterator.next(), that relays setValue changes to the
underlying map.
|
Modifier and Type | Field and Description |
---|---|
(package private) int |
concurrencyLevel
The concurrency level.
|
(package private) static int |
CONTAINS_VALUE_RETRIES
Number of (unsynchronized) retries in the containsValue method.
|
(package private) CacheLoader<? super K,V> |
defaultLoader
The default cache loader to use on loading operations.
|
(package private) static java.util.Queue<? extends java.lang.Object> |
DISCARDING_QUEUE |
(package private) static int |
DRAIN_MAX
Maximum number of entries to be drained in a single cleanup run.
|
(package private) static int |
DRAIN_THRESHOLD
Number of cache access operations that can be buffered per segment before the cache's recency
ordering information is updated.
|
(package private) LocalCache.EntryFactory |
entryFactory
Factory used to create new entries.
|
(package private) java.util.Set<java.util.Map.Entry<K,V>> |
entrySet |
(package private) long |
expireAfterAccessNanos
How long after the last access to an entry the map will retain that entry.
|
(package private) long |
expireAfterWriteNanos
How long after the last write to an entry the map will retain that entry.
|
(package private) AbstractCache.StatsCounter |
globalStatsCounter
Accumulates global cache statistics.
|
(package private) Equivalence<java.lang.Object> |
keyEquivalence
Strategy for comparing keys.
|
(package private) java.util.Set<K> |
keySet |
(package private) LocalCache.Strength |
keyStrength
Strategy for referencing keys.
|
(package private) static java.util.logging.Logger |
logger |
(package private) static int |
MAX_SEGMENTS
The maximum number of segments to allow; used to bound constructor arguments.
|
(package private) static int |
MAXIMUM_CAPACITY
The maximum capacity, used if a higher value is implicitly specified by either of the
constructors with arguments.
|
(package private) long |
maxWeight
The maximum weight of this map.
|
(package private) long |
refreshNanos
How long after the last write an entry becomes a candidate for refresh.
|
(package private) RemovalListener<K,V> |
removalListener
A listener that is invoked when an entry is removed due to expiration or garbage collection of
soft/weak entries.
|
(package private) java.util.Queue<RemovalNotification<K,V>> |
removalNotificationQueue
Entries waiting to be consumed by the removal listener.
|
(package private) int |
segmentMask
Mask value for indexing into segments.
|
(package private) LocalCache.Segment<K,V>[] |
segments
The segments, each of which is a specialized hash table.
|
(package private) int |
segmentShift
Shift value for indexing within segments.
|
(package private) Ticker |
ticker
Measures time in a testable way.
|
(package private) static LocalCache.ValueReference<java.lang.Object,java.lang.Object> |
UNSET
Placeholder.
|
(package private) Equivalence<java.lang.Object> |
valueEquivalence
Strategy for comparing values.
|
(package private) java.util.Collection<V> |
values |
(package private) LocalCache.Strength |
valueStrength
Strategy for referencing values.
|
(package private) Weigher<K,V> |
weigher
Weigher to weigh cache entries.
|
Constructor and Description |
---|
LocalCache(CacheBuilder<? super K,? super V> builder,
CacheLoader<? super K,V> loader)
Creates a new, empty map with the specified strategy, initial capacity and concurrency level.
|
Modifier and Type | Method and Description |
---|---|
void |
cleanUp() |
void |
clear() |
(package private) static <K,V> void |
connectAccessOrder(LocalCache.ReferenceEntry<K,V> previous,
LocalCache.ReferenceEntry<K,V> next) |
(package private) static <K,V> void |
connectWriteOrder(LocalCache.ReferenceEntry<K,V> previous,
LocalCache.ReferenceEntry<K,V> next) |
boolean |
containsKey(java.lang.Object key) |
boolean |
containsValue(java.lang.Object value) |
(package private) LocalCache.ReferenceEntry<K,V> |
copyEntry(LocalCache.ReferenceEntry<K,V> original,
LocalCache.ReferenceEntry<K,V> newNext)
This method is a convenience for testing.
|
(package private) LocalCache.Segment<K,V> |
createSegment(int initialCapacity,
long maxSegmentWeight,
AbstractCache.StatsCounter statsCounter) |
(package private) boolean |
customWeigher() |
(package private) static <E> java.util.Queue<E> |
discardingQueue()
Queue that discards all elements.
|
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet() |
(package private) boolean |
evictsBySize() |
(package private) boolean |
expires() |
(package private) boolean |
expiresAfterAccess() |
(package private) boolean |
expiresAfterWrite() |
(package private) V |
get(K key,
CacheLoader<? super K,V> loader) |
V |
get(java.lang.Object key) |
(package private) ImmutableMap<K,V> |
getAll(java.lang.Iterable<? extends K> keys) |
(package private) ImmutableMap<K,V> |
getAllPresent(java.lang.Iterable<?> keys) |
(package private) LocalCache.ReferenceEntry<K,V> |
getEntry(java.lang.Object key)
Returns the internal entry for the specified key.
|
V |
getIfPresent(java.lang.Object key) |
(package private) V |
getLiveValue(LocalCache.ReferenceEntry<K,V> entry,
long now)
Gets the value from an entry.
|
(package private) V |
getOrLoad(K key) |
(package private) int |
hash(java.lang.Object key) |
(package private) void |
invalidateAll(java.lang.Iterable<?> keys) |
boolean |
isEmpty() |
(package private) boolean |
isExpired(LocalCache.ReferenceEntry<K,V> entry,
long now)
Returns true if the entry has expired.
|
(package private) boolean |
isLive(LocalCache.ReferenceEntry<K,V> entry,
long now)
This method is a convenience for testing.
|
java.util.Set<K> |
keySet() |
(package private) java.util.Map<K,V> |
loadAll(java.util.Set<? extends K> keys,
CacheLoader<? super K,V> loader)
Returns the result of calling
CacheLoader.loadAll(java.lang.Iterable<? extends K>) , or null if loader doesn't
implement loadAll . |
(package private) long |
longSize() |
(package private) LocalCache.ReferenceEntry<K,V> |
newEntry(K key,
int hash,
LocalCache.ReferenceEntry<K,V> next)
This method is a convenience for testing.
|
(package private) LocalCache.Segment<K,V>[] |
newSegmentArray(int ssize) |
(package private) LocalCache.ValueReference<K,V> |
newValueReference(LocalCache.ReferenceEntry<K,V> entry,
V value,
int weight)
This method is a convenience for testing.
|
(package private) static <K,V> LocalCache.ReferenceEntry<K,V> |
nullEntry() |
(package private) static <K,V> void |
nullifyAccessOrder(LocalCache.ReferenceEntry<K,V> nulled) |
(package private) static <K,V> void |
nullifyWriteOrder(LocalCache.ReferenceEntry<K,V> nulled) |
(package private) void |
processPendingNotifications()
Notifies listeners that an entry has been automatically removed due to expiration, eviction,
or eligibility for garbage collection.
|
V |
put(K key,
V value) |
void |
putAll(java.util.Map<? extends K,? extends V> m) |
V |
putIfAbsent(K key,
V value) |
(package private) void |
reclaimKey(LocalCache.ReferenceEntry<K,V> entry) |
(package private) void |
reclaimValue(LocalCache.ValueReference<K,V> valueReference) |
(package private) boolean |
recordsAccess() |
(package private) boolean |
recordsTime() |
(package private) boolean |
recordsWrite() |
(package private) void |
refresh(K key) |
(package private) boolean |
refreshes() |
(package private) static int |
rehash(int h)
Applies a supplemental hash function to a given hash code, which defends against poor quality
hash functions.
|
V |
remove(java.lang.Object key) |
boolean |
remove(java.lang.Object key,
java.lang.Object value) |
V |
replace(K key,
V value) |
boolean |
replace(K key,
V oldValue,
V newValue) |
(package private) LocalCache.Segment<K,V> |
segmentFor(int hash)
Returns the segment that should be used for a key with the given hash.
|
int |
size() |
(package private) static <K,V> LocalCache.ValueReference<K,V> |
unset()
Singleton placeholder that indicates a value is being loaded.
|
(package private) boolean |
usesAccessEntries() |
(package private) boolean |
usesAccessQueue() |
(package private) boolean |
usesKeyReferences() |
(package private) boolean |
usesValueReferences() |
(package private) boolean |
usesWriteEntries() |
(package private) boolean |
usesWriteQueue() |
java.util.Collection<V> |
values() |
finalize, getClass, notify, notifyAll, wait, wait, wait
static final int MAXIMUM_CAPACITY
static final int MAX_SEGMENTS
static final int CONTAINS_VALUE_RETRIES
static final int DRAIN_THRESHOLD
This must be a (2^n)-1 as it is used as a mask.
static final int DRAIN_MAX
static final java.util.logging.Logger logger
final int segmentMask
final int segmentShift
final LocalCache.Segment<K,V>[] segments
final int concurrencyLevel
final Equivalence<java.lang.Object> keyEquivalence
final Equivalence<java.lang.Object> valueEquivalence
final LocalCache.Strength keyStrength
final LocalCache.Strength valueStrength
final long maxWeight
final long expireAfterAccessNanos
final long expireAfterWriteNanos
final long refreshNanos
final java.util.Queue<RemovalNotification<K,V>> removalNotificationQueue
final RemovalListener<K,V> removalListener
final Ticker ticker
final LocalCache.EntryFactory entryFactory
final AbstractCache.StatsCounter globalStatsCounter
@Nullable final CacheLoader<? super K,V> defaultLoader
static final LocalCache.ValueReference<java.lang.Object,java.lang.Object> UNSET
static final java.util.Queue<? extends java.lang.Object> DISCARDING_QUEUE
java.util.Set<K> keySet
java.util.Collection<V> values
LocalCache(CacheBuilder<? super K,? super V> builder, @Nullable CacheLoader<? super K,V> loader)
boolean evictsBySize()
boolean customWeigher()
boolean expires()
boolean expiresAfterWrite()
boolean expiresAfterAccess()
boolean refreshes()
boolean usesAccessQueue()
boolean usesWriteQueue()
boolean recordsWrite()
boolean recordsAccess()
boolean recordsTime()
boolean usesWriteEntries()
boolean usesAccessEntries()
boolean usesKeyReferences()
boolean usesValueReferences()
static <K,V> LocalCache.ValueReference<K,V> unset()
static <K,V> LocalCache.ReferenceEntry<K,V> nullEntry()
static <E> java.util.Queue<E> discardingQueue()
static int rehash(int h)
h
- hash codeLocalCache.ReferenceEntry<K,V> newEntry(K key, int hash, @Nullable LocalCache.ReferenceEntry<K,V> next)
LocalCache.Segment.newEntry(K, int, com.google.common.cache.LocalCache.ReferenceEntry<K, V>)
directly.LocalCache.ReferenceEntry<K,V> copyEntry(LocalCache.ReferenceEntry<K,V> original, LocalCache.ReferenceEntry<K,V> newNext)
LocalCache.Segment.copyEntry(com.google.common.cache.LocalCache.ReferenceEntry<K, V>, com.google.common.cache.LocalCache.ReferenceEntry<K, V>)
directly.LocalCache.ValueReference<K,V> newValueReference(LocalCache.ReferenceEntry<K,V> entry, V value, int weight)
LocalCache.Segment.setValue(com.google.common.cache.LocalCache.ReferenceEntry<K, V>, K, V, long)
instead.int hash(@Nullable java.lang.Object key)
void reclaimValue(LocalCache.ValueReference<K,V> valueReference)
void reclaimKey(LocalCache.ReferenceEntry<K,V> entry)
boolean isLive(LocalCache.ReferenceEntry<K,V> entry, long now)
LocalCache.Segment.getLiveValue(com.google.common.cache.LocalCache.ReferenceEntry<K, V>, long)
instead.LocalCache.Segment<K,V> segmentFor(int hash)
hash
- the hash code for the keyLocalCache.Segment<K,V> createSegment(int initialCapacity, long maxSegmentWeight, AbstractCache.StatsCounter statsCounter)
@Nullable V getLiveValue(LocalCache.ReferenceEntry<K,V> entry, long now)
LocalCache.Segment.getLiveValue(com.google.common.cache.LocalCache.ReferenceEntry<K, V>, long)
this method does not attempt to
cleanup stale entries. As such it should only be called outside of a segment context, such as
during iteration.boolean isExpired(LocalCache.ReferenceEntry<K,V> entry, long now)
static <K,V> void connectAccessOrder(LocalCache.ReferenceEntry<K,V> previous, LocalCache.ReferenceEntry<K,V> next)
static <K,V> void nullifyAccessOrder(LocalCache.ReferenceEntry<K,V> nulled)
static <K,V> void connectWriteOrder(LocalCache.ReferenceEntry<K,V> previous, LocalCache.ReferenceEntry<K,V> next)
static <K,V> void nullifyWriteOrder(LocalCache.ReferenceEntry<K,V> nulled)
void processPendingNotifications()
final LocalCache.Segment<K,V>[] newSegmentArray(int ssize)
public void cleanUp()
public boolean isEmpty()
long longSize()
public int size()
@Nullable public V get(@Nullable java.lang.Object key)
@Nullable public V getIfPresent(java.lang.Object key)
V get(K key, CacheLoader<? super K,V> loader) throws java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException
V getOrLoad(K key) throws java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException
ImmutableMap<K,V> getAllPresent(java.lang.Iterable<?> keys)
ImmutableMap<K,V> getAll(java.lang.Iterable<? extends K> keys) throws java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException
@Nullable java.util.Map<K,V> loadAll(java.util.Set<? extends K> keys, CacheLoader<? super K,V> loader) throws java.util.concurrent.ExecutionException
CacheLoader.loadAll(java.lang.Iterable<? extends K>)
, or null if loader
doesn't
implement loadAll
.java.util.concurrent.ExecutionException
LocalCache.ReferenceEntry<K,V> getEntry(@Nullable java.lang.Object key)
void refresh(K key)
public boolean containsKey(@Nullable java.lang.Object key)
public boolean containsValue(@Nullable java.lang.Object value)
public V remove(@Nullable java.lang.Object key)
public boolean remove(@Nullable java.lang.Object key, @Nullable java.lang.Object value)
public void clear()
void invalidateAll(java.lang.Iterable<?> keys)
public java.util.Set<K> keySet()
public java.util.Collection<V> values()