001    /**
002     * Copyright (c) 2000-2010 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.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 setDebug(boolean debug) {
062                    _debug = debug;
063            }
064    
065            public void setMemcachedClientPool(
066                    MemcachedClientFactory memcachedClientFactory) {
067    
068                    _memcachedClientFactory = memcachedClientFactory;
069            }
070    
071            public void setTimeout(int timeout) {
072                    _timeout = timeout;
073            }
074    
075            public void setTimeoutTimeUnit(String timeoutTimeUnit) {
076                    _timeoutTimeUnit = TimeUnit.valueOf(timeoutTimeUnit);
077            }
078    
079            private boolean _debug;
080            private MemcachedClientFactory _memcachedClientFactory;
081            private Map<String, PortalCache> _portalCaches =
082                    new ConcurrentHashMap<String, PortalCache>();
083            private int _timeout;
084            private TimeUnit _timeoutTimeUnit;
085    
086    }