Class LocalCache<K,​V>

  • All Implemented Interfaces:
    java.util.concurrent.ConcurrentMap<K,​V>, java.util.Map<K,​V>

    class LocalCache<K,​V>
    extends java.util.AbstractMap<K,​V>
    implements java.util.concurrent.ConcurrentMap<K,​V>
    The concurrent hash map implementation built by CacheBuilder.

    This implementation is heavily derived from revision 1.96 of ConcurrentHashMap.java.

    • Field Detail

      • MAXIMUM_CAPACITY

        static final int MAXIMUM_CAPACITY
        The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments. MUST be a power of two <= 1<<30 to ensure that entries are indexable using ints.
        See Also:
        Constant Field Values
      • MAX_SEGMENTS

        static final int MAX_SEGMENTS
        The maximum number of segments to allow; used to bound constructor arguments.
        See Also:
        Constant Field Values
      • CONTAINS_VALUE_RETRIES

        static final int CONTAINS_VALUE_RETRIES
        Number of (unsynchronized) retries in the containsValue method.
        See Also:
        Constant Field Values
      • DRAIN_THRESHOLD

        static final int DRAIN_THRESHOLD
        Number of cache access operations that can be buffered per segment before the cache's recency ordering information is updated. This is used to avoid lock contention by recording a memento of reads and delaying a lock acquisition until the threshold is crossed or a mutation occurs.

        This must be a (2^n)-1 as it is used as a mask.

        See Also:
        Constant Field Values
      • DRAIN_MAX

        static final int DRAIN_MAX
        Maximum number of entries to be drained in a single cleanup run. This applies independently to the cleanup queue and both reference queues.
        See Also:
        Constant Field Values
      • logger

        static final java.util.logging.Logger logger
      • segmentMask

        final int segmentMask
        Mask value for indexing into segments. The upper bits of a key's hash code are used to choose the segment.
      • segmentShift

        final int segmentShift
        Shift value for indexing within segments. Helps prevent entries that end up in the same segment from also ending up in the same bucket.
      • segments

        final LocalCache.Segment<K,​V>[] segments
        The segments, each of which is a specialized hash table.
      • concurrencyLevel

        final int concurrencyLevel
        The concurrency level.
      • keyEquivalence

        final Equivalence<java.lang.Object> keyEquivalence
        Strategy for comparing keys.
      • valueEquivalence

        final Equivalence<java.lang.Object> valueEquivalence
        Strategy for comparing values.
      • maxWeight

        final long maxWeight
        The maximum weight of this map. UNSET_INT if there is no maximum.
      • weigher

        final Weigher<K,​V> weigher
        Weigher to weigh cache entries.
      • expireAfterAccessNanos

        final long expireAfterAccessNanos
        How long after the last access to an entry the map will retain that entry.
      • expireAfterWriteNanos

        final long expireAfterWriteNanos
        How long after the last write to an entry the map will retain that entry.
      • refreshNanos

        final long refreshNanos
        How long after the last write an entry becomes a candidate for refresh.
      • removalNotificationQueue

        final java.util.Queue<RemovalNotification<K,​V>> removalNotificationQueue
        Entries waiting to be consumed by the removal listener.
      • removalListener

        final RemovalListener<K,​V> removalListener
        A listener that is invoked when an entry is removed due to expiration or garbage collection of soft/weak entries.
      • ticker

        final Ticker ticker
        Measures time in a testable way.
      • globalStatsCounter

        final AbstractCache.StatsCounter globalStatsCounter
        Accumulates global cache statistics. Note that there are also per-segments stats counters which must be aggregated to obtain a global stats view.
      • defaultLoader

        final CacheLoader<? super K,​V> defaultLoader
        The default cache loader to use on loading operations.
      • UNSET

        static final LocalCache.ValueReference<java.lang.Object,​java.lang.Object> UNSET
        Placeholder. Indicates that the value hasn't been set yet.
      • DISCARDING_QUEUE

        static final java.util.Queue<?> DISCARDING_QUEUE
      • keySet

        java.util.Set<K> keySet
      • values

        java.util.Collection<V> values
      • entrySet

        java.util.Set<java.util.Map.Entry<K,​V>> entrySet
    • Constructor Detail

      • LocalCache

        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.
    • Method Detail

      • evictsBySize

        boolean evictsBySize()
      • customWeigher

        boolean customWeigher()
      • expires

        boolean expires()
      • expiresAfterWrite

        boolean expiresAfterWrite()
      • expiresAfterAccess

        boolean expiresAfterAccess()
      • refreshes

        boolean refreshes()
      • usesAccessQueue

        boolean usesAccessQueue()
      • usesWriteQueue

        boolean usesWriteQueue()
      • recordsWrite

        boolean recordsWrite()
      • recordsAccess

        boolean recordsAccess()
      • recordsTime

        boolean recordsTime()
      • usesWriteEntries

        boolean usesWriteEntries()
      • usesAccessEntries

        boolean usesAccessEntries()
      • usesKeyReferences

        boolean usesKeyReferences()
      • usesValueReferences

        boolean usesValueReferences()
      • unset

        static <K,​V> LocalCache.ValueReference<K,​V> unset()
        Singleton placeholder that indicates a value is being loaded.
      • discardingQueue

        static <E> java.util.Queue<E> discardingQueue()
        Queue that discards all elements.
      • rehash

        static int rehash​(int h)
        Applies a supplemental hash function to a given hash code, which defends against poor quality hash functions. This is critical when the concurrent hash map uses power-of-two length hash tables, that otherwise encounter collisions for hash codes that do not differ in lower or upper bits.
        Parameters:
        h - hash code
      • hash

        int hash​(java.lang.Object key)
      • segmentFor

        LocalCache.Segment<K,​V> segmentFor​(int hash)
        Returns the segment that should be used for a key with the given hash.
        Parameters:
        hash - the hash code for the key
        Returns:
        the segment
      • isExpired

        boolean isExpired​(ReferenceEntry<K,​V> entry,
                          long now)
        Returns true if the entry has expired.
      • nullifyAccessOrder

        static <K,​V> void nullifyAccessOrder​(ReferenceEntry<K,​V> nulled)
      • nullifyWriteOrder

        static <K,​V> void nullifyWriteOrder​(ReferenceEntry<K,​V> nulled)
      • processPendingNotifications

        void processPendingNotifications()
        Notifies listeners that an entry has been automatically removed due to expiration, eviction, or eligibility for garbage collection. This should be called every time expireEntries or evictEntry is called (once the lock is released).
      • cleanUp

        public void cleanUp()
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Overrides:
        isEmpty in class java.util.AbstractMap<K,​V>
      • longSize

        long longSize()
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<K,​V>
        Overrides:
        size in class java.util.AbstractMap<K,​V>
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.AbstractMap<K,​V>
      • get

        V get​(K key,
              CacheLoader<? super K,​V> loader)
        throws java.util.concurrent.ExecutionException
        Throws:
        java.util.concurrent.ExecutionException
      • getIfPresent

        public V getIfPresent​(java.lang.Object key)
      • getOrDefault

        public V getOrDefault​(java.lang.Object key,
                              V defaultValue)
        Specified by:
        getOrDefault in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        getOrDefault in interface java.util.Map<K,​V>
      • getOrLoad

        V getOrLoad​(K key)
             throws java.util.concurrent.ExecutionException
        Throws:
        java.util.concurrent.ExecutionException
      • getAllPresent

        ImmutableMap<K,​V> getAllPresent​(java.lang.Iterable<?> keys)
      • getAll

        ImmutableMap<K,​V> getAll​(java.lang.Iterable<? extends K> keys)
                                throws java.util.concurrent.ExecutionException
        Throws:
        java.util.concurrent.ExecutionException
      • getEntry

        ReferenceEntry<K,​V> getEntry​(java.lang.Object key)
        Returns the internal entry for the specified key. The entry may be loading, expired, or partially collected.
      • refresh

        void refresh​(K key)
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Overrides:
        containsKey in class java.util.AbstractMap<K,​V>
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Overrides:
        containsValue in class java.util.AbstractMap<K,​V>
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.AbstractMap<K,​V>
      • putIfAbsent

        public V putIfAbsent​(K key,
                             V value)
        Specified by:
        putIfAbsent in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        putIfAbsent in interface java.util.Map<K,​V>
      • compute

        public V compute​(K key,
                         java.util.function.BiFunction<? super K,​? super V,​? extends V> function)
        Specified by:
        compute in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        compute in interface java.util.Map<K,​V>
      • computeIfAbsent

        public V computeIfAbsent​(K key,
                                 java.util.function.Function<? super K,​? extends V> function)
        Specified by:
        computeIfAbsent in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        computeIfAbsent in interface java.util.Map<K,​V>
      • computeIfPresent

        public V computeIfPresent​(K key,
                                  java.util.function.BiFunction<? super K,​? super V,​? extends V> function)
        Specified by:
        computeIfPresent in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        computeIfPresent in interface java.util.Map<K,​V>
      • merge

        public V merge​(K key,
                       V newValue,
                       java.util.function.BiFunction<? super V,​? super V,​? extends V> function)
        Specified by:
        merge in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        merge in interface java.util.Map<K,​V>
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Overrides:
        putAll in class java.util.AbstractMap<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.AbstractMap<K,​V>
      • remove

        public boolean remove​(java.lang.Object key,
                              java.lang.Object value)
        Specified by:
        remove in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        remove in interface java.util.Map<K,​V>
      • replace

        public boolean replace​(K key,
                               V oldValue,
                               V newValue)
        Specified by:
        replace in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        replace in interface java.util.Map<K,​V>
      • replace

        public V replace​(K key,
                         V value)
        Specified by:
        replace in interface java.util.concurrent.ConcurrentMap<K,​V>
        Specified by:
        replace in interface java.util.Map<K,​V>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.AbstractMap<K,​V>
      • invalidateAll

        void invalidateAll​(java.lang.Iterable<?> keys)
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Overrides:
        keySet in class java.util.AbstractMap<K,​V>
      • values

        public java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
        Overrides:
        values in class java.util.AbstractMap<K,​V>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Specified by:
        entrySet in class java.util.AbstractMap<K,​V>
      • toArrayList

        private static <E> java.util.ArrayList<E> toArrayList​(java.util.Collection<E> c)
      • removeIf

        boolean removeIf​(java.util.function.BiPredicate<? super K,​? super V> filter)