1 10 11 package net.sf.cache4j.perfomance.cache; 12 13 import net.sf.cache4j.perfomance.ICache; 14 import com.opensymphony.oscache.general.GeneralCacheAdministrator; 15 16 import java.util.Properties ; 17 18 import org.apache.log4j.PropertyConfigurator; 19 20 26 27 public class oscache implements ICache { 28 private GeneralCacheAdministrator _cache; 31 33 public static final String NAME = "oscache 2.2"; 34 35 38 42 public void init() throws Exception { 43 Properties prop = new Properties (); 44 prop.put("log4j.logger.com.opensymphony.oscache", "fatal, oscache"); 45 prop.put("log4j.appender.oscache", "org.apache.log4j.ConsoleAppender"); 46 prop.put("log4j.appender.oscache.layout", "org.apache.log4j.PatternLayout"); 47 prop.put("log4j.appender.oscache.layout.ConversionPattern", "%d %-5p%m%n"); 48 PropertyConfigurator.configure(prop); 49 50 prop = new Properties (); 51 prop.put("cache.memory", "true"); 52 prop.put("cache.use.host.domain.in.key", "false"); 53 prop.put("cache.algorithm", "com.opensymphony.oscache.base.algorithm.LRUCache"); 54 prop.put("cache.capacity", "1000"); 55 _cache = new GeneralCacheAdministrator(prop); 56 } 57 58 64 public Object get(Object id) throws Exception { 65 try { 66 return _cache.getFromCache(id.toString()); 67 } catch (Exception ex) { 68 try { 69 _cache.cancelUpdate(id.toString()); 70 80 } catch (Exception e){ } 81 } 82 return null; 83 } 84 85 91 public void put(Object id, Object value) throws Exception { 92 boolean updated = false; 93 try { 94 _cache.putInCache(id.toString(), value); 95 updated = true; 96 } catch (Exception ex) { 97 if (!updated) { 98 try { 99 _cache.cancelUpdate(id.toString()); 100 110 } catch (Exception e){ } 111 } 112 } 113 } 114 115 120 public void remove(Object id) throws Exception { 121 _cache.flushEntry(id.toString()); 122 } 123 124 129 public long size() throws Exception { 130 return 0; 131 } 132 133 136 public void destroy() { 137 _cache = null; 138 } 139 140 144 public String getCacheName() { 145 return NAME; 146 } 147 148 } | Popular Tags |