Class LinkedHashLruCache<K,V>
java.lang.Object
io.grpc.rls.LinkedHashLruCache<K,V>
- All Implemented Interfaces:
LruCache<K,V>
- Direct Known Subclasses:
CachingRlsLbClient.RlsAsyncLruCache
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).-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final classprivate final classALruCache.EvictionListenerkeeps track of size.Nested classes/interfaces inherited from interface LruCache
LruCache.EvictionListener<K,V>, LruCache.EvictionType -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final LinkedHashMap<K, LinkedHashLruCache<K, V>.SizedValue> private longprivate longprivate final LruCache.EvictionListener<K, LinkedHashLruCache<K, V>.SizedValue> private final com.google.common.base.Ticker -
Constructor Summary
ConstructorsConstructorDescriptionLinkedHashLruCache(long estimatedMaxSizeBytes, LruCache.EvictionListener<K, V> evictionListener, com.google.common.base.Ticker ticker) -
Method Summary
Modifier and TypeMethodDescriptionfinal VPopulates a cache entry.final booleanReturnstrueif any entries were removed.private booleancleanupExpiredEntries(int maxExpiredEntries, long now) private booleancleanupExpiredEntries(long now) final voidclose()Closes underlying resources.protected longfinal intReturns the estimated number of entry of the cache.longReturns estimated cache size bytes.protected intestimateSizeOf(K key, V value) Returns estimated size of entry to keep track.protected final booleanCleans up cache if needed to fit into max size bytes by removing expired entries and removing oldest entries by LRU order.final booleanhasCacheEntry(K key) Returnstrueif given key is cached.final Vinvalidate(K key) Invalidates an entry for given key if exists.private Vinvalidate(K key, LruCache.EvictionType cause) final voidInvalidates cache entries for all keys.protected abstract booleanDetermines if the entry is already expired or not.final VReturns cached value for given key if exists, otherwisenull.private LinkedHashLruCache<K,V>.SizedValue readInternal(K key) final voidresize(long newSizeBytes) Resizes cache.protected booleanshouldInvalidateEldestEntry(K eldestKey, V eldestValue, long now) Determines if the eldest entry should be kept or not when the cache size limit is reached.voidupdateEntrySize(K key) Updates size for given key if entry exists.values()Returns shallow copied values in the cache.
-
Field Details
-
delegate
-
ticker
private final com.google.common.base.Ticker ticker -
evictionListener
-
estimatedSizeBytes
private long estimatedSizeBytes -
estimatedMaxSizeBytes
private long estimatedMaxSizeBytes
-
-
Constructor Details
-
LinkedHashLruCache
LinkedHashLruCache(long estimatedMaxSizeBytes, @Nullable LruCache.EvictionListener<K, V> evictionListener, com.google.common.base.Ticker ticker)
-
-
Method Details
-
shouldInvalidateEldestEntry
-
isExpired
-
estimateSizeOf
-
estimatedMaxSizeBytes
protected long estimatedMaxSizeBytes() -
updateEntrySize
Updates size for given key if entry exists. It is useful if the cache value is mutated. -
estimatedSizeBytes
public long estimatedSizeBytes()Returns estimated cache size bytes. Each entry size is calculated byestimateSizeOf(java.lang.Object, java.lang.Object). -
cache
Description copied from interface:LruCachePopulates a cache entry. If the cache entry for given key already exists, the value will be replaced to the new value. -
read
-
readInternal
-
invalidate
Description copied from interface:LruCacheInvalidates an entry for given key if exists. This operation will triggerLruCache.EvictionListenerwithLruCache.EvictionType.EXPLICIT.- Specified by:
invalidatein interfaceLruCache<K,V> - Returns:
- the previous value associated with key, otherwise
null
-
invalidate
-
invalidateAll
public final void invalidateAll()Description copied from interface:LruCacheInvalidates cache entries for all keys. This operation will triggerLruCache.EvictionListenerwithLruCache.EvictionType.EXPLICIT.- Specified by:
invalidateAllin interfaceLruCache<K,V>
-
hasCacheEntry
Description copied from interface:LruCacheReturnstrueif given key is cached.- Specified by:
hasCacheEntryin interfaceLruCache<K,V>
-
values
-
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
Description copied from interface:LruCacheReturns 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:
estimatedSizein interfaceLruCache<K,V>
-
cleanupExpiredEntries
public final boolean cleanupExpiredEntries()Returnstrueif any entries were removed. -
cleanupExpiredEntries
private boolean cleanupExpiredEntries(long now) -
cleanupExpiredEntries
private boolean cleanupExpiredEntries(int maxExpiredEntries, long now) -
close
-