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