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 void afterPropertiesSet() {
032 _portalCache = _singleVMPool.getCache(_CACHE_NAME);
033 }
034
035 public void clear() {
036 _portalCache.removeAll();
037 }
038
039 public Object get(String key, WebCacheItem wci) {
040 Object obj = _portalCache.get(key);
041
042 if (obj == null) {
043 try {
044 obj = wci.convert(key);
045
046 int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
047
048 _portalCache.put(key, obj, timeToLive);
049 }
050 catch (WebCacheException wce) {
051 if (_log.isWarnEnabled()) {
052 Throwable cause = wce.getCause();
053
054 if (cause != null) {
055 _log.warn(cause, cause);
056 }
057 else {
058 _log.warn(wce, wce);
059 }
060 }
061 }
062 }
063
064 return obj;
065 }
066
067 public void remove(String key) {
068 _portalCache.remove(key);
069 }
070
071 public void setSingleVMPool(SingleVMPool singleVMPool) {
072 _singleVMPool = singleVMPool;
073 }
074
075 private static final String _CACHE_NAME = WebCachePool.class.getName();
076
077 private static Log _log = LogFactoryUtil.getLog(WebCachePoolImpl.class);
078
079 private PortalCache _portalCache;
080 private SingleVMPool _singleVMPool;
081
082 }