001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 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    }