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 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            public MemcachedClientIF getMemcachedClient() throws Exception {
027                    return (MemcachedClientIF)_memcachedClientPool.borrowObject();
028            }
029    
030            public void clear() throws Exception {
031                    _memcachedClientPool.clear();
032            }
033    
034            public void close() throws Exception {
035                    _memcachedClientPool.close();
036            }
037    
038            public void destroy() {
039                    try {
040                            close();
041                    }
042                    catch (Exception e) {
043                    }
044            }
045    
046            public int getNumActive() {
047                    return _memcachedClientPool.getNumActive();
048            }
049    
050            public int getNumIdle() {
051                    return _memcachedClientPool.getNumIdle();
052            }
053    
054            public void invalidateMemcachedClient(MemcachedClientIF memcachedClient)
055                    throws Exception {
056    
057                    _memcachedClientPool.invalidateObject(memcachedClient);
058            }
059    
060            public void returnMemcachedObject(MemcachedClientIF memcachedClient)
061                    throws Exception {
062    
063                    _memcachedClientPool.returnObject(memcachedClient);
064            }
065    
066            public void setMemcachedClientPool(ObjectPool memcachedClientPool) {
067                    _memcachedClientPool = memcachedClientPool;
068            }
069    
070            private ObjectPool _memcachedClientPool;
071    
072    }