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 ; 10 import java.util.Set ; 11 import java.util.Collection ; 12 13 34 public class OSCacheImplementation implements CacheImplementationInterface { 35 private AbstractConcurrentReadCache cacheImpl; 36 private static final String classname = "com.opensymphony.oscache.base.algorithm.LRUCache"; 37 private static final String persistanceclass = "com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener"; 38 private static final Logger log = Logging.getLoggerInstance(OSCacheImplementation.class); 39 40 public OSCacheImplementation() { 41 } 42 43 47 public void config(Map config) { 48 try { 49 Class 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 e) { 66 log.error("Exception while initializing cache: " + e); 67 } 68 } 69 70 73 public void setMaxSize(int size) { 74 cacheImpl.setMaxEntries(size); 75 } 76 77 80 public int maxSize() { 81 return cacheImpl.getMaxEntries(); 82 } 83 84 87 public int getCount(Object key) { 88 return -1; } 90 91 94 public void clear() { 95 cacheImpl.clear(); 96 } 97 98 101 public boolean containsKey(Object key) { 102 if (key instanceof String ) { 103 return cacheImpl.containsKey(key); 104 } else { 105 return cacheImpl.containsKey(computeKey(key)); 106 } 107 } 108 109 112 public boolean containsValue(Object value) { 113 return cacheImpl.containsValue(value); 114 } 115 116 119 public Set entrySet() { 120 return cacheImpl.entrySet(); 121 } 122 123 126 public boolean equals(Object o) { 127 return cacheImpl.equals(o); 128 } 129 130 133 public Object get(Object key) { 134 if (key instanceof String ) { 135 return cacheImpl.get(key); 136 } else { 137 return cacheImpl.get(computeKey(key)); 138 } 139 } 140 141 144 public int hashCode() { 145 return cacheImpl.hashCode(); 146 } 147 148 151 public boolean isEmpty() { 152 return cacheImpl.isEmpty(); 153 } 154 155 158 public Set keySet() { 159 return cacheImpl.keySet(); 160 } 161 162 165 public Object put(Object key, Object value) { 166 if (key instanceof String ) { 167 return cacheImpl.put(key, value); 168 } else { 169 return cacheImpl.put(computeKey(key), value); 170 } 171 } 172 173 176 public void putAll(Map t) { 177 cacheImpl.putAll(t); 178 } 179 180 183 public Object remove(Object key) { 184 if (key instanceof String ) { 185 return cacheImpl.remove(key); 186 } else { 187 return cacheImpl.remove(computeKey(key)); 188 } 189 } 190 191 194 public int size() { 195 return cacheImpl.size(); 196 } 197 198 201 public Collection values() { 202 return cacheImpl.values(); 203 } 204 205 213 private static String computeKey(Object o) { 214 return o.getClass().getName() + "_" + o.hashCode(); 215 } 216 217 220 public int getByteSize() { 221 throw new UnsupportedOperationException ("Size is not available for OSCache"); 222 } 223 224 227 public int getByteSize(SizeOf sizeof) { 228 throw new UnsupportedOperationException ("Size is not available for OSCache"); 229 } 230 } 231 | Popular Tags |