LRUQueryCache

open class LRUQueryCache(maxSize: Int, maxRamBytesUsed: Long, leavesToCache: (LeafReaderContext) -> Boolean = minSegmentSizePredicate(10000), skipCacheFactor: Float = 10.0f) : QueryCache, Accountable

A QueryCache that evicts queries using a LRU (least-recently-used) eviction policy in order to remain under a given maximum size and number of bytes used.

This class is thread-safe.

Note that query eviction runs in linear time with the total number of segments that have cache entries so this cache works best with caching policies that only cache on "large" segments, and it is advised to not share this cache across too many indices.

A default query cache and policy instance is used in IndexSearcher. If you want to replace those defaults it is typically done like this:

final int maxNumberOfCachedQueries = 256;
final long maxRamBytesUsed = 50 * 1024L * 1024L; // 50MB
// these cache and policy instances can be shared across several queries and readers
// it is fine to eg. store them into static variables
final QueryCache queryCache = new LRUQueryCache(maxNumberOfCachedQueries, maxRamBytesUsed);
final QueryCachingPolicy defaultCachingPolicy = new UsageTrackingQueryCachingPolicy();
indexSearcher.setQueryCache(queryCache);
indexSearcher.setQueryCachingPolicy(defaultCachingPolicy);
*

This cache exposes some global statistics (.getHitCount, .getMissCount, .getCacheSize, .getCacheCount, .getEvictionCount). In case you would like to have more fine-grained statistics, such as per-index or per-query-class statistics, it is possible to override various callbacks: .onHit, .onMiss, .onQueryCache, .onQueryEviction, .onDocIdSetCache, .onDocIdSetEviction and .onClear. It is better to not perform heavy computations in these methods though since they are called synchronously and under a lock.

See also

Constructors

Link copied to clipboard
constructor(maxSize: Int, maxRamBytesUsed: Long, leavesToCache: (LeafReaderContext) -> Boolean = minSegmentSizePredicate(10000), skipCacheFactor: Float = 10.0f)

Types

Link copied to clipboard
class CacheAndCount(cache: DocIdSet, count: Int) : Accountable

Cache of doc ids with a count.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Return the total number of cache entries that have been generated and put in the cache. It is highly desirable to have a .getHitCount that is much higher than the .getCacheCount as the opposite would indicate that the query cache makes efforts in order to cache queries but then they do not get reused.

Link copied to clipboard

Return the total number of DocIdSets which are currently stored in the cache.

Link copied to clipboard

Returns nested resources of this class. The result should be a point-in-time snapshot (to avoid race conditions).

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
suspend fun assertConsistent()
Link copied to clipboard
Link copied to clipboard
suspend fun clear()

Clear the content of this cache.

Link copied to clipboard
suspend fun clearCoreCacheKey(coreKey: Any)

Remove all cache entries for the given core cache key.

Link copied to clipboard
suspend fun clearQuery(query: Query)

Remove all cache entries for the given query.

Link copied to clipboard
open override fun doCache(weight: Weight, policy: QueryCachingPolicy): Weight

Return a wrapper around the provided weight that will cache matching docs per-segment accordingly to the given policy. NOTE: The returned weight will only be equivalent if scores are not needed.

Link copied to clipboard
Link copied to clipboard

Over the .getTotalCount number of times that a query has been looked up, return how many times a cached DocIdSet has been found and returned.

Link copied to clipboard

Over the .getTotalCount number of times that a query has been looked up, return how many times this query was not contained in the cache.

Link copied to clipboard
open override fun ramBytesUsed(): Long

Return the memory usage of this object in bytes. Negative values are illegal.

Link copied to clipboard

Whether evictions are required.