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 classLinkedHashLruCache.SizedValueprivate classLinkedHashLruCache.SizeHandlingEvictionListenerALruCache.EvictionListenerkeeps 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>delegateprivate longestimatedMaxSizeBytesprivate longestimatedSizeBytesprivate LruCache.EvictionListener<K,LinkedHashLruCache.SizedValue>evictionListenerprivate com.google.common.base.Tickerticker
-
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 Vcache(K key, V value)Populates a cache entry.booleancleanupExpiredEntries()Returnstrueif any entries were removed.private booleancleanupExpiredEntries(int maxExpiredEntries, long now)private booleancleanupExpiredEntries(long now)voidclose()Closes underlying resources.protected longestimatedMaxSizeBytes()intestimatedSize()Returns the estimated number of entry of the cache.longestimatedSizeBytes()Returns estimated cache size bytes.protected intestimateSizeOf(K key, V value)Returns estimated size of entry to keep track.protected booleanfitToLimit()Cleans up cache if needed to fit into max size bytes by removing expired entries and removing oldest entries by LRU order.booleanhasCacheEntry(K key)Returnstrueif given key is cached.Vinvalidate(K key)Invalidates an entry for given key if exists.private Vinvalidate(K key, LruCache.EvictionType cause)voidinvalidateAll()Invalidates cache entries for all keys.protected abstract booleanisExpired(K key, V value, long nowNanos)Determines if the entry is already expired or not.Vread(K key)Returns cached value for given key if exists, otherwisenull.private LinkedHashLruCache.SizedValuereadInternal(K key)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.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:LruCachePopulates 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:LruCacheReturns 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: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
@Nullable private V invalidate(K key, LruCache.EvictionType cause)
-
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
@CheckReturnValue public final boolean hasCacheEntry(K key)
Description copied from interface:LruCacheReturnstrueif given key is cached.- Specified by:
hasCacheEntryin 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: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)
-
-