001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    if (values != null) {
089                            return values.values();
090                    }
091    
092                    return null;
093            }
094    
095            public Object get(Serializable key) {
096                    String processedKey = _name.concat(String.valueOf(key));
097    
098                    Future<Object> future = null;
099    
100                    try {
101                            future = _memcachedClient.asyncGet(processedKey);
102                    }
103                    catch (IllegalArgumentException iae) {
104                            if (_log.isWarnEnabled()) {
105                                    _log.warn("Error retrieving with key " + key, iae);
106                            }
107    
108                            return null;
109                    }
110    
111                    Object value = null;
112    
113                    try {
114                            value = future.get(_timeout, _timeoutTimeUnit);
115                    }
116                    catch (Throwable t) {
117                            if (_log.isWarnEnabled()) {
118                                    _log.warn("Memcache operation error", t);
119                            }
120    
121                            future.cancel(true);
122                    }
123    
124                    return value;
125            }
126    
127            public String getName() {
128                    return _name;
129            }
130    
131            public void put(Serializable key, Object value) {
132                    put(key, value, _timeToLive);
133            }
134    
135            public void put(Serializable key, Object value, int timeToLive) {
136                    String processedKey = _name.concat(String.valueOf(key));
137    
138                    try {
139                            _memcachedClient.set(processedKey, timeToLive, value);
140                    }
141                    catch (IllegalArgumentException iae) {
142                            if (_log.isWarnEnabled()) {
143                                    _log.warn("Error storing value with key " + key, iae);
144                            }
145                    }
146            }
147    
148            public void put(Serializable key, Serializable value) {
149                    put(key, value, _timeToLive);
150            }
151    
152            public void put(Serializable key, Serializable value, int timeToLive) {
153                    String processedKey = _name.concat(String.valueOf(key));
154    
155                    try {
156                            _memcachedClient.set(processedKey, timeToLive, value);
157                    }
158                    catch (IllegalArgumentException iae) {
159                            if (_log.isWarnEnabled()) {
160                                    _log.warn("Error storing value with key " + key, iae);
161                            }
162                    }
163            }
164    
165            public void registerCacheListener(CacheListener cacheListener) {
166                    registerCacheListener(cacheListener, CacheListenerScope.ALL);
167            }
168    
169            public void registerCacheListener(
170                    CacheListener cacheListener, CacheListenerScope cacheListenerScope) {
171    
172                    throw new UnsupportedOperationException();
173            }
174    
175            public void remove(Serializable key) {
176                    String processedKey = _name.concat(String.valueOf(key));
177    
178                    try {
179                            _memcachedClient.delete(processedKey);
180                    }
181                    catch (IllegalArgumentException iae) {
182                            if (_log.isWarnEnabled()) {
183                                    _log.warn("Error removing value with key " + key, iae);
184                            }
185                    }
186            }
187    
188            public void removeAll() {
189                    _memcachedClient.flush();
190            }
191    
192            public void setTimeToLive(int timeToLive) {
193                    _timeToLive = timeToLive;
194            }
195    
196            public void unregisterCacheListener(CacheListener cacheListener) {
197            }
198    
199            public void unregisterCacheListeners() {
200            }
201    
202            private static Log _log = LogFactoryUtil.getLog(MemcachePortalCache.class);
203    
204            private MemcachedClientIF _memcachedClient;
205            private String _name;
206            private int _timeout;
207            private TimeUnit _timeoutTimeUnit;
208            private int _timeToLive;
209    
210    }