ruthst00 commented on code in PR #6731:
URL: https://github.com/apache/jmeter/pull/6731#discussion_r4066453347
##########
src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/CacheManager.java:
##########
@@ -602,15 +602,18 @@ public void clear(){
private void clearCache() {
log.debug("Clear cache");
- // TODO: avoid re-creating the thread local every time, reset its
contents instead
- threadCache = new InheritableThreadLocal<Cache<String, CacheEntry>>(){
- @Override
- protected Cache<String, CacheEntry> initialValue() {
- return Caffeine.newBuilder()
- .maximumSize(getMaxSize())
- .build();
- }
- };
+ if (threadCache == null) {
+ threadCache = new InheritableThreadLocal<Cache<String,
CacheEntry>>(){
+ @Override
+ protected Cache<String, CacheEntry> initialValue() {
+ return Caffeine.newBuilder()
+ .maximumSize(getMaxSize())
+ .build();
+ }
+ };
+ } else {
+ threadCache.remove();
+ }
Review Comment:
### Recommended Fix
The correct approach to address the original TODO without breaking semantics
is to keep the single `InheritableThreadLocal` instance but invalidate the
cache contents rather than replacing the instance. For example:
```java
// Initialize once as a static-like field initializer or in the constructor
only
private transient InheritableThreadLocal<Cache<String, CacheEntry>>
threadCache =
new InheritableThreadLocal<Cache<String, CacheEntry>>() {
@Override
protected Cache<String, CacheEntry> initialValue() {
return Caffeine.newBuilder()
.maximumSize(getMaxSize())
.build();
}
};
private void clearCache() {
log.debug("Clear cache");
Cache<String, CacheEntry> cache = threadCache.get();
if (cache != null) {
cache.invalidateAll();
}
}
```
This keeps the `InheritableThreadLocal` instance stable (no memory leak from
repeated allocation), and `invalidateAll()` clears the Caffeine cache contents
for the current thread — which is the correct scope, since each thread owns its
own cache instance.
Generated with [Claude Sonnet](https://www.anthropic.com/claude/sonnet) via
[Cline](https://cline.bot/)
--
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]