SURYAS1306 opened a new pull request, #16053:
URL: https://github.com/apache/dubbo/pull/16053

   ## What is the purpose of the change?
   
   This PR fixes an issue in `LRUCache` and `LRU2Cache` where reducing 
`maxCapacity` at runtime did **not** evict existing entries.
   
   Currently, calling `setMaxCapacity()` only updates the capacity value, but 
already-stored entries remain in the cache even if the new capacity is smaller. 
This breaks expected LRU behavior and can lead to caches holding more entries 
than allowed.
   
   This change ensures both caches are immediately trimmed when the maximum 
capacity is reduced.
   
   Fixes #16038
   
   ---
   
   ## What is changed?
   
   ### 1. LRUCache: enforce trimming on capacity change
   
   - Added a `trimToSize()` method.
   - Updated `setMaxCapacity()` to:
     - acquire the lock
     - update `maxCapacity`
     - evict eldest entries until `size() <= maxCapacity`.
   
   ### 2. LRU2Cache: trim both main cache and history cache
   
   - Updated `setMaxCapacity()` to:
     - acquire the lock
     - update `maxCapacity`
     - update `preCache` capacity
     - trim the main cache immediately.
   - Updated `PreCache#setMaxCapacity()` to also trim existing entries.
   
   This guarantees that both the main cache and the history cache always 
respect the latest capacity.
   
   ---
   
   ## Why is this needed?
   
   LRU and LRU-2 caches are expected to strictly respect their configured 
capacity.
   
   Without this fix:
   
   - Reducing `maxCapacity` does not remove old entries.
   - Caches can permanently exceed their configured limits.
   - Eviction guarantees are broken.
   - Memory usage can grow beyond expected bounds.
   
   This change makes cache behavior correct, predictable, and safe when 
capacity is adjusted dynamically.
   
   ---
   
   ## Verifying this change
   
   - Existing tests pass locally.
   - No public API changes.
   - Build verified with:
   
   ```bash
   mvn -pl dubbo-common -am test
   ```
   
   ## Checklist
   
   - [x] Logic change explained clearly  
   - [x] Both LRUCache and LRU2Cache handled  
   - [x] Capacity reduction correctly trims existing entries  
   - [x] Thread-safety preserved  
   - [x] All tests passing locally  
   
   
   
   
   
   


-- 
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]

Reply via email to