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