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.hc.client5.http.impl.cache.memcached;
28  
29  import java.io.IOException;
30  import java.net.InetSocketAddress;
31  import java.util.Collection;
32  import java.util.HashMap;
33  import java.util.Map;
34  import java.util.concurrent.CancellationException;
35  
36  import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer;
37  import org.apache.hc.client5.http.cache.ResourceIOException;
38  import org.apache.hc.client5.http.impl.cache.AbstractBinaryCacheStorage;
39  import org.apache.hc.client5.http.impl.cache.CacheConfig;
40  import org.apache.hc.client5.http.impl.cache.HttpByteArrayCacheEntrySerializer;
41  import org.apache.hc.core5.util.Args;
42  
43  import net.spy.memcached.CASResponse;
44  import net.spy.memcached.CASValue;
45  import net.spy.memcached.MemcachedClient;
46  import net.spy.memcached.MemcachedClientIF;
47  import net.spy.memcached.OperationTimeoutException;
48  
49  /**
50   * <p>
51   * This class is a storage backend that uses an external <i>memcached</i>
52   * for storing cached origin responses. This storage option provides a
53   * couple of interesting advantages over the default in-memory storage
54   * backend:
55   * </p>
56   * <ol>
57   * <li>in-memory cached objects can survive an application restart since
58   * they are held in a separate process</li>
59   * <li>it becomes possible for several cooperating applications to share
60   * a large <i>memcached</i> farm together</li>
61   * </ol>
62   * <p>
63   * Note that in a shared memcached pool setting you may wish to make use
64   * of the Ketama consistent hashing algorithm to reduce the number of
65   * cache misses that might result if one of the memcached cluster members
66   * fails (see the <a href="http://dustin.github.com/java-memcached-client/apidocs/net/spy/memcached/KetamaConnectionFactory.html">
67   * KetamaConnectionFactory</a>).
68   * </p>
69   * <p>
70   * Because memcached places limits on the size of its keys, we need to
71   * introduce a key hashing scheme to map the annotated URLs the higher-level
72   * caching HTTP client wants to use as keys onto ones that are suitable
73   * for use with memcached. Please see {@link KeyHashingScheme} if you would
74   * like to use something other than the provided {@link SHA256KeyHashingScheme}.
75   * </p>
76   *
77   * <p>
78   * Please refer to the <a href="http://code.google.com/p/memcached/wiki/NewStart">
79   * memcached documentation</a> and in particular to the documentation for
80   * the <a href="http://code.google.com/p/spymemcached/">spymemcached
81   * documentation</a> for details about how to set up and configure memcached
82   * and the Java client used here, respectively.
83   * </p>
84   *
85   * @since 4.1
86   */
87  public class MemcachedHttpCacheStorage extends AbstractBinaryCacheStorage<CASValue<Object>> {
88  
89      private final MemcachedClientIF client;
90      private final KeyHashingScheme keyHashingScheme;
91  
92      /**
93       * Create a storage backend talking to a <i>memcached</i> instance
94       * listening on the specified host and port. This is useful if you
95       * just have a single local memcached instance running on the same
96       * machine as your application, for example.
97       * @param address where the <i>memcached</i> daemon is running
98       * @throws IOException in case of an error
99       */
100     public MemcachedHttpCacheStorage(final InetSocketAddress address) throws IOException {
101         this(new MemcachedClient(address));
102     }
103 
104     /**
105      * Create a storage backend using the pre-configured given
106      * <i>memcached</i> client.
107      * @param cache client to use for communicating with <i>memcached</i>
108      */
109     public MemcachedHttpCacheStorage(final MemcachedClient cache) {
110         this(cache, CacheConfig.DEFAULT, HttpByteArrayCacheEntrySerializer.INSTANCE, SHA256KeyHashingScheme.INSTANCE);
111     }
112 
113     /**
114      * Create a storage backend using the pre-configured given
115      * <i>memcached</i> client.
116      *
117      * @param cache client to use for communicating with <i>memcached</i>
118      *
119      * @since 5.2
120      */
121     public MemcachedHttpCacheStorage(final MemcachedClientIF cache) {
122         this(cache, CacheConfig.DEFAULT, HttpByteArrayCacheEntrySerializer.INSTANCE, SHA256KeyHashingScheme.INSTANCE);
123     }
124 
125     /**
126      * Create a storage backend using the given <i>memcached</i> client and
127      * applying the given cache configuration, serialization, and hashing
128      * mechanisms.
129      * @param client how to talk to <i>memcached</i>
130      * @param config apply HTTP cache-related options
131      * @param serializer alternative serialization mechanism
132      * @param keyHashingScheme how to map higher-level logical "storage keys"
133      *   onto "cache keys" suitable for use with memcached
134      */
135     public MemcachedHttpCacheStorage(
136             final MemcachedClient client,
137             final CacheConfig config,
138             final HttpCacheEntrySerializer<byte[]> serializer,
139             final KeyHashingScheme keyHashingScheme) {
140         this((MemcachedClientIF) client, config, serializer, keyHashingScheme);
141     }
142 
143     /**
144      * Create a storage backend using the given <i>memcached</i> client and
145      * applying the given cache configuration, serialization, and hashing
146      * mechanisms.
147      *
148      * @param client           how to talk to <i>memcached</i>
149      * @param config           apply HTTP cache-related options
150      * @param serializer       alternative serialization mechanism
151      * @param keyHashingScheme how to map higher-level logical "storage keys"
152      *                         onto "cache keys" suitable for use with memcached
153      * @since 5.2
154      */
155     public MemcachedHttpCacheStorage(
156             final MemcachedClientIF client,
157             final CacheConfig config,
158             final HttpCacheEntrySerializer<byte[]> serializer,
159             final KeyHashingScheme keyHashingScheme) {
160         super((config != null ? config : CacheConfig.DEFAULT).getMaxUpdateRetries(),
161                 serializer != null ? serializer : HttpByteArrayCacheEntrySerializer.INSTANCE);
162         this.client = Args.notNull(client, "Memcached client");
163         this.keyHashingScheme = keyHashingScheme;
164     }
165 
166     @Override
167     protected String digestToStorageKey(final String key) {
168         return keyHashingScheme.hash(key);
169     }
170 
171     @Override
172     protected void store(final String storageKey, final byte[] storageObject) throws ResourceIOException {
173         try {
174             client.set(storageKey, 0, storageObject);
175         } catch (final CancellationException ex) {
176             throw new MemcachedOperationCancellationException(ex);
177         }
178     }
179 
180     private byte[] castAsByteArray(final Object storageObject) throws ResourceIOException {
181         if (storageObject == null) {
182             return null;
183         }
184         if (storageObject instanceof byte[]) {
185             return (byte[]) storageObject;
186         }
187         throw new ResourceIOException("Unexpected cache content: " + storageObject.getClass());
188     }
189 
190     @Override
191     protected byte[] restore(final String storageKey) throws ResourceIOException {
192         try {
193             return castAsByteArray(client.get(storageKey));
194         } catch (final CancellationException ex) {
195             throw new MemcachedOperationCancellationException(ex);
196         } catch (final OperationTimeoutException ex) {
197             throw new MemcachedOperationTimeoutException(ex);
198         }
199     }
200 
201     @Override
202     protected CASValue<Object> getForUpdateCAS(final String storageKey) throws ResourceIOException {
203         try {
204             return client.gets(storageKey);
205         } catch (final CancellationException ex) {
206             throw new MemcachedOperationCancellationException(ex);
207         } catch (final OperationTimeoutException ex) {
208             throw new MemcachedOperationTimeoutException(ex);
209         }
210     }
211 
212     @Override
213     protected byte[] getStorageObject(final CASValue<Object> casValue) throws ResourceIOException {
214         return castAsByteArray(casValue.getValue());
215     }
216 
217     @Override
218     protected boolean updateCAS(
219             final String storageKey, final CASValue<Object> casValue, final byte[] storageObject) throws ResourceIOException {
220         try {
221             final CASResponse casResult = client.cas(storageKey, casValue.getCas(), storageObject);
222             return casResult == CASResponse.OK;
223         } catch (final CancellationException ex) {
224             throw new MemcachedOperationCancellationException(ex);
225         } catch (final OperationTimeoutException ex) {
226             throw new MemcachedOperationTimeoutException(ex);
227         }
228     }
229 
230     @Override
231     protected void delete(final String storageKey) throws ResourceIOException {
232         try {
233             client.delete(storageKey);
234         } catch (final CancellationException ex) {
235             throw new MemcachedOperationCancellationException(ex);
236         }
237     }
238 
239     @Override
240     protected Map<String, byte[]> bulkRestore(final Collection<String> storageKeys) throws ResourceIOException {
241         try {
242             final Map<String, ?> storageObjectMap = client.getBulk(storageKeys);
243             final Map<String, byte[]> resultMap = new HashMap<>(storageObjectMap.size());
244             for (final Map.Entry<String, ?> resultEntry: storageObjectMap.entrySet()) {
245                 resultMap.put(resultEntry.getKey(), castAsByteArray(resultEntry.getValue()));
246             }
247             return resultMap;
248         } catch (final CancellationException ex) {
249             throw new MemcachedOperationCancellationException(ex);
250         } catch (final OperationTimeoutException ex) {
251             throw new MemcachedOperationTimeoutException(ex);
252         }
253     }
254 
255 }