1 9 package net.jforum.cache; 10 11 import java.io.Serializable ; 12 import java.util.ArrayList ; 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import net.jforum.util.preferences.SystemGlobals; 18 import net.sf.ehcache.Cache; 19 import net.sf.ehcache.CacheException; 20 import net.sf.ehcache.CacheManager; 21 import net.sf.ehcache.Element; 22 23 import org.apache.log4j.Logger; 24 35 public class EhCacheEngine implements CacheEngine { 36 37 private static final Logger log = Logger.getLogger(EhCacheEngine.class); 38 39 private CacheManager manager; 40 41 public void init() { 42 try { 43 manager = CacheManager.create(SystemGlobals.getValue("ehcache.cache.properties")); 44 } catch (CacheException ce) { 45 log.error("EhCache could not be initialized", ce); 46 throw new RuntimeException (ce); 47 } 48 } 49 50 public void stop() { 51 manager.shutdown(); 52 } 53 54 public void add(String key, Object value) { 55 if (log.isDebugEnabled()) { 56 log.debug("Caching " + value + " with key " + key); 57 } 58 add(DUMMY_FQN, key, value); 59 } 60 61 public void add(String fullyQualifiedName, String key, Object value) { 62 if (!manager.cacheExists(fullyQualifiedName)) { 63 try { 64 manager.addCache(fullyQualifiedName); 65 } catch (CacheException ce) { 66 log.error(ce, ce); 67 throw new RuntimeException (ce); 68 } 69 } 70 Cache cache = manager.getCache(fullyQualifiedName); 71 72 Element element = new Element(key, (Serializable )value); 73 cache.put(element); 74 } 75 76 public Object get(String fullyQualifiedName, String key) { 77 try { 78 if (!manager.cacheExists(fullyQualifiedName)) { 79 manager.addCache(fullyQualifiedName); 80 return null; 81 } 82 Cache cache = manager.getCache(fullyQualifiedName); 83 Element element = cache.get(key); 84 if (element != null) { 85 return element.getValue(); 86 } 87 88 return null; 89 } catch (CacheException ce) { 90 log.error("EhCache could not be shutdown", ce); 91 throw new RuntimeException (ce); 92 } 93 } 94 95 public Object get(String fullyQualifiedName) { 96 if (!manager.cacheExists(fullyQualifiedName)) { 97 try { 98 manager.addCache(fullyQualifiedName); 99 } catch (CacheException ce) { 100 log.error("EhCache could not be shutdown", ce); 101 throw new RuntimeException (ce); 102 } 103 } 104 Cache cache = manager.getCache(fullyQualifiedName); 105 return cache; 106 } 107 108 public Collection getValues(String fullyQualifiedName) { 109 try { 110 if (!manager.cacheExists(fullyQualifiedName)) { 111 manager.addCache(fullyQualifiedName); 112 return new ArrayList (); 113 } 114 Cache cache = manager.getCache(fullyQualifiedName); 115 List values = new ArrayList (cache.getSize()); 116 List keys = cache.getKeys(); 117 118 for (Iterator iter = keys.iterator(); iter.hasNext(); ) { 119 values.add(cache.get((Serializable )iter.next())); 120 } 121 122 return values; 123 } catch (CacheException ce) { 124 log.error("EhCache could not be shutdown", ce); 125 throw new RuntimeException (ce); 126 } 127 } 128 129 public void remove(String fullyQualifiedName, String key) { 130 Cache cache = manager.getCache(fullyQualifiedName); 131 132 if (cache != null) { 133 cache.remove(key); 134 } 135 } 136 137 public void remove(String fullyQualifiedName) { 138 if (manager.cacheExists(fullyQualifiedName)) { 139 manager.removeCache(fullyQualifiedName); 140 } 141 } 142 143 } 144 | Popular Tags |