public class Cache<K,V> extends Object
A generic cache which keeps the total memory consumed by its content below a configured maximum.
Thread safe.
Modifier and Type | Class and Description |
---|---|
private static class |
Cache.AgingCacheValue<K,V> |
private static class |
Cache.CacheKey<K> |
private static class |
Cache.CacheValue<K,V> |
Modifier and Type | Field and Description |
---|---|
private com.yahoo.cache.SizeCalculator |
calc |
private Map<Cache.CacheKey<K>,Cache.CacheValue<K,V>> |
content |
private long |
currentSizeBytes |
private com.yahoo.statistics.Value |
elems |
private com.yahoo.statistics.Value |
entrySizes |
private long |
maxEntrySizeBytes
The max allowed size of an entry
|
private long |
maxSizeBytes |
private long |
timeToLiveMillis
The time an element is allowed to live, negative for indefinite lifespan
|
Constructor and Description |
---|
Cache(long maxSizeBytes,
long timeToLiveMillis,
long maxEntrySizeBytes,
com.yahoo.statistics.Statistics manager)
Creates a new cache
|
Modifier and Type | Method and Description |
---|---|
boolean |
containsKey(K k) |
V |
get(K key)
Returns a value, if it is present in the cache
|
private void |
initStats(com.yahoo.statistics.Statistics manager) |
private void |
makeRoomForBytes(long bytes) |
boolean |
put(K key,
V value)
Attempts to add a value to the cache
|
boolean |
remove(K key)
Removes a cache value if present
|
int |
size() |
private Cache.CacheValue<K,V> |
synchGet(Cache.CacheKey<K> k) |
private boolean |
synchPut(K key,
V value,
long keySizeBytes,
long valueSizeBytes) |
private boolean |
tooBigToCache(long totalSize)
Don't cache elems that are too big, even if there's space
|
private com.yahoo.statistics.Value elems
private com.yahoo.statistics.Value entrySizes
private Map<Cache.CacheKey<K>,Cache.CacheValue<K,V>> content
private com.yahoo.cache.SizeCalculator calc
private long maxSizeBytes
private long currentSizeBytes
private long timeToLiveMillis
private long maxEntrySizeBytes
public Cache(long maxSizeBytes, long timeToLiveMillis, long maxEntrySizeBytes, com.yahoo.statistics.Statistics manager)
maxSizeBytes
- the max size in bytes this cache is permitted to consume,
including Result objects and Query keystimeToLiveMillis
- a negative value means unlimited timemanager
- the current Statistics manager acquired by injectionprivate void initStats(com.yahoo.statistics.Statistics manager)
private Cache.CacheValue<K,V> synchGet(Cache.CacheKey<K> k)
public boolean put(K key, V value)
key
- the key of the valuevalue
- the value to addprivate boolean tooBigToCache(long totalSize)
private void makeRoomForBytes(long bytes)
public boolean containsKey(K k)
public boolean remove(K key)
public int size()
Copyright © 2017. All rights reserved.