View Javadoc

1   /**
2    * This file Copyright (c) 2008-2011 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  
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   * Magnolia cache wrapper for underlying ehCache implementation.
48   * @author gjoseph
49   * @version $Revision: $ ($Author: $)
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          // we can't use isKeyInCache(), as it does not check for the element's expiration
89          // which may lead to unexpected results.
90          // return ehcache.isKeyInCache(key);
91          try {
92              // get() and getQuiet() do check for expiration and return null if the element was expired.
93              // we can't use getQuiet, as it's non-blocking which could lead to multiple copies of same page to be generated
94              // if page is requested while previous request for same page is still being processed by different thread
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              // 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.
99              // returning false would mean server attempts to generate the response again, possibly loosing another thread in the process
100             // returning true means server will assume resource exists and will try to retrieve it later, possibly failing with the same error
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 }