Clover icon

Magnolia Module EhCache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:52:57 CET
  2. Package info.magnolia.module.cache.ehcache

File EhCacheWrapper.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
71% of files have more coverage

Code metrics

8
41
12
1
196
119
18
0.44
3.42
12
1.5
14.1% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
EhCacheWrapper 59 41 14.1% 18 23
0.622950862.3%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /**
2    * This file Copyright (c) 2008-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Magnolia cache wrapper for underlying ehCache implementation.
55    *
56    * @deprecated since 5.4.4. Ehcache2 implementation will be dropped in a next major version in favour of EhCache3. Use {@link info.magnolia.module.cache.ehcache3.EhCache3Wrapper} instead.
57    */
58    @Deprecated
 
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   
 
66  24 toggle 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 since 5.3. Use {@link #EhCacheWrapper(CacheModule, Ehcache, CacheMonitor, String)} instead.
75    */
 
76  0 toggle public EhCacheWrapper(BlockingCache ehcache, CacheMonitor cacheMonitor, String name) {
77  0 this(Components.getComponent(CacheModule.class), ehcache, cacheMonitor, name);
78    }
79   
80    /**
81    * @deprecated since 5.3. Use {@link #EhCacheWrapper(CacheModule, Ehcache, CacheMonitor, String)} instead.
82    */
 
83  0 toggle public EhCacheWrapper(Ehcache ehcache, CacheMonitor cacheMonitor, String name) {
84  0 this(castToBlockingCacheOrThrowException(ehcache), cacheMonitor, name);
85    }
86   
 
87  24 toggle 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   
 
94  36 toggle @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   
 
108  2 toggle @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   
 
115  0 toggle @Override
116    public boolean hasElement(Object key) {
117  0 boolean hasElement;
118    // we can't use isKeyInCache(), as it does not check for the element's expiration
119    // which may lead to unexpected results.
120    // return ehcache.isKeyInCache(key);
121  0 try {
122    // get() and getQuiet() do check for expiration and return null if the element was expired.
123    // we can't use getQuiet, as it's non-blocking which could lead to multiple copies of same page to be generated
124    // if page is requested while previous request for same page is still being processed by different thread
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    // FYI: in case you want to return some value instead of re-throwing exception: this is a dilemma ... obviously resource does not exist yet, but being stuck here for while means that it is either being generated or it takes time to generate.
129    // returning false would mean server attempts to generate the response again, possibly loosing another thread in the process
130    // returning true means server will assume resource exists and will try to retrieve it later, possibly failing with the same error
131  0 throw new MgnlLockTimeoutException(e);
132    }
133  0 return hasElement;
134    }
135   
 
136  12 toggle @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   
 
143  0 toggle @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   
 
151  1 toggle @Override
152    public void remove(Object key) {
153  1 ehcache.remove(key);
154  1 super.remove(key);
155    }
156   
 
157  0 toggle @Override
158    public void clear() {
159  0 cacheMonitor.countFlush(this.name);
160  0 ehcache.removeAll();
161  0 super.clear();
162    }
163   
 
164  1 toggle @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   
 
172    toggle @Override
173    public int getBlockingTimeout() {
174    return ehcache.getTimeoutMillis();
175    }
176   
 
177    toggle public Ehcache getWrappedEhcache() {
178    return ehcache;
179    }
180   
 
181    toggle @Override
182    public String getName() {
183    return name;
184    }
185   
 
186    toggle @Override
187    public int getSize() {
188    return ehcache.getSize();
189    }
190   
 
191    toggle @Override
192    public List<Object> getKeys() {
193    return ehcache.getKeys();
194    }
195   
196    }