Skip to content

Commit

Permalink
AF-2452 : Making LRUCache configurable to avoid large memory retention (
Browse files Browse the repository at this point in the history
kiegroup#1032)

* Added parameterized constructor to configure MAX_ENTRIES in LRUCache
  • Loading branch information
akumar074 authored Sep 3, 2020
1 parent 27dbcb7 commit 1582452
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ public abstract class LRUCache<Path, V> implements Cache<Path, V> {
private Map<Path, V> cache;

public LRUCache() {
cache = new LinkedHashMap<Path, V>(MAX_ENTRIES + 1,
0.75f,
true) {
public boolean removeEldestEntry(Map.Entry eldest) {
return size() > MAX_ENTRIES;
}
};
cache = (Map) Collections.synchronizedMap(cache);
setCache(MAX_ENTRIES);
}

public LRUCache(final int maxEntries) {
setCache(maxEntries);
}

@Override
Expand Down Expand Up @@ -75,4 +72,15 @@ public void invalidateCache(final Path path) {
public Set<Path> getKeys() {
return cache.keySet();
}

private void setCache(final int maxEntries) {
cache = new LinkedHashMap<Path, V>(maxEntries + 1,
0.75f,
true) {
public boolean removeEldestEntry(Map.Entry eldest) {
return size() > maxEntries;
}
};
cache = (Map) Collections.synchronizedMap(cache);
}
}

0 comments on commit 1582452

Please sign in to comment.