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