1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }