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 net.spy.memcached.MemcachedClientIF;
018    
019    import org.apache.commons.pool.ObjectPool;
020    
021    /**
022     * @author Michael C. Han
023     */
024    public class PooledMemcachedClientFactory implements MemcachedClientFactory {
025    
026            @Override
027            public void clear() throws Exception {
028                    _memcachedClientPool.clear();
029            }
030    
031            @Override
032            public void close() throws Exception {
033                    _memcachedClientPool.close();
034            }
035    
036            public void destroy() {
037                    try {
038                            close();
039                    }
040                    catch (Exception e) {
041                    }
042            }
043    
044            @Override
045            public MemcachedClientIF getMemcachedClient() throws Exception {
046                    return (MemcachedClientIF)_memcachedClientPool.borrowObject();
047            }
048    
049            @Override
050            public int getNumActive() {
051                    return _memcachedClientPool.getNumActive();
052            }
053    
054            @Override
055            public int getNumIdle() {
056                    return _memcachedClientPool.getNumIdle();
057            }
058    
059            @Override
060            public void invalidateMemcachedClient(MemcachedClientIF memcachedClient)
061                    throws Exception {
062    
063                    _memcachedClientPool.invalidateObject(memcachedClient);
064            }
065    
066            @Override
067            public void returnMemcachedObject(MemcachedClientIF memcachedClient)
068                    throws Exception {
069    
070                    _memcachedClientPool.returnObject(memcachedClient);
071            }
072    
073            public void setMemcachedClientPool(ObjectPool memcachedClientPool) {
074                    _memcachedClientPool = memcachedClientPool;
075            }
076    
077            private ObjectPool _memcachedClientPool;
078    
079    }