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
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, callcleanupExpiredEntries()
(e.g., via a recurring timer).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
LinkedHashLruCache.SizedValue
private class
LinkedHashLruCache.SizeHandlingEvictionListener
ALruCache.EvictionListener
keeps track of size.-
Nested classes/interfaces inherited from interface io.grpc.rls.LruCache
LruCache.EvictionListener<K,V>, LruCache.EvictionType
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.LinkedHashMap<K,LinkedHashLruCache.SizedValue>
delegate
private long
estimatedMaxSizeBytes
private long
estimatedSizeBytes
private LruCache.EvictionListener<K,LinkedHashLruCache.SizedValue>
evictionListener
private com.google.common.base.Ticker
ticker
-
Constructor Summary
Constructors Constructor Description LinkedHashLruCache(long estimatedMaxSizeBytes, LruCache.EvictionListener<K,V> evictionListener, com.google.common.base.Ticker ticker)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description V
cache(K key, V value)
Populates a cache entry.boolean
cleanupExpiredEntries()
Returnstrue
if any entries were removed.private boolean
cleanupExpiredEntries(int maxExpiredEntries, long now)
private boolean
cleanupExpiredEntries(long now)
void
close()
Closes underlying resources.protected long
estimatedMaxSizeBytes()
int
estimatedSize()
Returns the estimated number of entry of the cache.long
estimatedSizeBytes()
Returns estimated cache size bytes.protected int
estimateSizeOf(K key, V value)
Returns estimated size of entry to keep track.protected boolean
fitToLimit()
Cleans up cache if needed to fit into max size bytes by removing expired entries and removing oldest entries by LRU order.boolean
hasCacheEntry(K key)
Returnstrue
if given key is cached.V
invalidate(K key)
Invalidates an entry for given key if exists.private V
invalidate(K key, LruCache.EvictionType cause)
void
invalidateAll()
Invalidates cache entries for all keys.protected abstract boolean
isExpired(K key, V value, long nowNanos)
Determines if the entry is already expired or not.V
read(K key)
Returns cached value for given key if exists, otherwisenull
.private LinkedHashLruCache.SizedValue
readInternal(K key)
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.java.util.List<V>
values()
Returns shallow copied values in the cache.
-
-
-
Field Detail
-
delegate
private final java.util.LinkedHashMap<K,LinkedHashLruCache.SizedValue> delegate
-
ticker
private final com.google.common.base.Ticker ticker
-
evictionListener
private final LruCache.EvictionListener<K,LinkedHashLruCache.SizedValue> evictionListener
-
estimatedSizeBytes
private long estimatedSizeBytes
-
estimatedMaxSizeBytes
private long estimatedMaxSizeBytes
-
-
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.
-
estimatedSizeBytes
public long estimatedSizeBytes()
Returns estimated cache size bytes. Each entry size is calculated byestimateSizeOf(java.lang.Object, java.lang.Object)
.
-
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.
-
read
@Nullable @CheckReturnValue public final V read(K key)
Description copied from interface:LruCache
Returns cached value for given key if exists, otherwisenull
. This operation doesn't return already expired cache entry.
-
readInternal
@Nullable @CheckReturnValue private LinkedHashLruCache.SizedValue readInternal(K key)
-
invalidate
@Nullable public final V invalidate(K key)
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
@Nullable private V invalidate(K key, LruCache.EvictionType cause)
-
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
@CheckReturnValue public final boolean hasCacheEntry(K key)
Description copied from interface:LruCache
Returnstrue
if given key is cached.- Specified by:
hasCacheEntry
in interfaceLruCache<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 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)
-
-