LRUQueryCache
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
Properties
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.
Returns nested resources of this class. The result should be a point-in-time snapshot (to avoid race conditions).
Functions
Remove all cache entries for the given core cache key.
Remove all cache entries for the given query.
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.
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.
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.
Return the memory usage of this object in bytes. Negative values are illegal.
Whether evictions are required.