001    /**
002     * Copyright (c) 2000-2011 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 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    }