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