1 55 package org.hibernate.cache; 56 57 import java.io.IOException ; 58 import java.io.Serializable ; 59 import java.util.HashMap ; 60 import java.util.Iterator ; 61 import java.util.Map ; 62 63 import net.sf.ehcache.CacheManager; 64 import net.sf.ehcache.Element; 65 66 import org.apache.commons.logging.Log; 67 import org.apache.commons.logging.LogFactory; 68 69 81 public class EhCache implements Cache { 82 private static final Log log = LogFactory.getLog(EhCache.class); 83 84 private net.sf.ehcache.Cache cache; 85 86 91 public EhCache(net.sf.ehcache.Cache cache) { 92 this.cache = cache; 93 } 94 95 101 public Object get(Object key) throws CacheException { 102 try { 103 if ( log.isDebugEnabled() ) { 104 log.debug("key: " + key); 105 } 106 if (key == null) { 107 return null; 108 } 109 else { 110 Element element = cache.get( (Serializable ) key ); 111 if (element == null) { 112 if ( log.isDebugEnabled() ) { 113 log.debug("Element for " + key + " is null"); 114 } 115 return null; 116 } 117 else { 118 return element.getValue(); 119 } 120 } 121 } 122 catch (net.sf.ehcache.CacheException e) { 123 throw new CacheException(e); 124 } 125 } 126 127 public Object read(Object key) throws CacheException { 128 return get(key); 129 } 130 131 132 139 public void update(Object key, Object value) throws CacheException { 140 put(key, value); 141 } 142 143 150 public void put(Object key, Object value) throws CacheException { 151 try { 152 Element element = new Element( (Serializable ) key, (Serializable ) value ); 153 cache.put(element); 154 } 155 catch (IllegalArgumentException e) { 156 throw new CacheException(e); 157 } 158 catch (IllegalStateException e) { 159 throw new CacheException(e); 160 } 161 162 } 163 164 171 public void remove(Object key) throws CacheException { 172 try { 173 cache.remove( (Serializable ) key ); 174 } 175 catch (ClassCastException e) { 176 throw new CacheException(e); 177 } 178 catch (IllegalStateException e) { 179 throw new CacheException(e); 180 } 181 } 182 183 188 public void clear() throws CacheException { 189 try { 190 cache.removeAll(); 191 } 192 catch (IllegalStateException e) { 193 throw new CacheException(e); 194 } 195 catch (IOException e) { 196 throw new CacheException(e); 197 } 198 } 199 200 204 public void destroy() throws CacheException { 205 try { 206 CacheManager.getInstance().removeCache( cache.getName() ); 207 } 208 catch (IllegalStateException e) { 209 throw new CacheException(e); 210 } 211 catch (net.sf.ehcache.CacheException e) { 212 throw new CacheException(e); 213 } 214 } 215 216 221 public void lock(Object key) throws CacheException { 222 } 223 224 229 public void unlock(Object key) throws CacheException { 230 } 231 232 235 public long nextTimestamp() { 236 return Timestamper.next(); 237 } 238 239 242 public int getTimeout() { 243 return Timestamper.ONE_MS * 60000; 245 } 246 247 public String getRegionName() { 248 return cache.getName(); 249 } 250 251 public long getSizeInMemory() { 252 try { 253 return cache.calculateInMemorySize(); 254 } 255 catch(Throwable t) { 256 return -1; 257 } 258 } 259 260 public long getElementCountInMemory() { 261 try { 262 return cache.getSize(); 263 } 264 catch (net.sf.ehcache.CacheException ce) { 265 throw new CacheException(ce); 266 } 267 } 268 269 public long getElementCountOnDisk() { 270 return cache.getDiskStoreSize(); 271 } 272 273 public Map toMap() { 274 try { 275 Map result = new HashMap (); 276 Iterator iter = cache.getKeys().iterator(); 277 while ( iter.hasNext() ) { 278 Object key = iter.next(); 279 result.put( key, cache.get( (Serializable ) key ).getValue() ); 280 } 281 return result; 282 } 283 catch (Exception e) { 284 throw new CacheException(e); 285 } 286 } 287 288 public String toString() { 289 return "EHCache(" + getRegionName() + ')'; 290 } 291 292 } | Popular Tags |