KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > el > util > ConcurrentCache


1 package org.apache.el.util;
2
3 import java.util.Map JavaDoc;
4 import java.util.WeakHashMap JavaDoc;
5 import java.util.concurrent.ConcurrentHashMap JavaDoc;
6
7 public final class ConcurrentCache<K,V> {
8
9     private final int size;
10     
11     private final Map JavaDoc<K,V> eden;
12     
13     private final Map JavaDoc<K,V> longterm;
14     
15     public ConcurrentCache(int size) {
16         this.size = size;
17         this.eden = new ConcurrentHashMap JavaDoc<K,V>(size);
18         this.longterm = new WeakHashMap JavaDoc<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 }
40
Popular Tags