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