Package io.grpc.rls
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 class
private final class
ALruCache.EvictionListener
keeps track of size.Nested classes/interfaces inherited from interface io.grpc.rls.LruCache
LruCache.EvictionListener<K,
V>, LruCache.EvictionType -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final LinkedHashMap
<K, LinkedHashLruCache<K, V>.SizedValue> private long
private long
private 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 V
Populates a cache entry.final boolean
Returnstrue
if any entries were removed.private boolean
cleanupExpiredEntries
(int maxExpiredEntries, long now) private boolean
cleanupExpiredEntries
(long now) final void
close()
Closes underlying resources.protected long
final int
Returns the estimated number of entry of the cache.long
Returns estimated cache size bytes.protected int
estimateSizeOf
(K key, V value) Returns estimated size of entry to keep track.protected final boolean
Cleans up cache if needed to fit into max size bytes by removing expired entries and removing oldest entries by LRU order.final boolean
hasCacheEntry
(K key) Returnstrue
if given key is cached.final V
invalidate
(K key) Invalidates an entry for given key if exists.private V
invalidate
(K key, LruCache.EvictionType cause) final void
Invalidates cache entries for all keys.protected abstract boolean
Determines if the entry is already expired or not.final V
Returns cached value for given key if exists, otherwisenull
.private LinkedHashLruCache<K,
V>.SizedValue readInternal
(K key) final void
resize
(long newSizeBytes) Resizes cache.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.void
updateEntrySize
(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
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
Determines if the entry is already expired or not. -
estimateSizeOf
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
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:LruCache
Populates a cache entry. If the cache entry for given key already exists, the value will be replaced to the new value. -
read
Description copied from interface:LruCache
Returns cached value for given key if exists, otherwisenull
. This operation doesn't return already expired cache entry. -
readInternal
-
invalidate
Description copied from interface:LruCache
Invalidates an entry for given key if exists. This operation will triggerLruCache.EvictionListener
withLruCache.EvictionType.EXPLICIT
.- Specified by:
invalidate
in interfaceLruCache<K,
V> - Returns:
- the previous value associated with key, otherwise
null
-
invalidate
-
invalidateAll
public final void invalidateAll()Description copied from interface:LruCache
Invalidates cache entries for all keys. This operation will triggerLruCache.EvictionListener
withLruCache.EvictionType.EXPLICIT
.- Specified by:
invalidateAll
in interfaceLruCache<K,
V>
-
hasCacheEntry
Description copied from interface:LruCache
Returnstrue
if given key is cached.- Specified by:
hasCacheEntry
in interfaceLruCache<K,
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
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 interfaceLruCache<K,
V>
-
cleanupExpiredEntries
public final boolean cleanupExpiredEntries()Returnstrue
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.
-