01: package org.apache.el.util;
02:
03: import java.util.Map;
04: import java.util.WeakHashMap;
05: import java.util.concurrent.ConcurrentHashMap;
06:
07: public final class ConcurrentCache<K, V> {
08:
09: private final int size;
10:
11: private final Map<K, V> eden;
12:
13: private final Map<K, V> longterm;
14:
15: public ConcurrentCache(int size) {
16: this .size = size;
17: this .eden = new ConcurrentHashMap<K, V>(size);
18: this .longterm = new WeakHashMap<K, V>(size);
19: }
20:
21: public V get(K k) {
22: V v = this .eden.get(k);
23: if (v == null) {
24: v = this .longterm.get(k);
25: if (v != null) {
26: this .eden.put(k, v);
27: }
28: }
29: return v;
30: }
31:
32: public void put(K k, V v) {
33: if (this.eden.size() >= size) {
34: this.longterm.putAll(this.eden);
35: this.eden.clear();
36: }
37: this.eden.put(k, v);
38: }
39: }
|