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 with better 
performance. Here is an example.
   ```
   public class LRUCache<K, V> {
       private final int capacity;
       private ConcurrentMap<K, V> cache;
       private ConcurrentLinkedDeque<K> keys;
       private final ReentrantReadWriteLock lock;
   
       public LRUCache(int capacity) {
           this.capacity = capacity;
           this.cache = new ConcurrentHashMap<>(capacity);
           this.keys = new ConcurrentLinkedDeque<>();
           this.lock = new ReentrantReadWriteLock();
       }
   
       public V get(K key) {
           lock.readLock().lock();
           try {
               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;
           } finally {
               lock.readLock().unlock();
           }
       }
   
       public void put(K key, V value) {
           lock.writeLock().lock();
           try {
               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);
           } finally {
               lock.writeLock().unlock();
           }
       }
   }
   ```



-- 
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: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to