KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > cache > ehcache > CacheImpl


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 JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.OutputStream JavaDoc;
12
13 import javax.jcr.RepositoryException;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
27  * A <code>Cache</code> implementation using <a HREF="http://ehcache.sf.net/">EHCACHE</a>.
28  * @author Andreas Brenk
29  * @author Fabrizio Giustina
30  * @since 3.0
31  */

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     /**
51      * Remove the entry
52      */

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 JavaDoc(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 JavaDoc(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 JavaDoc(e);
92         }
93     }
94
95     public void stop() {
96         this.ehcacheManager.shutdown();
97     }
98
99     public boolean streamFromCache(CacheKey key, HttpServletResponse JavaDoc 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 JavaDoc in = new ByteArrayInputStream JavaDoc(buffer);
108             response.setContentLength(buffer.length);
109
110             try {
111                 OutputStream JavaDoc out = response.getOutputStream();
112                 IOUtils.copy(in, out);
113                 out.flush();
114                 IOUtils.closeQuietly(out);
115             }
116             catch (IOException JavaDoc 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 JavaDoc(e);
128         }
129     }
130
131     private Cache createCache() throws ConfigurationException {
132
133         // commented lines require ehcache 1.2
134
try {
135             Content configNode = this.config.getContent("ehcache");
136             String JavaDoc name = configNode.getNodeData("name").getString();
137             int maxElements = (int) configNode.getNodeData("maxElementsInMemory").getLong();
138
139             // MemoryStoreEvictionPolicy evictionPolicy =
140
// MemoryStoreEvictionPolicy.fromString(configNode.getNodeData("memoryStoreEvictionPolicy").getString());
141
boolean overflow = configNode.getNodeData("overflowToDisk").getBoolean();
142
143             // String diskStore = configNode.getNodeData("diskStorePath").getString();
144
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             // RegisteredEventListeners listeners = null;
150

151             // if (StringUtils.isBlank(diskStore)) { diskStore = Path.getCacheDirectoryPath(); }
152

153             // return new Cache(name, maxElements, evictionPolicy, overflow, diskStore, eternal, ttl, tti, persistent,
154
// expiryInterval,
155
// listeners);
156
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