001    /**
002     * Copyright (c) 2000-2013 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.security.pacl.DoPrivileged;
022    import com.liferay.portal.kernel.util.Time;
023    import com.liferay.portal.kernel.webcache.WebCacheException;
024    import com.liferay.portal.kernel.webcache.WebCacheItem;
025    import com.liferay.portal.kernel.webcache.WebCachePool;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    @DoPrivileged
031    public class WebCachePoolImpl implements WebCachePool {
032    
033            public void afterPropertiesSet() {
034                    _portalCache = (PortalCache<String, Object>)_singleVMPool.getCache(
035                            _CACHE_NAME);
036            }
037    
038            @Override
039            public void clear() {
040                    _portalCache.removeAll();
041            }
042    
043            @Override
044            public Object get(String key, WebCacheItem wci) {
045                    Object obj = _portalCache.get(key);
046    
047                    if (obj != null) {
048                            return obj;
049                    }
050    
051                    try {
052                            obj = wci.convert(key);
053    
054                            int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
055    
056                            _portalCache.put(key, obj, timeToLive);
057                    }
058                    catch (WebCacheException wce) {
059                            if (_log.isWarnEnabled()) {
060                                    Throwable cause = wce.getCause();
061    
062                                    if (cause != null) {
063                                            _log.warn(cause, cause);
064                                    }
065                                    else {
066                                            _log.warn(wce, wce);
067                                    }
068                            }
069                    }
070    
071                    return obj;
072            }
073    
074            @Override
075            public void remove(String key) {
076                    _portalCache.remove(key);
077            }
078    
079            public void setSingleVMPool(SingleVMPool singleVMPool) {
080                    _singleVMPool = singleVMPool;
081            }
082    
083            private static final String _CACHE_NAME = WebCachePool.class.getName();
084    
085            private static Log _log = LogFactoryUtil.getLog(WebCachePoolImpl.class);
086    
087            private PortalCache<String, Object> _portalCache;
088            private SingleVMPool _singleVMPool;
089    
090    }