1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.webcache;
16  
17  import com.liferay.portal.kernel.cache.PortalCache;
18  import com.liferay.portal.kernel.cache.SingleVMPool;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.Time;
22  import com.liferay.portal.kernel.webcache.WebCacheException;
23  import com.liferay.portal.kernel.webcache.WebCacheItem;
24  import com.liferay.portal.kernel.webcache.WebCachePool;
25  
26  /**
27   * <a href="WebCachePoolImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class WebCachePoolImpl implements WebCachePool {
32  
33      public static final String CACHE_NAME = WebCachePool.class.getName();
34  
35      public void afterPropertiesSet() {
36          _cache = _singleVMPool.getCache(CACHE_NAME);
37      }
38  
39      public void clear() {
40          _cache.removeAll();
41      }
42  
43      public Object get(String key, WebCacheItem wci) {
44          Object obj = _cache.get(key);
45  
46          if (obj == null) {
47              try {
48                  obj = wci.convert(key);
49  
50                  int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
51  
52                  _cache.put(key, obj, timeToLive);
53              }
54              catch (WebCacheException wce) {
55                  if (_log.isWarnEnabled()) {
56                      Throwable cause = wce.getCause();
57  
58                      if (cause != null) {
59                          _log.warn(cause, cause);
60                      }
61                      else {
62                          _log.warn(wce, wce);
63                      }
64                  }
65              }
66          }
67  
68          return obj;
69      }
70  
71      public void remove(String key) {
72          _cache.remove(key);
73      }
74  
75      public void setSingleVMPool(SingleVMPool singleVMPool) {
76          _singleVMPool = singleVMPool;
77      }
78  
79      private static Log _log = LogFactoryUtil.getLog(WebCachePoolImpl.class);
80  
81      private SingleVMPool _singleVMPool;
82      private PortalCache _cache;
83  
84  }