001
014
015 package com.liferay.portal.webcache;
016
017 import com.liferay.portal.kernel.cache.PortalCache;
018 import com.liferay.portal.kernel.cache.SingleVMPool;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.Time;
022 import com.liferay.portal.kernel.webcache.WebCacheException;
023 import com.liferay.portal.kernel.webcache.WebCacheItem;
024 import com.liferay.portal.kernel.webcache.WebCachePool;
025
026
029 public class WebCachePoolImpl implements WebCachePool {
030
031 public static final String CACHE_NAME = WebCachePool.class.getName();
032
033 public void afterPropertiesSet() {
034 _cache = _singleVMPool.getCache(CACHE_NAME);
035 }
036
037 public void clear() {
038 _cache.removeAll();
039 }
040
041 public Object get(String key, WebCacheItem wci) {
042 Object obj = _cache.get(key);
043
044 if (obj == null) {
045 try {
046 obj = wci.convert(key);
047
048 int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
049
050 _cache.put(key, obj, timeToLive);
051 }
052 catch (WebCacheException wce) {
053 if (_log.isWarnEnabled()) {
054 Throwable cause = wce.getCause();
055
056 if (cause != null) {
057 _log.warn(cause, cause);
058 }
059 else {
060 _log.warn(wce, wce);
061 }
062 }
063 }
064 }
065
066 return obj;
067 }
068
069 public void remove(String key) {
070 _cache.remove(key);
071 }
072
073 public void setSingleVMPool(SingleVMPool singleVMPool) {
074 _singleVMPool = singleVMPool;
075 }
076
077 private static Log _log = LogFactoryUtil.getLog(WebCachePoolImpl.class);
078
079 private SingleVMPool _singleVMPool;
080 private PortalCache _cache;
081
082 }