KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > HashtableCache


1 //$Id: HashtableCache.java,v 1.8 2005/04/21 07:57:19 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import java.util.Collections JavaDoc;
5 import java.util.Hashtable JavaDoc;
6
7 import java.util.Map JavaDoc;
8
9 /**
10  * A lightweight implementation of the <tt>Cache</tt> interface
11  * @author Gavin King
12  */

13 public class HashtableCache implements Cache {
14     
15     private final Map JavaDoc hashtable = new Hashtable JavaDoc();
16     private final String JavaDoc regionName;
17     
18     public HashtableCache(String JavaDoc regionName) {
19         this.regionName = regionName;
20     }
21
22     public String JavaDoc getRegionName() {
23         return regionName;
24     }
25
26     public Object JavaDoc read(Object JavaDoc key) throws CacheException {
27         return hashtable.get(key);
28     }
29
30     public Object JavaDoc get(Object JavaDoc key) throws CacheException {
31         return hashtable.get(key);
32     }
33
34     public void update(Object JavaDoc key, Object JavaDoc value) throws CacheException {
35         put(key, value);
36     }
37     
38     public void put(Object JavaDoc key, Object JavaDoc value) throws CacheException {
39         hashtable.put(key, value);
40     }
41
42     public void remove(Object JavaDoc key) throws CacheException {
43         hashtable.remove(key);
44     }
45
46     public void clear() throws CacheException {
47         hashtable.clear();
48     }
49
50     public void destroy() throws CacheException {
51
52     }
53
54     public void lock(Object JavaDoc key) throws CacheException {
55         // local cache, so we use synchronization
56
}
57
58     public void unlock(Object JavaDoc key) throws CacheException {
59         // local cache, so we use synchronization
60
}
61
62     public long nextTimestamp() {
63         return Timestamper.next();
64     }
65
66     public int getTimeout() {
67         return Timestamper.ONE_MS * 60000; //ie. 60 seconds
68
}
69
70     public long getSizeInMemory() {
71         return -1;
72     }
73
74     public long getElementCountInMemory() {
75         return hashtable.size();
76     }
77
78     public long getElementCountOnDisk() {
79         return 0;
80     }
81     
82     public Map JavaDoc toMap() {
83         return Collections.unmodifiableMap(hashtable);
84     }
85
86     public String JavaDoc toString() {
87         return "HashtableCache(" + regionName + ')';
88     }
89
90 }
91
Popular Tags