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.CacheListener;
018    import com.liferay.portal.kernel.cache.CacheListenerScope;
019    import com.liferay.portal.kernel.cache.PortalCache;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.concurrent.Future;
028    import java.util.concurrent.TimeUnit;
029    
030    import net.spy.memcached.MemcachedClientIF;
031    
032    /**
033     * @author Michael C. Han
034     */
035    public class MemcachePortalCache<V> implements PortalCache<String, V> {
036    
037            public MemcachePortalCache(
038                    String name, MemcachedClientIF memcachedClient, int timeout,
039                    TimeUnit timeoutTimeUnit) {
040    
041                    _name = name;
042                    _memcachedClient = memcachedClient;
043                    _timeout = timeout;
044                    _timeoutTimeUnit = timeoutTimeUnit;
045            }
046    
047            @Override
048            public void destroy() {
049                    _memcachedClient.shutdown();
050            }
051    
052            @Override
053            public Collection<V> get(Collection<String> keys) {
054                    List<String> processedKeys = new ArrayList<String>(keys.size());
055    
056                    for (String key : keys) {
057                            String processedKey = _name.concat(key);
058    
059                            processedKeys.add(processedKey);
060                    }
061    
062                    Future<Map<String, Object>> future = null;
063    
064                    try {
065                            future = _memcachedClient.asyncGetBulk(processedKeys);
066                    }
067                    catch (IllegalArgumentException iae) {
068                            if (_log.isWarnEnabled()) {
069                                    _log.warn("Error retrieving with keys " + keys, iae);
070                            }
071    
072                            return null;
073                    }
074    
075                    Map<String, Object> values = null;
076    
077                    try {
078                            values = future.get(_timeout, _timeoutTimeUnit);
079                    }
080                    catch (Throwable t) {
081                            if (_log.isWarnEnabled()) {
082                                    _log.warn("Memcache operation error", t);
083                            }
084    
085                            future.cancel(true);
086                    }
087    
088                    if (values != null) {
089                            return (Collection<V>)values.values();
090                    }
091    
092                    return null;
093            }
094    
095            @Override
096            public V get(String key) {
097                    String processedKey = _name.concat(key);
098    
099                    Future<Object> future = null;
100    
101                    try {
102                            future = _memcachedClient.asyncGet(processedKey);
103                    }
104                    catch (IllegalArgumentException iae) {
105                            if (_log.isWarnEnabled()) {
106                                    _log.warn("Error retrieving with key " + key, iae);
107                            }
108    
109                            return null;
110                    }
111    
112                    Object value = null;
113    
114                    try {
115                            value = future.get(_timeout, _timeoutTimeUnit);
116                    }
117                    catch (Throwable t) {
118                            if (_log.isWarnEnabled()) {
119                                    _log.warn("Memcache operation error", t);
120                            }
121    
122                            future.cancel(true);
123                    }
124    
125                    return (V)value;
126            }
127    
128            @Override
129            public String getName() {
130                    return _name;
131            }
132    
133            @Override
134            public void put(String key, V value) {
135                    put(key, value, _timeToLive);
136            }
137    
138            @Override
139            public void put(String key, V value, int timeToLive) {
140                    String processedKey = _name.concat(key);
141    
142                    try {
143                            _memcachedClient.set(processedKey, timeToLive, value);
144                    }
145                    catch (IllegalArgumentException iae) {
146                            if (_log.isWarnEnabled()) {
147                                    _log.warn("Error storing value with key " + key, iae);
148                            }
149                    }
150            }
151    
152            @Override
153            public void registerCacheListener(CacheListener<String, V> cacheListener) {
154                    registerCacheListener(cacheListener, CacheListenerScope.ALL);
155            }
156    
157            @Override
158            public void registerCacheListener(
159                    CacheListener<String, V> cacheListener,
160                    CacheListenerScope cacheListenerScope) {
161    
162                    throw new UnsupportedOperationException();
163            }
164    
165            @Override
166            public void remove(String key) {
167                    String processedKey = _name.concat(key);
168    
169                    try {
170                            _memcachedClient.delete(processedKey);
171                    }
172                    catch (IllegalArgumentException iae) {
173                            if (_log.isWarnEnabled()) {
174                                    _log.warn("Error removing value with key " + key, iae);
175                            }
176                    }
177            }
178    
179            @Override
180            public void removeAll() {
181                    _memcachedClient.flush();
182            }
183    
184            public void setTimeToLive(int timeToLive) {
185                    _timeToLive = timeToLive;
186            }
187    
188            @Override
189            public void unregisterCacheListener(
190                    CacheListener<String, V> cacheListener) {
191            }
192    
193            @Override
194            public void unregisterCacheListeners() {
195            }
196    
197            private static Log _log = LogFactoryUtil.getLog(MemcachePortalCache.class);
198    
199            private MemcachedClientIF _memcachedClient;
200            private String _name;
201            private int _timeout;
202            private TimeUnit _timeoutTimeUnit;
203            private int _timeToLive;
204    
205    }