Package io.grpc.rls

Class LinkedHashLruCache<K,​V>

  • All Implemented Interfaces:
    LruCache<K,​V>
    Direct Known Subclasses:
    CachingRlsLbClient.RlsAsyncLruCache

    abstract class LinkedHashLruCache<K,​V>
    extends java.lang.Object
    implements LruCache<K,​V>
    A LinkedHashLruCache implements least recently used caching where it supports access order lru cache eviction while allowing entry level expiration time. When the cache reaches max capacity, LruCache try to remove up to one already expired entries. If it doesn't find any expired entries, it will remove based on access order of entry. To proactively clean up expired entries, call cleanupExpiredEntries() (e.g., via a recurring timer).
    • Constructor Detail

      • LinkedHashLruCache

        LinkedHashLruCache​(long estimatedMaxSizeBytes,
                           @Nullable
                           LruCache.EvictionListener<K,​V> evictionListener,
                           com.google.common.base.Ticker ticker)
    • Method Detail

      • shouldInvalidateEldestEntry

        protected boolean shouldInvalidateEldestEntry​(K eldestKey,
                                                      V eldestValue,
                                                      long now)
        Determines if the eldest entry should be kept or not when the cache size limit is reached. Note that LruCache is access level and the eldest is determined by access pattern.
      • isExpired

        protected abstract boolean isExpired​(K key,
                                             V value,
                                             long nowNanos)
        Determines if the entry is already expired or not.
      • estimateSizeOf

        protected int estimateSizeOf​(K key,
                                     V value)
        Returns estimated size of entry to keep track. If it always returns 1, the max size bytes behaves like max number of entry (default behavior).
      • estimatedMaxSizeBytes

        protected long estimatedMaxSizeBytes()
      • updateEntrySize

        public void updateEntrySize​(K key)
        Updates size for given key if entry exists. It is useful if the cache value is mutated.
      • cache

        @Nullable
        public final V cache​(K key,
                             V value)
        Description copied from interface: LruCache
        Populates a cache entry. If the cache entry for given key already exists, the value will be replaced to the new value.
        Specified by:
        cache in interface LruCache<K,​V>
        Returns:
        the previous value associated with key, otherwise null
      • read

        @Nullable
        @CheckReturnValue
        public final V read​(K key)
        Description copied from interface: LruCache
        Returns cached value for given key if exists, otherwise null. This operation doesn't return already expired cache entry.
        Specified by:
        read in interface LruCache<K,​V>
      • hasCacheEntry

        @CheckReturnValue
        public final boolean hasCacheEntry​(K key)
        Description copied from interface: LruCache
        Returns true if given key is cached.
        Specified by:
        hasCacheEntry in interface LruCache<K,​V>
      • values

        public final java.util.List<V> values()
        Returns shallow copied values in the cache.
      • fitToLimit

        protected final boolean fitToLimit()
        Cleans up cache if needed to fit into max size bytes by removing expired entries and removing oldest entries by LRU order. Returns TRUE if any unexpired entries were removed
      • resize

        public final void resize​(long newSizeBytes)
        Resizes cache. If new size is smaller than current estimated size, it will free up space by removing expired entries and removing oldest entries by LRU order.
      • estimatedSize

        @CheckReturnValue
        public final int estimatedSize()
        Description copied from interface: LruCache
        Returns the estimated number of entry of the cache. Note that the size can be larger than its true size, because there might be already expired cache.
        Specified by:
        estimatedSize in interface LruCache<K,​V>
      • cleanupExpiredEntries

        public final boolean cleanupExpiredEntries()
        Returns true if any entries were removed.
      • cleanupExpiredEntries

        private boolean cleanupExpiredEntries​(long now)
      • cleanupExpiredEntries

        private boolean cleanupExpiredEntries​(int maxExpiredEntries,
                                              long now)
      • close

        public final void close()
        Description copied from interface: LruCache
        Closes underlying resources.
        Specified by:
        close in interface LruCache<K,​V>