boicehuang commented on code in PR #13306:
URL: https://github.com/apache/lucene/pull/13306#discussion_r1579596679
##########
lucene/core/src/java/org/apache/lucene/search/LRUQueryCache.java:
##########
@@ -265,7 +269,6 @@ boolean requiresEviction() {
}
CacheAndCount get(Query key, IndexReader.CacheHelper cacheHelper) {
- assert lock.isHeldByCurrentThread();
assert key instanceof BoostQuery == false;
assert key instanceof ConstantScoreQuery == false;
final IndexReader.CacheKey readerKey = cacheHelper.getKey();
Review Comment:
but I don't think it is the best to use `Collections.synchronizedMap`, we
can use `ConcurrentLinkedDeque` to achieve LRU expiration better. Here is an
example.
```
public class LRUCache<K, V> {
private final int capacity;
private ConcurrentMap<K, V> cache;
private ConcurrentLinkedDeque<K> keys;
public LRUCache(int capacity) {
this.capacity = capacity;
this.cache = new ConcurrentHashMap<>(capacity);
this.keys = new ConcurrentLinkedDeque<>();
}
public V get(K key) {
V value = cache.get(key);
if (value != null) {
keys.remove(key); // remove first occurrence of key
keys.addFirst(key); // add key to the head
}
return value;
}
public void put(K key, V value) {
if (cache.containsKey(key)) {
keys.remove(key); // remove first occurrence of key
}
while (keys.size() >= capacity) {
K lastKey = keys.removeLast();
if (lastKey != null) {
cache.remove(lastKey);
}
}
cache.put(key, value);
keys.addFirst(key);
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]