001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
027     * @author Brian Wing Shun Chan
028     */
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    }