View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client.cache.memcached;
28  
29  import java.io.IOException;
30  import java.net.InetSocketAddress;
31  
32  import net.spy.memcached.CASResponse;
33  import net.spy.memcached.CASValue;
34  import net.spy.memcached.MemcachedClient;
35  import net.spy.memcached.MemcachedClientIF;
36  import net.spy.memcached.OperationTimeoutException;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.apache.http.client.cache.HttpCacheEntry;
41  import org.apache.http.client.cache.HttpCacheEntrySerializer;
42  import org.apache.http.client.cache.HttpCacheStorage;
43  import org.apache.http.client.cache.HttpCacheUpdateCallback;
44  import org.apache.http.client.cache.HttpCacheUpdateException;
45  import org.apache.http.impl.client.cache.CacheConfig;
46  
47  /**
48   * <p>
49   * This class is a storage backend that uses an external <i>memcached</i>
50   * for storing cached origin responses. This storage option provides a
51   * couple of interesting advantages over the default in-memory storage
52   * backend:
53   * </p>
54   * <ol>
55   * <li>in-memory cached objects can survive an application restart since
56   * they are held in a separate process</li>
57   * <li>it becomes possible for several cooperating applications to share
58   * a large <i>memcached</i> farm together</li>
59   * </ol>
60   * <p>
61   * Note that in a shared memcached pool setting you may wish to make use
62   * of the Ketama consistent hashing algorithm to reduce the number of
63   * cache misses that might result if one of the memcached cluster members
64   * fails (see the <a href="http://dustin.github.com/java-memcached-client/apidocs/net/spy/memcached/KetamaConnectionFactory.html">
65   * KetamaConnectionFactory</a>).
66   * </p>
67   * <p>
68   * Because memcached places limits on the size of its keys, we need to
69   * introduce a key hashing scheme to map the annotated URLs the higher-level
70   * caching HTTP client wants to use as keys onto ones that are suitable
71   * for use with memcached. Please see {@link KeyHashingScheme} if you would
72   * like to use something other than the provided {@link SHA256KeyHashingScheme}.
73   * </p>
74   *
75   * <p>
76   * Because this hashing scheme can potentially result in key collisions (though
77   * highly unlikely), we need to store the higher-level logical storage key along
78   * with the {@link HttpCacheEntry} so that we can re-check it on retrieval. There
79   * is a default serialization scheme provided for this, although you can provide
80   * your own implementations of {@link MemcachedCacheEntry} and
81   * {@link MemcachedCacheEntryFactory} to customize this serialization.
82   * </p>
83   *
84   * <p>
85   * Please refer to the <a href="http://code.google.com/p/memcached/wiki/NewStart">
86   * memcached documentation</a> and in particular to the documentation for
87   * the <a href="http://code.google.com/p/spymemcached/">spymemcached
88   * documentation</a> for details about how to set up and configure memcached
89   * and the Java client used here, respectively.
90   * </p>
91   *
92   * @since 4.1
93   */
94  public class MemcachedHttpCacheStorage implements HttpCacheStorage {
95  
96      private static final Log log = LogFactory.getLog(MemcachedHttpCacheStorage.class);
97  
98      private final MemcachedClientIF client;
99      private final KeyHashingScheme keyHashingScheme;
100     private final MemcachedCacheEntryFactory memcachedCacheEntryFactory;
101     private final int maxUpdateRetries;
102 
103     /**
104      * Create a storage backend talking to a <i>memcached</i> instance
105      * listening on the specified host and port. This is useful if you
106      * just have a single local memcached instance running on the same
107      * machine as your application, for example.
108      * @param address where the <i>memcached</i> daemon is running
109      * @throws IOException in case of an error
110      */
111     public MemcachedHttpCacheStorage(final InetSocketAddress address) throws IOException {
112         this(new MemcachedClient(address));
113     }
114 
115     /**
116      * Create a storage backend using the pre-configured given
117      * <i>memcached</i> client.
118      * @param cache client to use for communicating with <i>memcached</i>
119      */
120     public MemcachedHttpCacheStorage(final MemcachedClientIF cache) {
121         this(cache, CacheConfig.DEFAULT, new MemcachedCacheEntryFactoryImpl(),
122                 new SHA256KeyHashingScheme());
123     }
124 
125     /**
126      * Create a storage backend using the given <i>memcached</i> client and
127      * applying the given cache configuration and cache entry serialization
128      * mechanism. <b>Deprecation note:</b> In the process of fixing a bug
129      * based on the need to hash logical storage keys onto memcached cache
130      * keys, the serialization process was revamped. This constructor still
131      * works, but the serializer argument will be ignored and default
132      * implementations of the new framework will be used. You can still
133      * provide custom serialization by using the
134      * {@link #MemcachedHttpCacheStorage(MemcachedClientIF, CacheConfig,
135      * MemcachedCacheEntryFactory, KeyHashingScheme)} constructor.
136      * @param client how to talk to <i>memcached</i>
137      * @param config apply HTTP cache-related options
138      * @param serializer <b>ignored</b>
139      *
140      * @deprecated (4.2) do not use
141      */
142     @Deprecated
143     public MemcachedHttpCacheStorage(final MemcachedClientIF client, final CacheConfig config,
144             final HttpCacheEntrySerializer serializer) {
145         this(client, config, new MemcachedCacheEntryFactoryImpl(),
146                 new SHA256KeyHashingScheme());
147     }
148 
149     /**
150      * Create a storage backend using the given <i>memcached</i> client and
151      * applying the given cache configuration, serialization, and hashing
152      * mechanisms.
153      * @param client how to talk to <i>memcached</i>
154      * @param config apply HTTP cache-related options
155      * @param memcachedCacheEntryFactory Factory pattern used for obtaining
156      *   instances of alternative cache entry serialization mechanisms
157      * @param keyHashingScheme how to map higher-level logical "storage keys"
158      *   onto "cache keys" suitable for use with memcached
159      */
160     public MemcachedHttpCacheStorage(final MemcachedClientIF client, final CacheConfig config,
161             final MemcachedCacheEntryFactory memcachedCacheEntryFactory,
162             final KeyHashingScheme keyHashingScheme) {
163         this.client = client;
164         this.maxUpdateRetries = config.getMaxUpdateRetries();
165         this.memcachedCacheEntryFactory = memcachedCacheEntryFactory;
166         this.keyHashingScheme = keyHashingScheme;
167     }
168 
169     @Override
170     public void putEntry(final String url, final HttpCacheEntry entry) throws IOException  {
171         final byte[] bytes = serializeEntry(url, entry);
172         final String key = getCacheKey(url);
173         if (key == null) {
174             return;
175         }
176         try {
177             client.set(key, 0, bytes);
178         } catch (final OperationTimeoutException ex) {
179             throw new MemcachedOperationTimeoutException(ex);
180         }
181     }
182 
183     private String getCacheKey(final String url) {
184         try {
185             return keyHashingScheme.hash(url);
186         } catch (final MemcachedKeyHashingException mkhe) {
187             return null;
188         }
189     }
190 
191     private byte[] serializeEntry(final String url, final HttpCacheEntry hce) throws IOException {
192         final MemcachedCacheEntry mce = memcachedCacheEntryFactory.getMemcachedCacheEntry(url, hce);
193         try {
194             return mce.toByteArray();
195         } catch (final MemcachedSerializationException mse) {
196             final IOException ioe = new IOException();
197             ioe.initCause(mse);
198             throw ioe;
199         }
200     }
201 
202     private byte[] convertToByteArray(final Object o) {
203         if (o == null) {
204             return null;
205         }
206         if (!(o instanceof byte[])) {
207             log.warn("got a non-bytearray back from memcached: " + o);
208             return null;
209         }
210         return (byte[])o;
211     }
212 
213     private MemcachedCacheEntry reconstituteEntry(final Object o) {
214         final byte[] bytes = convertToByteArray(o);
215         if (bytes == null) {
216             return null;
217         }
218         final MemcachedCacheEntry mce = memcachedCacheEntryFactory.getUnsetCacheEntry();
219         try {
220             mce.set(bytes);
221         } catch (final MemcachedSerializationException mse) {
222             return null;
223         }
224         return mce;
225     }
226 
227     @Override
228     public HttpCacheEntry getEntry(final String url) throws IOException {
229         final String key = getCacheKey(url);
230         if (key == null) {
231             return null;
232         }
233         try {
234             final MemcachedCacheEntry mce = reconstituteEntry(client.get(key));
235             if (mce == null || !url.equals(mce.getStorageKey())) {
236                 return null;
237             }
238             return mce.getHttpCacheEntry();
239         } catch (final OperationTimeoutException ex) {
240             throw new MemcachedOperationTimeoutException(ex);
241         }
242     }
243 
244     @Override
245     public void removeEntry(final String url) throws IOException {
246         final String key = getCacheKey(url);
247         if (key == null) {
248             return;
249         }
250         try {
251             client.delete(key);
252         } catch (final OperationTimeoutException ex) {
253             throw new MemcachedOperationTimeoutException(ex);
254         }
255     }
256 
257     @Override
258     public void updateEntry(final String url, final HttpCacheUpdateCallback callback)
259             throws HttpCacheUpdateException, IOException {
260         int numRetries = 0;
261         final String key = getCacheKey(url);
262         if (key == null) {
263             throw new HttpCacheUpdateException("couldn't generate cache key");
264         }
265         do {
266             try {
267                 final CASValue<Object> v = client.gets(key);
268                 MemcachedCacheEntry mce = (v == null) ? null
269                         : reconstituteEntry(v.getValue());
270                 if (mce != null && (!url.equals(mce.getStorageKey()))) {
271                     mce = null;
272                 }
273                 final HttpCacheEntry existingEntry = (mce == null) ? null
274                         : mce.getHttpCacheEntry();
275                 final HttpCacheEntry updatedEntry = callback.update(existingEntry);
276 
277                 if (existingEntry == null) {
278                     putEntry(url, updatedEntry);
279                     return;
280 
281                 } else {
282                     final byte[] updatedBytes = serializeEntry(url, updatedEntry);
283                     final CASResponse casResult = client.cas(key, v.getCas(),
284                             updatedBytes);
285                     if (casResult != CASResponse.OK) {
286                         numRetries++;
287                     } else {
288                         return;
289                     }
290                 }
291             } catch (final OperationTimeoutException ex) {
292                 throw new MemcachedOperationTimeoutException(ex);
293             }
294         } while (numRetries <= maxUpdateRetries);
295 
296         throw new HttpCacheUpdateException("Failed to update");
297     }
298 }