KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > cache > oscache > OSCacheImplementation


1 package org.mmbase.cache.oscache;
2 import org.mmbase.cache.CacheImplementationInterface;
3 import org.mmbase.util.SizeOf;
4 import org.mmbase.util.logging.Logger;
5 import org.mmbase.util.logging.Logging;
6 import com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache;
7 import com.opensymphony.oscache.base.persistence.PersistenceListener;
8 import com.opensymphony.oscache.base.Config;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11 import java.util.Collection JavaDoc;
12
13 /**
14  * Implementation of the MMBase 'CacheImplementationInterface' interface, which
15  * delegates all calls to an OSCache implementation.
16  * This implementation accepts the following configuration properties:
17  * <ul>
18  * <li>path</li>
19  * </ul>
20  * To use this implementation, you can use a construction like this in your 'caches.xml'
21  * configuration file:
22  * <pre>
23  * &lt;cache name="NodeListCache"&gt;
24  * &lt;status&gt;active&lt;/status&gt;
25  * &lt;size&gt;3&lt;/size&gt;
26  * &lt;implementation&gt;
27  * &lt;class&gt;org.mmbase.cache.oscache.OSCacheImplementation&lt;/class&gt;
28  * &lt;param name="path"&gt;/tmp&lt;/param&gt;
29  * &lt;/implementation&gt;
30  * &lt;/cache&gt;
31  * </pre>
32  * @author Johannes Verelst &lt;johannes.verelst@eo.nl&gt;
33  */

34 public class OSCacheImplementation implements CacheImplementationInterface {
35     private AbstractConcurrentReadCache cacheImpl;
36     private static final String JavaDoc classname = "com.opensymphony.oscache.base.algorithm.LRUCache";
37     private static final String JavaDoc persistanceclass = "com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener";
38     private static final Logger log = Logging.getLoggerInstance(OSCacheImplementation.class);
39
40     public OSCacheImplementation() {
41     }
42
43     /**
44      * This method is called by MMBase to configure the cache using the given
45      * map of configuration parameters.
46      */

47     public void config(Map JavaDoc config) {
48        try {
49            Class JavaDoc c = Class.forName(classname);
50            if (c != null) {
51                cacheImpl = (AbstractConcurrentReadCache)c.newInstance();
52            }
53
54            c = Class.forName(persistanceclass);
55            if (c != null) {
56                PersistenceListener pl = (PersistenceListener)c.newInstance();
57                Config osconfig = new Config();
58                osconfig.set("cache.path", config.get("path"));
59                pl.configure(osconfig);
60                cacheImpl.setPersistenceListener(pl);
61                cacheImpl.setMemoryCaching(true);
62                cacheImpl.setUnlimitedDiskCache(true);
63                cacheImpl.setOverflowPersistence(true);
64             }
65         } catch (Exception JavaDoc e) {
66             log.error("Exception while initializing cache: " + e);
67         }
68     }
69
70     /**
71      * Wrapper around the setMaxEntries() method of the cache implementation.
72      */

73     public void setMaxSize(int size) {
74         cacheImpl.setMaxEntries(size);
75     }
76
77     /**
78      * Wrapper around the getMaxEntries() method of the cache implementation.
79      */

80     public int maxSize() {
81         return cacheImpl.getMaxEntries();
82     }
83
84     /**
85      * Wrapper around the getCount() method of the cache implementation. (not implemented)
86      */

87     public int getCount(Object JavaDoc key) {
88         return -1; //not implemented
89
}
90
91     /**
92      * Wrapper around the clear() method of the cache implementation.
93      */

94     public void clear() {
95         cacheImpl.clear();
96     }
97
98     /**
99      * Wrapper around the containsKey() method of the cache implementation.
100      */

101     public boolean containsKey(Object JavaDoc key) {
102         if (key instanceof String JavaDoc) {
103             return cacheImpl.containsKey(key);
104         } else {
105             return cacheImpl.containsKey(computeKey(key));
106         }
107     }
108
109     /**
110      * Wrapper around the containsValue() method of the cache implementation.
111      */

112     public boolean containsValue(Object JavaDoc value) {
113         return cacheImpl.containsValue(value);
114     }
115
116     /**
117      * Wrapper around the entrySet() method of the cache implementation.
118      */

119     public Set JavaDoc entrySet() {
120         return cacheImpl.entrySet();
121     }
122
123     /**
124      * Wrapper around the equals() method of the cache implementation.
125      */

126     public boolean equals(Object JavaDoc o) {
127         return cacheImpl.equals(o);
128     }
129
130     /**
131      * Wrapper around the get() method of the cache implementation.
132      */

133     public Object JavaDoc get(Object JavaDoc key) {
134         if (key instanceof String JavaDoc) {
135             return cacheImpl.get(key);
136         } else {
137             return cacheImpl.get(computeKey(key));
138         }
139     }
140
141     /**
142      * Wrapper around the hashCode() method of the cache implementation.
143      */

144     public int hashCode() {
145         return cacheImpl.hashCode();
146     }
147     
148     /**
149      * Wrapper around the isEmpty() method of the cache implementation.
150      */

151     public boolean isEmpty() {
152         return cacheImpl.isEmpty();
153     }
154
155     /**
156      * Wrapper around the keySet() method of the cache implementation.
157      */

158     public Set JavaDoc keySet() {
159         return cacheImpl.keySet();
160     }
161
162     /**
163      * Wrapper around the put() method of the cache implementation.
164      */

165     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
166         if (key instanceof String JavaDoc) {
167             return cacheImpl.put(key, value);
168         } else {
169             return cacheImpl.put(computeKey(key), value);
170         }
171     }
172
173     /**
174      * Wrapper around the putAll() method of the cache implementation.
175      */

176     public void putAll(Map JavaDoc t) {
177         cacheImpl.putAll(t);
178     }
179
180     /**
181      * Wrapper around the remove() method of the cache implementation.
182      */

183     public Object JavaDoc remove(Object JavaDoc key) {
184         if (key instanceof String JavaDoc) {
185             return cacheImpl.remove(key);
186         } else {
187             return cacheImpl.remove(computeKey(key));
188         }
189     }
190
191     /**
192      * Wrapper around the size() method of the cache implementation.
193      */

194     public int size() {
195         return cacheImpl.size();
196     }
197
198     /**
199      * Wrapper around the values() method of the cache implementation.
200      */

201     public Collection JavaDoc values() {
202         return cacheImpl.values();
203     }
204     
205     /**
206      * Calculate an unique string key for an object that is not a 'String'.
207      * The 'toString()' will not suffice, partly because it doesn't guarantee
208      * a unique value, and also because the toString() method may generate
209      * keys that are too long.
210      * This method will concatenate the classname and the hashcode of the object,
211      * this combination should be unique.
212      */

213     private static String JavaDoc computeKey(Object JavaDoc o) {
214         return o.getClass().getName() + "_" + o.hashCode();
215     }
216
217     /**
218      * @see org.mmbase.util.SizeMeasurable#getByteSize()
219      */

220     public int getByteSize() {
221         throw new UnsupportedOperationException JavaDoc("Size is not available for OSCache");
222     }
223
224     /**
225      * @see org.mmbase.util.SizeMeasurable#getByteSize(org.mmbase.util.SizeOf)
226      */

227     public int getByteSize(SizeOf sizeof) {
228         throw new UnsupportedOperationException JavaDoc("Size is not available for OSCache");
229     }
230 }
231
Popular Tags