1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.cache.ehcache;
35
36
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import info.magnolia.module.cache.mbean.CacheMonitor;
41 import net.sf.ehcache.Ehcache;
42 import net.sf.ehcache.Element;
43 import net.sf.ehcache.constructs.blocking.BlockingCache;
44 import net.sf.ehcache.constructs.blocking.LockTimeoutException;
45
46
47
48
49
50
51 public class EhCacheWrapper implements info.magnolia.module.cache.BlockingCache {
52 private static final Logger log = LoggerFactory.getLogger(EhCacheWrapper.class);
53
54 private final BlockingCache ehcache;
55 private final CacheMonitor cacheMonitor;
56 private final String name;
57
58 public EhCacheWrapper(BlockingCache ehcache, CacheMonitor cacheMonitor, String name) {
59 this.ehcache = ehcache;
60 this.cacheMonitor = cacheMonitor;
61 this.name = name;
62 }
63
64 public EhCacheWrapper(Ehcache ehcache, CacheMonitor cacheMonitor, String name) {
65 this(castToBlockingCacheOrThrowException(ehcache), cacheMonitor, name);
66 }
67
68 private static BlockingCache castToBlockingCacheOrThrowException(Ehcache ehcache) {
69 if(!(ehcache instanceof BlockingCache)){
70 throw new RuntimeException("The current caching framework depends on the fact the a blocking cache is used.");
71 }
72 return (BlockingCache) ehcache;
73 }
74
75 @Override
76 public Object get(Object key) {
77 final Element element = ehcache.get(key);
78 try {
79 return element != null ? element.getObjectValue() : null;
80 } catch (LockTimeoutException e) {
81 log.error("Detected 1 thread stuck in generating response for {}. This might be temporary if obtaining the response is resource intensive or when accessing remote resources.", key);
82 throw e;
83 }
84 }
85
86 @Override
87 public boolean hasElement(Object key) {
88
89
90
91 try {
92
93
94
95 return ehcache.get(key) != null;
96 } catch (LockTimeoutException e) {
97 log.error("Detected 1 thread stuck in generating response for {}. This might be temporary if obtaining the response is resource intensive or when accessing remote resources.", key);
98
99
100
101 throw e;
102 }
103 }
104
105 @Override
106 public void put(Object key, Object value) {
107 final Element element = new Element(key, value);
108 ehcache.put(element);
109 }
110
111 @Override
112 public void put(Object key, Object value, int timeToLiveInSeconds) {
113 final Element element = new Element(key, value);
114 element.setTimeToLive(timeToLiveInSeconds);
115 ehcache.put(element);
116 }
117
118 @Override
119 public void remove(Object key) {
120 ehcache.remove(key);
121 }
122
123 @Override
124 public void clear() {
125 cacheMonitor.countFlush(this.name);
126 ehcache.removeAll();
127 }
128
129 @Override
130 public void unlock(Object key) {
131 if(ehcache.getQuiet(key) == null) {
132 put(key, null);
133 remove(key);
134 }
135 }
136
137 @Override
138 public int getBlockingTimeout() {
139 return ehcache.getTimeoutMillis();
140 }
141
142 public Ehcache getWrappedEhcache() {
143 return ehcache;
144 }
145
146 @Override
147 public String getName() {
148 return name;
149 }
150
151 @Override
152 public int getSize() {
153 return ehcache.getSize();
154 }
155
156
157
158 }