Class MemcachedHttpAsyncCacheStorage
- All Implemented Interfaces:
HttpAsyncCacheStorage
This class is a storage backend that uses an external memcached for storing cached origin responses. This storage option provides a couple of interesting advantages over the default in-memory storage backend:
- in-memory cached objects can survive an application restart since they are held in a separate process
- it becomes possible for several cooperating applications to share a large memcached farm together
Note that in a shared memcached pool setting you may wish to make use of the Ketama consistent hashing algorithm to reduce the number of cache misses that might result if one of the memcached cluster members fails (see the KetamaConnectionFactory).
Because memcached places limits on the size of its keys, we need to
introduce a key hashing scheme to map the annotated URLs the higher-level
caching HTTP client wants to use as keys onto ones that are suitable
for use with memcached. Please see KeyHashingScheme if you would
like to use something other than the provided SHA256KeyHashingScheme.
Please refer to the memcached documentation and in particular to the documentation for the spymemcached documentation for details about how to set up and configure memcached and the Java client used here, respectively.
- Since:
- 5.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final net.spy.memcached.MemcachedClientprivate final KeyHashingScheme -
Constructor Summary
ConstructorsConstructorDescriptionCreate a storage backend talking to a memcached instance listening on the specified host and port.MemcachedHttpAsyncCacheStorage(net.spy.memcached.MemcachedClient cache) Create a storage backend using the pre-configured given memcached client.MemcachedHttpAsyncCacheStorage(net.spy.memcached.MemcachedClient client, CacheConfig config, HttpCacheEntrySerializer<byte[]> serializer, KeyHashingScheme keyHashingScheme) Create a storage backend using the given memcached client and applying the given cache configuration, serialization, and hashing mechanisms. -
Method Summary
Modifier and TypeMethodDescriptionprotected org.apache.hc.core5.concurrent.CancellablebulkRestore(Collection<String> storageKeys, org.apache.hc.core5.concurrent.FutureCallback<Map<String, byte[]>> callback) private byte[]castAsByteArray(Object storageObject) protected org.apache.hc.core5.concurrent.Cancellableprotected StringdigestToStorageKey(String key) protected org.apache.hc.core5.concurrent.CancellablegetForUpdateCAS(String storageKey, org.apache.hc.core5.concurrent.FutureCallback<net.spy.memcached.CASValue<Object>> callback) protected byte[]getStorageObject(net.spy.memcached.CASValue<Object> casValue) private <T> org.apache.hc.core5.concurrent.Cancellableoperation(net.spy.memcached.internal.OperationFuture<T> operationFuture, org.apache.hc.core5.concurrent.FutureCallback<T> callback) protected org.apache.hc.core5.concurrent.Cancellableprotected org.apache.hc.core5.concurrent.Cancellablestore(String storageKey, byte[] storageObject, org.apache.hc.core5.concurrent.FutureCallback<Boolean> callback) protected org.apache.hc.core5.concurrent.CancellableupdateCAS(String storageKey, net.spy.memcached.CASValue<Object> casValue, byte[] storageObject, org.apache.hc.core5.concurrent.FutureCallback<Boolean> callback) Methods inherited from class org.apache.hc.client5.http.impl.cache.AbstractSerializingAsyncCacheStorage
getEntries, getEntry, putEntry, removeEntry, updateEntry
-
Field Details
-
client
private final net.spy.memcached.MemcachedClient client -
keyHashingScheme
-
-
Constructor Details
-
MemcachedHttpAsyncCacheStorage
Create a storage backend talking to a memcached instance listening on the specified host and port. This is useful if you just have a single local memcached instance running on the same machine as your application, for example.- Parameters:
address- where the memcached daemon is running- Throws:
IOException- in case of an error
-
MemcachedHttpAsyncCacheStorage
public MemcachedHttpAsyncCacheStorage(net.spy.memcached.MemcachedClient cache) Create a storage backend using the pre-configured given memcached client.- Parameters:
cache- client to use for communicating with memcached
-
MemcachedHttpAsyncCacheStorage
public MemcachedHttpAsyncCacheStorage(net.spy.memcached.MemcachedClient client, CacheConfig config, HttpCacheEntrySerializer<byte[]> serializer, KeyHashingScheme keyHashingScheme) Create a storage backend using the given memcached client and applying the given cache configuration, serialization, and hashing mechanisms.- Parameters:
client- how to talk to memcachedconfig- apply HTTP cache-related optionsserializer- alternative serialization mechanismkeyHashingScheme- how to map higher-level logical "storage keys" onto "cache keys" suitable for use with memcached
-
-
Method Details
-
digestToStorageKey
- Specified by:
digestToStorageKeyin classAbstractSerializingAsyncCacheStorage<byte[],net.spy.memcached.CASValue<Object>>
-
castAsByteArray
- Throws:
ResourceIOException
-
getStorageObject
protected byte[] getStorageObject(net.spy.memcached.CASValue<Object> casValue) throws ResourceIOException - Specified by:
getStorageObjectin classAbstractSerializingAsyncCacheStorage<byte[],net.spy.memcached.CASValue<Object>> - Throws:
ResourceIOException
-
operation
private <T> org.apache.hc.core5.concurrent.Cancellable operation(net.spy.memcached.internal.OperationFuture<T> operationFuture, org.apache.hc.core5.concurrent.FutureCallback<T> callback) -
store
protected org.apache.hc.core5.concurrent.Cancellable store(String storageKey, byte[] storageObject, org.apache.hc.core5.concurrent.FutureCallback<Boolean> callback) - Specified by:
storein classAbstractSerializingAsyncCacheStorage<byte[],net.spy.memcached.CASValue<Object>>
-
restore
protected org.apache.hc.core5.concurrent.Cancellable restore(String storageKey, org.apache.hc.core5.concurrent.FutureCallback<byte[]> callback) - Specified by:
restorein classAbstractSerializingAsyncCacheStorage<byte[],net.spy.memcached.CASValue<Object>>
-
getForUpdateCAS
protected org.apache.hc.core5.concurrent.Cancellable getForUpdateCAS(String storageKey, org.apache.hc.core5.concurrent.FutureCallback<net.spy.memcached.CASValue<Object>> callback) - Specified by:
getForUpdateCASin classAbstractSerializingAsyncCacheStorage<byte[],net.spy.memcached.CASValue<Object>>
-
updateCAS
protected org.apache.hc.core5.concurrent.Cancellable updateCAS(String storageKey, net.spy.memcached.CASValue<Object> casValue, byte[] storageObject, org.apache.hc.core5.concurrent.FutureCallback<Boolean> callback) - Specified by:
updateCASin classAbstractSerializingAsyncCacheStorage<byte[],net.spy.memcached.CASValue<Object>>
-
delete
protected org.apache.hc.core5.concurrent.Cancellable delete(String storageKey, org.apache.hc.core5.concurrent.FutureCallback<Boolean> callback) - Specified by:
deletein classAbstractSerializingAsyncCacheStorage<byte[],net.spy.memcached.CASValue<Object>>
-
bulkRestore
protected org.apache.hc.core5.concurrent.Cancellable bulkRestore(Collection<String> storageKeys, org.apache.hc.core5.concurrent.FutureCallback<Map<String, byte[]>> callback) - Specified by:
bulkRestorein classAbstractSerializingAsyncCacheStorage<byte[],net.spy.memcached.CASValue<Object>>
-