Class MemcachedHttpAsyncCacheStorage

  • All Implemented Interfaces:
    HttpAsyncCacheStorage

    public class MemcachedHttpAsyncCacheStorage
    extends AbstractBinaryAsyncCacheStorage<net.spy.memcached.CASValue<java.lang.Object>>

    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:

    1. in-memory cached objects can survive an application restart since they are held in a separate process
    2. 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
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected org.apache.hc.core5.concurrent.Cancellable bulkRestore​(java.util.Collection<java.lang.String> storageKeys, org.apache.hc.core5.concurrent.FutureCallback<java.util.Map<java.lang.String,​byte[]>> callback)  
      private byte[] castAsByteArray​(java.lang.Object storageObject)  
      protected org.apache.hc.core5.concurrent.Cancellable delete​(java.lang.String storageKey, org.apache.hc.core5.concurrent.FutureCallback<java.lang.Boolean> callback)  
      protected java.lang.String digestToStorageKey​(java.lang.String key)  
      protected org.apache.hc.core5.concurrent.Cancellable getForUpdateCAS​(java.lang.String storageKey, org.apache.hc.core5.concurrent.FutureCallback<net.spy.memcached.CASValue<java.lang.Object>> callback)  
      protected byte[] getStorageObject​(net.spy.memcached.CASValue<java.lang.Object> casValue)  
      private <T> org.apache.hc.core5.concurrent.Cancellable operation​(net.spy.memcached.internal.OperationFuture<T> operationFuture, org.apache.hc.core5.concurrent.FutureCallback<T> callback)  
      protected org.apache.hc.core5.concurrent.Cancellable restore​(java.lang.String storageKey, org.apache.hc.core5.concurrent.FutureCallback<byte[]> callback)  
      protected org.apache.hc.core5.concurrent.Cancellable store​(java.lang.String storageKey, byte[] storageObject, org.apache.hc.core5.concurrent.FutureCallback<java.lang.Boolean> callback)  
      protected org.apache.hc.core5.concurrent.Cancellable updateCAS​(java.lang.String storageKey, net.spy.memcached.CASValue<java.lang.Object> casValue, byte[] storageObject, org.apache.hc.core5.concurrent.FutureCallback<java.lang.Boolean> callback)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • client

        private final net.spy.memcached.MemcachedClient client
    • Constructor Detail

      • MemcachedHttpAsyncCacheStorage

        public MemcachedHttpAsyncCacheStorage​(java.net.InetSocketAddress address)
                                       throws java.io.IOException
        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:
        java.io.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 memcached
        config - apply HTTP cache-related options
        serializer - alternative serialization mechanism
        keyHashingScheme - how to map higher-level logical "storage keys" onto "cache keys" suitable for use with memcached
    • Method Detail

      • 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​(java.lang.String storageKey,
                                                                   byte[] storageObject,
                                                                   org.apache.hc.core5.concurrent.FutureCallback<java.lang.Boolean> callback)
        Specified by:
        store in class AbstractSerializingAsyncCacheStorage<byte[],​net.spy.memcached.CASValue<java.lang.Object>>
      • restore

        protected org.apache.hc.core5.concurrent.Cancellable restore​(java.lang.String storageKey,
                                                                     org.apache.hc.core5.concurrent.FutureCallback<byte[]> callback)
        Specified by:
        restore in class AbstractSerializingAsyncCacheStorage<byte[],​net.spy.memcached.CASValue<java.lang.Object>>
      • getForUpdateCAS

        protected org.apache.hc.core5.concurrent.Cancellable getForUpdateCAS​(java.lang.String storageKey,
                                                                             org.apache.hc.core5.concurrent.FutureCallback<net.spy.memcached.CASValue<java.lang.Object>> callback)
        Specified by:
        getForUpdateCAS in class AbstractSerializingAsyncCacheStorage<byte[],​net.spy.memcached.CASValue<java.lang.Object>>
      • updateCAS

        protected org.apache.hc.core5.concurrent.Cancellable updateCAS​(java.lang.String storageKey,
                                                                       net.spy.memcached.CASValue<java.lang.Object> casValue,
                                                                       byte[] storageObject,
                                                                       org.apache.hc.core5.concurrent.FutureCallback<java.lang.Boolean> callback)
        Specified by:
        updateCAS in class AbstractSerializingAsyncCacheStorage<byte[],​net.spy.memcached.CASValue<java.lang.Object>>
      • delete

        protected org.apache.hc.core5.concurrent.Cancellable delete​(java.lang.String storageKey,
                                                                    org.apache.hc.core5.concurrent.FutureCallback<java.lang.Boolean> callback)
        Specified by:
        delete in class AbstractSerializingAsyncCacheStorage<byte[],​net.spy.memcached.CASValue<java.lang.Object>>
      • bulkRestore

        protected org.apache.hc.core5.concurrent.Cancellable bulkRestore​(java.util.Collection<java.lang.String> storageKeys,
                                                                         org.apache.hc.core5.concurrent.FutureCallback<java.util.Map<java.lang.String,​byte[]>> callback)
        Specified by:
        bulkRestore in class AbstractSerializingAsyncCacheStorage<byte[],​net.spy.memcached.CASValue<java.lang.Object>>