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