001    /**
002     * Copyright (c) 2000-2012 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.net.URL;
021    
022    import java.util.Map;
023    import java.util.concurrent.ConcurrentHashMap;
024    import java.util.concurrent.TimeUnit;
025    
026    import net.spy.memcached.MemcachedClientIF;
027    
028    /**
029     * @author Michael C. Han
030     */
031    public class MemcachePortalCacheManager implements PortalCacheManager {
032    
033            public void clearAll() {
034                    _memcachePortalCaches.clear();
035            }
036    
037            public void destroy() throws Exception {
038                    for (MemcachePortalCache memcachePortalCache :
039                                    _memcachePortalCaches.values()) {
040    
041                            memcachePortalCache.destroy();
042                    }
043            }
044    
045            public PortalCache getCache(String name) {
046                    return getCache(name, false);
047            }
048    
049            public PortalCache getCache(String name, boolean blocking) {
050                    MemcachePortalCache memcachePortalCache = _memcachePortalCaches.get(
051                            name);
052    
053                    if (memcachePortalCache == null) {
054                            try {
055                                    MemcachedClientIF memcachedClient =
056                                            _memcachedClientFactory.getMemcachedClient();
057    
058                                    memcachePortalCache = new MemcachePortalCache(
059                                            name, memcachedClient, _timeout, _timeoutTimeUnit);
060    
061                                    _memcachePortalCaches.put(name, memcachePortalCache);
062                            }
063                            catch (Exception e) {
064                                    throw new IllegalStateException(
065                                            "Unable to initiatlize Memcache connection", e);
066                            }
067                    }
068    
069                    return memcachePortalCache;
070            }
071    
072            public void reconfigureCaches(URL configurationURL) {
073            }
074    
075            public void removeCache(String name) {
076                    _memcachePortalCaches.remove(name);
077            }
078    
079            public void setMemcachedClientPool(
080                    MemcachedClientFactory memcachedClientFactory) {
081    
082                    _memcachedClientFactory = memcachedClientFactory;
083            }
084    
085            public void setTimeout(int timeout) {
086                    _timeout = timeout;
087            }
088    
089            public void setTimeoutTimeUnit(String timeoutTimeUnit) {
090                    _timeoutTimeUnit = TimeUnit.valueOf(timeoutTimeUnit);
091            }
092    
093            private MemcachedClientFactory _memcachedClientFactory;
094            private Map<String, MemcachePortalCache> _memcachePortalCaches =
095                    new ConcurrentHashMap<String, MemcachePortalCache>();
096            private int _timeout;
097            private TimeUnit _timeoutTimeUnit;
098    
099    }