1 10 11 package net.sf.cache4j.perfomance.cache; 12 13 import net.sf.ehcache.Cache; 14 import net.sf.ehcache.CacheManager; 15 import net.sf.ehcache.Element; 16 17 import java.io.Serializable ; 18 import java.util.Properties ; 19 20 import net.sf.cache4j.perfomance.ICache; 21 import org.apache.log4j.PropertyConfigurator; 22 23 29 30 public class ehcache implements ICache { 31 private Cache _cache; 34 36 public static final String NAME = "ehcache 1.1"; 37 38 41 45 public void init() throws Exception { 46 Properties prop = new Properties (); 47 prop.put("log4j.logger.net.sf.ehcache", "fatal, ehcache"); 48 prop.put("log4j.appender.ehcache", "org.apache.log4j.ConsoleAppender"); 49 prop.put("log4j.appender.ehcache.layout", "org.apache.log4j.PatternLayout"); 50 prop.put("log4j.appender.ehcache.layout.ConversionPattern", "%d %-5p%m%n"); 51 PropertyConfigurator.configure(prop); 52 53 _cache = new Cache("test", 1000, false, true, 0, 0); 55 CacheManager.getInstance().addCache(_cache); 56 } 57 58 64 public Object get(Object id) throws Exception { 65 Element elm = _cache.get((Serializable )id); 66 return elm==null ? null : elm.getValue(); 67 } 68 69 75 public void put(Object id, Object value) throws Exception { 76 _cache.put(new Element((Serializable )id, (Serializable )value)); 77 } 78 79 84 public void remove(Object id) throws Exception { 85 _cache.remove((Serializable )id); 86 } 87 88 93 public long size() throws Exception { 94 return _cache.getSize(); 95 } 96 97 100 public void destroy() { 101 try { 102 CacheManager.getInstance().removeCache("test"); 103 _cache = null; 104 } catch (Exception e){ 105 e.printStackTrace(); 106 } 107 } 108 109 113 public String getCacheName() { 114 return NAME; 115 } 116 117 } | Popular Tags |