Class CaffeineCache<K,V>

java.lang.Object
org.apache.solr.search.SolrCacheBase
org.apache.solr.search.CaffeineCache<K,V>
All Implemented Interfaces:
com.github.benmanes.caffeine.cache.RemovalListener<K,V>, AutoCloseable, org.apache.lucene.util.Accountable, SolrInfoBean, SolrMetricProducer, SolrCache<K,V>

public class CaffeineCache<K,V> extends SolrCacheBase implements SolrCache<K,V>, org.apache.lucene.util.Accountable, com.github.benmanes.caffeine.cache.RemovalListener<K,V>
A SolrCache backed by the Caffeine caching library [1]. By default it uses the Window TinyLFU (W-TinyLFU) eviction policy.

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

  • Constructor Details

    • CaffeineCache

      public CaffeineCache()
  • Method Details

    • init

      public Object init(Map<String,String> args, Object persistence, CacheRegenerator regenerator)
      Description copied from interface: SolrCache
      The initialization routine. Instance specific arguments are passed in the 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.

      Specified by:
      init in interface SolrCache<K,V>
    • onRemoval

      public void onRemoval(K key, V value, com.github.benmanes.caffeine.cache.RemovalCause cause)
      Specified by:
      onRemoval in interface com.github.benmanes.caffeine.cache.RemovalListener<K,V>
    • ramBytesUsed

      public long ramBytesUsed()
      Specified by:
      ramBytesUsed in interface org.apache.lucene.util.Accountable
    • get

      public V get(K key)
      Description copied from interface: SolrCache
      :TODO: copy from Map
      Specified by:
      get in interface SolrCache<K,V>
    • computeIfAbsent

      public V computeIfAbsent(K key, IOFunction<? super K,? extends V> mappingFunction) throws IOException
      Description copied from interface: SolrCache
      Get an existing element or atomically compute it if missing.
      Specified by:
      computeIfAbsent in interface SolrCache<K,V>
      Parameters:
      key - key
      mappingFunction - 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.
      Returns:
      existing or newly computed value, null if there was no existing value and it was not possible to compute a new value (in which case the new mapping won't be created).
      Throws:
      IOException - if and only if mappingFunction threw an IOException. A cache mapping will not be created in this case
    • put

      public V put(K key, V val)
      Description copied from interface: SolrCache
      :TODO: copy from Map
      Specified by:
      put in interface SolrCache<K,V>
    • remove

      public V remove(K key)
      Specified by:
      remove in interface SolrCache<K,V>
    • clear

      public void clear()
      Description copied from interface: SolrCache
      :TODO: copy from Map
      Specified by:
      clear in interface SolrCache<K,V>
    • size

      public int size()
      Description copied from interface: SolrCache
      :TODO: copy from Map
      Specified by:
      size in interface SolrCache<K,V>
    • close

      public void close() throws IOException
      Description copied from interface: SolrCache
      Frees any non-memory resources
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface SolrCache<K,V>
      Specified by:
      close in interface SolrMetricProducer
      Throws:
      IOException
    • getMaxSize

      public int getMaxSize()
      Description copied from interface: SolrCache
      Returns maximum size limit (number of items) if set and supported, -1 otherwise.
      Specified by:
      getMaxSize in interface SolrCache<K,V>
    • setMaxSize

      public void setMaxSize(int maxSize)
      Description copied from interface: SolrCache
      Set maximum size limit (number of items), or -1 for unlimited. Note: this has effect only on implementations that support it, it's a no-op otherwise
      Specified by:
      setMaxSize in interface SolrCache<K,V>
    • getMaxRamMB

      public int getMaxRamMB()
      Description copied from interface: SolrCache
      Returns maximum size limit (in MB) if set and supported, -1 otherwise.
      Specified by:
      getMaxRamMB in interface SolrCache<K,V>
    • setMaxRamMB

      public void setMaxRamMB(int maxRamMB)
      Description copied from interface: SolrCache
      Set maximum size limit (in MB), or -1 for unlimited. Note: this has effect only on implementations that support it, it's a no-op otherwise.
      Specified by:
      setMaxRamMB in interface SolrCache<K,V>
    • adjustMetrics

      protected void adjustMetrics(long hitsAdjust, long insertsAdjust, long lookupsAdjust)
    • warm

      public void warm(SolrIndexSearcher searcher, SolrCache<K,V> old)
      Description copied from interface: SolrCache
      Warm this cache associated with searcher using the old cache object. this and old will have the same concrete type.
      Specified by:
      warm in interface SolrCache<K,V>
    • isRecursionSupported

      public boolean isRecursionSupported()
      Description copied from interface: SolrCache
      Check if this SolrCache supports recursive calls to SolrCache.computeIfAbsent(Object, IOFunction). Caches backed by ConcurrentHashMap.computeIfAbsent(Object, Function) explicitly do not support that, but other caches might.
      Specified by:
      isRecursionSupported in interface SolrCache<K,V>
      Returns:
      whether this cache allows recursive computations
    • getName

      public String getName()
      Description copied from interface: SolrInfoBean
      Simple common usage name, e.g. BasicQueryHandler, or fully qualified class name.
      Specified by:
      getName in interface SolrInfoBean
    • getDescription

      public String getDescription()
      Description copied from interface: SolrInfoBean
      Simple one or two line description
      Specified by:
      getDescription in interface SolrInfoBean
    • getSolrMetricsContext

      public SolrMetricsContext getSolrMetricsContext()
      Description copied from interface: SolrMetricProducer
      Implementations should return the context used in SolrMetricProducer.initializeMetrics(SolrMetricsContext, Attributes) to ensure proper cleanup of metrics at the end of the life-cycle of this component. This should be the child context if one was created, or null if the parent context was used.
      Specified by:
      getSolrMetricsContext in interface SolrMetricProducer
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • initializeMetrics

      public void initializeMetrics(SolrMetricsContext parentContext, io.opentelemetry.api.common.Attributes attributes)
      Description copied from interface: SolrMetricProducer
      Implementation should initialize all metrics to a SolrMetricsContext Registry/MeterProvider with Attributes as the common set of attributes that will be attached to every metric that is initialized for that class/component
      Specified by:
      initializeMetrics in interface SolrMetricProducer
      Parameters:
      parentContext - The registry that the component will initialize metrics to
      attributes - Base set of attributes that will be bound to all metrics for that component
    • initializeMetrics

      public void initializeMetrics(SolrMetricsContext solrMetricsContext, io.opentelemetry.api.common.Attributes attributes, String metricName)