KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: SwarmCache.java,v 1.7 2005/04/21 07:57:19 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import net.sf.swarmcache.ObjectCache;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.Map JavaDoc;
8
9 /**
10  * @author Jason Carreira, Gavin King
11  */

12 public class SwarmCache implements Cache {
13     
14     private final ObjectCache cache;
15     private final String JavaDoc regionName;
16     
17     public SwarmCache(ObjectCache cache, String JavaDoc regionName) {
18         this.cache = cache;
19         this.regionName = regionName;
20     }
21
22     /**
23      * Get an item from the cache
24      * @param key
25      * @return the cached object or <tt>null</tt>
26      * @throws CacheException
27      */

28     public Object JavaDoc get(Object JavaDoc key) throws CacheException {
29         if (key instanceof Serializable JavaDoc) {
30             return cache.get( (Serializable JavaDoc) key );
31         }
32         else {
33             throw new CacheException("Keys must implement Serializable");
34         }
35     }
36
37     public Object JavaDoc read(Object JavaDoc key) throws CacheException {
38         return get(key);
39     }
40     
41     /**
42      * Add an item to the cache
43      * @param key
44      * @param value
45      * @throws CacheException
46      */

47     public void update(Object JavaDoc key, Object JavaDoc value) throws CacheException {
48         put(key, value);
49     }
50     
51     /**
52      * Add an item to the cache
53      * @param key
54      * @param value
55      * @throws CacheException
56      */

57     public void put(Object JavaDoc key, Object JavaDoc value) throws CacheException {
58         if (key instanceof Serializable JavaDoc) {
59             cache.put( (Serializable JavaDoc) key, value );
60         }
61         else {
62             throw new CacheException("Keys must implement Serializable");
63         }
64     }
65
66     /**
67      * Remove an item from the cache
68      */

69     public void remove(Object JavaDoc key) throws CacheException {
70         if (key instanceof Serializable JavaDoc) {
71             cache.clear( (Serializable JavaDoc) key );
72         }
73         else {
74             throw new CacheException("Keys must implement Serializable");
75         }
76     }
77
78     /**
79      * Clear the cache
80      */

81     public void clear() throws CacheException {
82         cache.clearAll();
83     }
84
85     /**
86      * Clean up
87      */

88     public void destroy() throws CacheException {
89         cache.clearAll();
90     }
91
92     /**
93      * If this is a clustered cache, lock the item
94      */

95     public void lock(Object JavaDoc key) throws CacheException {
96         throw new UnsupportedOperationException JavaDoc("SwarmCache does not support locking (use nonstrict-read-write)");
97     }
98
99     /**
100      * If this is a clustered cache, unlock the item
101      */

102     public void unlock(Object JavaDoc key) throws CacheException {
103         throw new UnsupportedOperationException JavaDoc("SwarmCache does not support locking (use nonstrict-read-write)");
104     }
105
106     /**
107      * Generate a (coarse) timestamp
108      */

109     public long nextTimestamp() {
110         return System.currentTimeMillis() / 100;
111     }
112
113     /**
114      * Get a reasonable "lock timeout"
115      */

116     public int getTimeout() {
117         return 600;
118     }
119
120     public String JavaDoc getRegionName() {
121         return regionName;
122     }
123
124     public long getSizeInMemory() {
125         return -1;
126     }
127
128     public long getElementCountInMemory() {
129         return -1;
130     }
131
132     public long getElementCountOnDisk() {
133         return -1;
134     }
135     
136     public Map JavaDoc toMap() {
137         throw new UnsupportedOperationException JavaDoc();
138     }
139
140     public String JavaDoc toString() {
141         return "SwarmCache(" + regionName + ')';
142     }
143
144 }
145
Popular Tags