1 package info.magnolia.cms.cache.ehcache; 2 3 import info.magnolia.cms.beans.config.ConfigurationException; 4 import info.magnolia.cms.cache.CacheConfig; 5 import info.magnolia.cms.cache.CacheKey; 6 import info.magnolia.cms.cache.CacheableEntry; 7 import info.magnolia.cms.core.Content; 8 9 import java.io.ByteArrayInputStream ; 10 import java.io.IOException ; 11 import java.io.OutputStream ; 12 13 import javax.jcr.RepositoryException; 14 import javax.servlet.http.HttpServletResponse ; 15 16 import net.sf.ehcache.Cache; 17 import net.sf.ehcache.CacheException; 18 import net.sf.ehcache.CacheManager; 19 import net.sf.ehcache.Element; 20 21 import org.apache.commons.io.IOUtils; 22 import org.slf4j.Logger; 23 import org.slf4j.LoggerFactory; 24 25 26 32 public class CacheImpl implements info.magnolia.cms.cache.Cache { 33 34 private static final Logger log = LoggerFactory.getLogger(CacheImpl.class); 35 36 private CacheConfig config; 37 38 private Cache ehcache; 39 40 private CacheManager ehcacheManager; 41 42 public void cacheRequest(CacheKey key, CacheableEntry out, boolean canCompress) { 43 this.ehcache.put(new Element(key, out)); 44 } 45 46 public void flush() { 47 this.ehcache.removeAll(); 48 } 49 50 53 public void remove(CacheKey key) { 54 this.ehcache.remove(key); 55 } 56 57 public long getCreationTime(CacheKey key) { 58 try { 59 Element element = this.ehcache.get(key); 60 61 if (element == null) { 62 return -1; 63 } 64 65 return element.getCreationTime(); 66 } 67 catch (CacheException e) { 68 throw new RuntimeException (e); 69 } 70 } 71 72 public void start(CacheConfig config) throws ConfigurationException { 73 try { 74 this.config = config; 75 this.ehcache = createCache(); 76 this.ehcacheManager = CacheManager.getInstance(); 77 this.ehcacheManager.addCache(this.ehcache); 78 } 79 catch (CacheException e) { 80 throw new RuntimeException (e); 81 } 82 } 83 84 public boolean isCached(CacheKey request) { 85 try { 86 Element element = this.ehcache.getQuiet(request); 87 88 return (element != null); 89 } 90 catch (CacheException e) { 91 throw new RuntimeException (e); 92 } 93 } 94 95 public void stop() { 96 this.ehcacheManager.shutdown(); 97 } 98 99 public boolean streamFromCache(CacheKey key, HttpServletResponse response, boolean canCompress) { 100 try { 101 Element element = this.ehcache.get(key); 102 if (element == null) { 103 return false; 104 } 105 106 byte[] buffer = ((CacheableEntry) element.getValue()).getOut(); 107 ByteArrayInputStream in = new ByteArrayInputStream (buffer); 108 response.setContentLength(buffer.length); 109 110 try { 111 OutputStream out = response.getOutputStream(); 112 IOUtils.copy(in, out); 113 out.flush(); 114 IOUtils.closeQuietly(out); 115 } 116 catch (IOException e) { 117 log.error("Error while reading cache for: '" + key + "'.", e); 118 return false; 119 } 120 finally { 121 IOUtils.closeQuietly(in); 122 } 123 124 return true; 125 } 126 catch (CacheException e) { 127 throw new RuntimeException (e); 128 } 129 } 130 131 private Cache createCache() throws ConfigurationException { 132 133 try { 135 Content configNode = this.config.getContent("ehcache"); 136 String name = configNode.getNodeData("name").getString(); 137 int maxElements = (int) configNode.getNodeData("maxElementsInMemory").getLong(); 138 139 boolean overflow = configNode.getNodeData("overflowToDisk").getBoolean(); 142 143 boolean eternal = configNode.getNodeData("eternal").getBoolean(); 145 long ttl = configNode.getNodeData("timeToLiveSeconds").getLong(); 146 long tti = configNode.getNodeData("timeToIdleSeconds").getLong(); 147 boolean persistent = configNode.getNodeData("diskPersistent").getBoolean(); 148 long expiryInterval = configNode.getNodeData("diskExpiryThreadIntervalSeconds").getLong(); 149 151 153 return new Cache(name, maxElements, overflow, eternal, ttl, tti, persistent, expiryInterval); 157 } 158 catch (RepositoryException e) { 159 throw new ConfigurationException(e); 160 } 161 } 162 163 } 164 | Popular Tags |