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    import net.spy.memcached.MemcachedClientIF;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class MemcachePortalCacheManager implements PortalCacheManager {
030    
031            public void clearAll() {
032                    _memcachePortalCaches.clear();
033            }
034    
035            public void destroy() throws Exception {
036                    for (MemcachePortalCache memcachePortalCache :
037                                    _memcachePortalCaches.values()) {
038    
039                            memcachePortalCache.destroy();
040                    }
041            }
042    
043            public PortalCache getCache(String name) {
044                    return getCache(name, false);
045            }
046    
047            public PortalCache getCache(String name, boolean blocking) {
048                    MemcachePortalCache memcachePortalCache = _memcachePortalCaches.get(
049                            name);
050    
051                    if (memcachePortalCache == null) {
052                            try {
053                                    MemcachedClientIF memcachedClient  =
054                                            _memcachedClientFactory.getMemcachedClient();
055    
056                                    memcachePortalCache = new MemcachePortalCache(
057                                            name, memcachedClient, _timeout, _timeoutTimeUnit);
058    
059                                    memcachePortalCache.setDebug(_debug);
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 setDebug(boolean debug) {
073                    _debug = debug;
074            }
075    
076            public void setMemcachedClientPool(
077                    MemcachedClientFactory memcachedClientFactory) {
078    
079                    _memcachedClientFactory = memcachedClientFactory;
080            }
081    
082            public void setTimeout(int timeout) {
083                    _timeout = timeout;
084            }
085    
086            public void setTimeoutTimeUnit(String timeoutTimeUnit) {
087                    _timeoutTimeUnit = TimeUnit.valueOf(timeoutTimeUnit);
088            }
089    
090            private boolean _debug;
091            private MemcachedClientFactory _memcachedClientFactory;
092            private Map<String, MemcachePortalCache> _memcachePortalCaches =
093                    new ConcurrentHashMap<String, MemcachePortalCache>();
094            private int _timeout;
095            private TimeUnit _timeoutTimeUnit;
096    
097    }