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 |
|
import info.magnolia.context.MgnlContext; |
37 |
|
import info.magnolia.module.cache.CacheModule; |
38 |
|
import info.magnolia.module.cache.exception.MgnlLockTimeoutException; |
39 |
|
import info.magnolia.module.cache.listeners.AbstractListeningCacheWrapper; |
40 |
|
import info.magnolia.module.cache.mbean.CacheMonitor; |
41 |
|
import info.magnolia.objectfactory.Components; |
42 |
|
|
43 |
|
import java.util.List; |
44 |
|
|
45 |
|
import org.slf4j.Logger; |
46 |
|
import org.slf4j.LoggerFactory; |
47 |
|
|
48 |
|
import net.sf.ehcache.Ehcache; |
49 |
|
import net.sf.ehcache.Element; |
50 |
|
import net.sf.ehcache.constructs.blocking.BlockingCache; |
51 |
|
import net.sf.ehcache.constructs.blocking.LockTimeoutException; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
@deprecated@link |
57 |
|
|
58 |
|
@Deprecated |
|
|
| 62.3% |
Uncovered Elements: 23 (61) |
Complexity: 18 |
Complexity Density: 0.44 |
|
59 |
|
public class EhCacheWrapper extends AbstractListeningCacheWrapper implements info.magnolia.module.cache.BlockingCache { |
60 |
|
private static final Logger log = LoggerFactory.getLogger(EhCacheWrapper.class); |
61 |
|
|
62 |
|
private final BlockingCache ehcache; |
63 |
|
private final CacheMonitor cacheMonitor; |
64 |
|
private final String name; |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
66 |
24 |
public EhCacheWrapper(CacheModule cacheModule, Ehcache ehcache, CacheMonitor cacheMonitor, String name) {... |
67 |
24 |
super(cacheModule); |
68 |
24 |
this.ehcache = castToBlockingCacheOrThrowException(ehcache); |
69 |
24 |
this.cacheMonitor = cacheMonitor; |
70 |
24 |
this.name = name; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
@deprecated@link |
75 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
76 |
0 |
public EhCacheWrapper(BlockingCache ehcache, CacheMonitor cacheMonitor, String name) {... |
77 |
0 |
this(Components.getComponent(CacheModule.class), ehcache, cacheMonitor, name); |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
@deprecated@link |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
0 |
public EhCacheWrapper(Ehcache ehcache, CacheMonitor cacheMonitor, String name) {... |
84 |
0 |
this(castToBlockingCacheOrThrowException(ehcache), cacheMonitor, name); |
85 |
|
} |
86 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
87 |
24 |
private static BlockingCache castToBlockingCacheOrThrowException(Ehcache ehcache) {... |
88 |
24 |
if (!(ehcache instanceof BlockingCache)) { |
89 |
0 |
throw new RuntimeException("The current caching framework depends on the fact the a blocking cache is used."); |
90 |
|
} |
91 |
24 |
return (BlockingCache) ehcache; |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
94 |
36 |
@Override... |
95 |
|
public Object get(Object key) { |
96 |
36 |
Object value; |
97 |
36 |
try { |
98 |
37 |
final Element element = ehcache.get(key); |
99 |
34 |
value = element != null ? element.getObjectValue() : null; |
100 |
|
} catch (LockTimeoutException e) { |
101 |
3 |
log.error("Detected 1 thread stuck in generating response. This might be temporary if obtaining the response is resource intensive or when accessing remote resources. [url={}]", MgnlContext.getWebContext().getRequest().getRequestURL()); |
102 |
3 |
throw new MgnlLockTimeoutException(e); |
103 |
|
} |
104 |
34 |
super.get(key); |
105 |
34 |
return value; |
106 |
|
} |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
108 |
2 |
@Override... |
109 |
|
public Object getQuiet(Object key) { |
110 |
2 |
super.getQuiet(key); |
111 |
2 |
Element element = ehcache.getQuiet(key); |
112 |
2 |
return element == null ? null : element.getObjectValue(); |
113 |
|
} |
114 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
115 |
0 |
@Override... |
116 |
|
public boolean hasElement(Object key) { |
117 |
0 |
boolean hasElement; |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
0 |
try { |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
0 |
hasElement = ehcache.get(key) != null; |
126 |
|
} catch (LockTimeoutException e) { |
127 |
0 |
log.error("Detected 1 thread stuck in generating response. This might be temporary if obtaining the response is resource intensive or when accessing remote resources. [url={}]", MgnlContext.getWebContext().getRequest().getRequestURL()); |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
0 |
throw new MgnlLockTimeoutException(e); |
132 |
|
} |
133 |
0 |
return hasElement; |
134 |
|
} |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
136 |
12 |
@Override... |
137 |
|
public void put(Object key, Object value) { |
138 |
12 |
final Element element = new Element(key, value); |
139 |
12 |
ehcache.put(element); |
140 |
12 |
super.put(key, value); |
141 |
|
} |
142 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
143 |
0 |
@Override... |
144 |
|
public void put(Object key, Object value, int timeToLiveInSeconds) { |
145 |
0 |
final Element element = new Element(key, value); |
146 |
0 |
element.setTimeToLive(timeToLiveInSeconds); |
147 |
0 |
ehcache.put(element); |
148 |
0 |
super.put(key, value, timeToLiveInSeconds); |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
151 |
1 |
@Override... |
152 |
|
public void remove(Object key) { |
153 |
1 |
ehcache.remove(key); |
154 |
1 |
super.remove(key); |
155 |
|
} |
156 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
157 |
0 |
@Override... |
158 |
|
public void clear() { |
159 |
0 |
cacheMonitor.countFlush(this.name); |
160 |
0 |
ehcache.removeAll(); |
161 |
0 |
super.clear(); |
162 |
|
} |
163 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
164 |
1 |
@Override... |
165 |
|
public void unlock(Object key) { |
166 |
1 |
if (ehcache.getQuiet(key) == null) { |
167 |
1 |
put(key, null); |
168 |
1 |
remove(key); |
169 |
|
} |
170 |
|
} |
171 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
172 |
|
@Override... |
173 |
|
public int getBlockingTimeout() { |
174 |
|
return ehcache.getTimeoutMillis(); |
175 |
|
} |
176 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
177 |
|
public Ehcache getWrappedEhcache() {... |
178 |
|
return ehcache; |
179 |
|
} |
180 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
181 |
|
@Override... |
182 |
|
public String getName() { |
183 |
|
return name; |
184 |
|
} |
185 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
186 |
|
@Override... |
187 |
|
public int getSize() { |
188 |
|
return ehcache.getSize(); |
189 |
|
} |
190 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
191 |
|
@Override... |
192 |
|
public List<Object> getKeys() { |
193 |
|
return ehcache.getKeys(); |
194 |
|
} |
195 |
|
|
196 |
|
} |