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.cache.memcached;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.PortalCacheManager;
019    
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    import java.util.concurrent.TimeUnit;
023    
024    /**
025     * @author Michael C. Han
026     */
027    public class PooledMemcachePortalCacheManager implements PortalCacheManager {
028    
029            public void afterPropertiesSet() {
030            }
031    
032            public void destroy() throws Exception {
033                    for (PortalCache portalCache : _portalCaches.values()) {
034                            portalCache.destroy();
035                    }
036            }
037    
038            public void clearAll() {
039                    _portalCaches.clear();
040            }
041    
042            public PortalCache getCache(String name) {
043                    return getCache(name, false);
044            }
045    
046            public PortalCache getCache(String name, boolean blocking) {
047                    PortalCache portalCache = _portalCaches.get(name);
048    
049                    if (portalCache == null) {
050                            portalCache = new PooledMemcachePortalCache(
051                                    name, _memcachedClientFactory, _timeout, _timeoutTimeUnit);
052    
053                            portalCache.setDebug(_debug);
054    
055                            _portalCaches.put(name, portalCache);
056                    }
057    
058                    return portalCache;
059            }
060    
061            public void removeCache(String name) {
062                    _portalCaches.remove(name);
063            }
064    
065            public void setDebug(boolean debug) {
066                    _debug = debug;
067            }
068    
069            public void setMemcachedClientPool(
070                    MemcachedClientFactory memcachedClientFactory) {
071    
072                    _memcachedClientFactory = memcachedClientFactory;
073            }
074    
075            public void setTimeout(int timeout) {
076                    _timeout = timeout;
077            }
078    
079            public void setTimeoutTimeUnit(String timeoutTimeUnit) {
080                    _timeoutTimeUnit = TimeUnit.valueOf(timeoutTimeUnit);
081            }
082    
083            private boolean _debug;
084            private MemcachedClientFactory _memcachedClientFactory;
085            private Map<String, PortalCache> _portalCaches =
086                    new ConcurrentHashMap<String, PortalCache>();
087            private int _timeout;
088            private TimeUnit _timeoutTimeUnit;
089    
090    }