org.apache.el.util.ConcurrentCache Maven / Gradle / Ivy
package org.apache.el.util;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
public final class ConcurrentCache {
private final int size;
private final Map eden;
private final Map longterm;
public ConcurrentCache(int size) {
this.size = size;
this.eden = new ConcurrentHashMap(size);
this.longterm = new WeakHashMap(size);
}
public V get(K k) {
V v = this.eden.get(k);
if (v == null) {
v = this.longterm.get(k);
if (v != null) {
this.eden.put(k, v);
}
}
return v;
}
public void put(K k, V v) {
if (this.eden.size() >= size) {
this.longterm.putAll(this.eden);
this.eden.clear();
}
this.eden.put(k, v);
}
}