public class CaffeineCache<K,V> extends SolrCacheBase implements SolrCache<K,V>, Accountable, com.github.benmanes.caffeine.cache.RemovalListener<K,V>
This cache supports either maximum size limit (the number of items) or maximum ram bytes limit, but not both. If both values are set then only maxRamMB limit is used and maximum size limit is ignored.
W-TinyLFU [2] is a near optimal policy that uses recency and frequency to determine which entry to evict in O(1) time. The estimated frequency is retained in a Count-Min Sketch and entries reside on LRU priority queues [3]. By capturing the historic frequency of an entry, the cache is able to outperform classic policies like LRU and LFU, as well as modern policies like ARC and LIRS. This policy performed particularly well in search workloads.
[1] https://github.com/ben-manes/caffeine [2] http://arxiv.org/pdf/1512.00727.pdf [3] http://highscalability.com/blog/2016/1/25/design-of-a-modern-cache.html
SolrCacheBase.AutoWarmCountRef
SolrCache.State
SolrInfoBean.Category, SolrInfoBean.Group
autowarm, regenerator
CLEANUP_THREAD_PARAM, EVICTIONS_PARAM, HIT_RATIO_PARAM, HITS_PARAM, INITIAL_SIZE_PARAM, INSERTS_PARAM, LOOKUPS_PARAM, MAX_IDLE_TIME_PARAM, MAX_RAM_MB_PARAM, MAX_SIZE_PARAM, RAM_BYTES_USED_PARAM, SHOW_ITEMS_PARAM, SIZE_PARAM
NULL_ACCOUNTABLE
Constructor and Description |
---|
CaffeineCache() |
Modifier and Type | Method and Description |
---|---|
void |
clear()
:TODO: copy from Map
|
void |
close()
Frees any non-memory resources
|
V |
computeIfAbsent(K key,
Function<? super K,? extends V> mappingFunction)
Get an existing element or atomically compute it if missing.
|
V |
get(K key)
:TODO: copy from Map
|
String |
getDescription()
Simple one or two line description
|
int |
getMaxRamMB()
Returns maximum size limit (in MB) if set and supported, -1 otherwise.
|
int |
getMaxSize()
Returns maximum size limit (number of items) if set and supported, -1 otherwise.
|
Set<String> |
getMetricNames()
Modifiable set of metric names that this component reports (default is null,
which means none).
|
String |
getName()
Simple common usage name, e.g.
|
SolrMetricsContext |
getSolrMetricsContext()
Implementing classes should override this method to provide the context obtained in
SolrMetricProducer.initializeMetrics(SolrMetricsContext, String) to ensure proper cleanup of metrics
at the end of the life-cycle of this component. |
Object |
init(Map args,
Object persistence,
CacheRegenerator regenerator)
The initialization routine.
|
void |
initializeMetrics(SolrMetricsContext parentContext,
String scope)
Initialize metrics specific to this producer.
|
void |
onRemoval(K key,
V value,
com.github.benmanes.caffeine.cache.RemovalCause cause) |
V |
put(K key,
V val)
:TODO: copy from Map
|
long |
ramBytesUsed() |
V |
remove(K key) |
void |
setMaxRamMB(int maxRamMB)
Set maximum size limit (in MB), or -1 for unlimited.
|
void |
setMaxSize(int maxSize)
Set maximum size limit (number of items), or -1 for unlimited.
|
int |
size()
:TODO: copy from Map
|
String |
toString() |
void |
warm(SolrIndexSearcher searcher,
SolrCache<K,V> old)
Warm this cache associated with
searcher using the old
cache object. |
calcHitRatio, getAutowarmDescription, getCategory, getState, getVersion, init, isAutowarmingOn, name, setState
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getCategory, getMetricRegistry, getMetricsSnapshot, registerMetricName
getUniqueMetricTag, initializeMetrics
getChildResources
public Object init(Map args, Object persistence, CacheRegenerator regenerator)
SolrCache
args
map.
The persistence object will exist across different lifetimes of similar caches. For example, all filter caches will share the same persistence object, sometimes at the same time (it must be thread-safe). If null is passed, then the cache implementation should create and return a new persistence object. If not null, the passed in object should be returned again.
Since it will exist across the lifetime of many caches, care should be taken to not reference any particular cache instance and prevent it from being garbage collected (no using inner classes unless they are static).
The persistence object is designed to be used as a way for statistics to accumulate across all instances of the same type of cache, however the object may be of any type desired by the cache implementation.
The CacheRegenerator
is what the cache uses during auto-warming to
regenerate an item in the new cache from an entry in the old cache.
public void onRemoval(K key, V value, com.github.benmanes.caffeine.cache.RemovalCause cause)
public long ramBytesUsed()
ramBytesUsed
in interface Accountable
public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
SolrCache
computeIfAbsent
in interface SolrCache<K,V>
key
- keymappingFunction
- function to compute the element. If the function returns a null
result the cache mapping will not be created. NOTE: this function
must NOT attempt to modify any mappings in the cache.public void clear()
SolrCache
public int size()
SolrCache
public void close() throws IOException
SolrCache
close
in interface AutoCloseable
close
in interface SolrMetricProducer
close
in interface SolrCache<K,V>
IOException
public int getMaxSize()
SolrCache
getMaxSize
in interface SolrCache<K,V>
public void setMaxSize(int maxSize)
SolrCache
setMaxSize
in interface SolrCache<K,V>
public int getMaxRamMB()
SolrCache
getMaxRamMB
in interface SolrCache<K,V>
public void setMaxRamMB(int maxRamMB)
SolrCache
setMaxRamMB
in interface SolrCache<K,V>
public void warm(SolrIndexSearcher searcher, SolrCache<K,V> old)
SolrCache
searcher
using the old
cache object. this
and old
will have the same concrete type.public String getName()
SolrInfoBean
getName
in interface SolrInfoBean
public String getDescription()
SolrInfoBean
getDescription
in interface SolrInfoBean
public SolrMetricsContext getSolrMetricsContext()
SolrMetricProducer
SolrMetricProducer.initializeMetrics(SolrMetricsContext, String)
to ensure proper cleanup of metrics
at the end of the life-cycle of this component.getSolrMetricsContext
in interface SolrMetricProducer
public Set<String> getMetricNames()
SolrInfoBean
SolrInfoBean.registerMetricName(String)
to capture what metrics names are reported from this component.
NOTE: this set has to allow iteration under modifications.
getMetricNames
in interface SolrInfoBean
public void initializeMetrics(SolrMetricsContext parentContext, String scope)
SolrMetricProducer
initializeMetrics
in interface SolrMetricProducer
parentContext
- parent metrics context. If this component has the same life-cycle as the parent
it can simply use the parent context, otherwise it should obtain a child context
using SolrMetricsContext.getChildContext(Object)
passing this
as the child.scope
- component scopeCopyright © 2000-2020 Apache Software Foundation. All Rights Reserved.