org.apache.hc.client5.http.impl.cache.memcached

## Class MemcachedHttpCacheStorage

- java.lang.Object
- - org.apache.hc.client5.http.impl.cache.AbstractSerializingCacheStorage<byte[],CAS>
  - - org.apache.hc.client5.http.impl.cache.AbstractBinaryCacheStorage<net.spy.memcached.CASValue<Object>>
    - - org.apache.hc.client5.http.impl.cache.memcached.MemcachedHttpCacheStorage

- All Implemented Interfaces:
    HttpCacheStorage

      public class MemcachedHttpCacheStorage
      extends AbstractBinaryCacheStorage<net.spy.memcached.CASValue<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:
    4.1

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `MemcachedHttpCacheStorage(InetSocketAddress address)` Create a storage backend talking to a memcached instance listening on the specified host and port.
    `MemcachedHttpCacheStorage(net.spy.memcached.MemcachedClient cache)` Create a storage backend using the pre-configured given memcached client.
    `MemcachedHttpCacheStorage(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.
    `MemcachedHttpCacheStorage(net.spy.memcached.MemcachedClientIF cache)` Create a storage backend using the pre-configured given memcached client.
    `MemcachedHttpCacheStorage(net.spy.memcached.MemcachedClientIF 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

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected Map<String,byte[]>` | `bulkRestore(Collection<String> storageKeys)`
    `protected void` | `delete(String storageKey)`
    `protected String` | `digestToStorageKey(String key)`
    `protected net.spy.memcached.CASValue<Object>` | `getForUpdateCAS(String storageKey)`
    `protected byte[]` | `getStorageObject(net.spy.memcached.CASValue<Object> casValue)`
    `protected byte[]` | `restore(String storageKey)`
    `protected void` | `store(String storageKey, byte[] storageObject)`
    `protected boolean` | `updateCAS(String storageKey, net.spy.memcached.CASValue<Object> casValue, byte[] storageObject)`

    - ### Methods inherited from class org.apache.hc.client5.http.impl.cache.AbstractSerializingCacheStorage

      `getEntries, getEntry, putEntry, removeEntry, updateEntry`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### MemcachedHttpCacheStorage

          public MemcachedHttpCacheStorage(InetSocketAddress address)
                                    throws 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:
        `IOException` - in case of an error

    - #### MemcachedHttpCacheStorage

          public MemcachedHttpCacheStorage(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

    - #### MemcachedHttpCacheStorage

          public MemcachedHttpCacheStorage(net.spy.memcached.MemcachedClientIF cache)

      Create a storage backend using the pre-configured given memcached client.
      Parameters:
        `cache` - client to use for communicating with memcached
      Since:
        5.2

    - #### MemcachedHttpCacheStorage

          public MemcachedHttpCacheStorage(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

    - #### MemcachedHttpCacheStorage

          public MemcachedHttpCacheStorage(net.spy.memcached.MemcachedClientIF 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
      Since:
        5.2

  - ### Method Detail

    - #### digestToStorageKey

          protected String digestToStorageKey(String key)

      Specified by:
        `digestToStorageKey` in class `AbstractSerializingCacheStorage<byte[],net.spy.memcached.CASValue<Object>>`

    - #### store

          protected void store(String storageKey,
                               byte[] storageObject)
                        throws ResourceIOException

      Specified by:
        `store` in class `AbstractSerializingCacheStorage<byte[],net.spy.memcached.CASValue<Object>>`
      Throws:
        `ResourceIOException`

    - #### restore

          protected byte[] restore(String storageKey)
                            throws ResourceIOException

      Specified by:
        `restore` in class `AbstractSerializingCacheStorage<byte[],net.spy.memcached.CASValue<Object>>`
      Throws:
        `ResourceIOException`

    - #### getForUpdateCAS

          protected net.spy.memcached.CASValue<Object> getForUpdateCAS(String storageKey)
                                                                throws ResourceIOException

      Specified by:
        `getForUpdateCAS` in class `AbstractSerializingCacheStorage<byte[],net.spy.memcached.CASValue<Object>>`
      Throws:
        `ResourceIOException`

    - #### getStorageObject

          protected byte[] getStorageObject(net.spy.memcached.CASValue<Object> casValue)
                                     throws ResourceIOException

      Specified by:
        `getStorageObject` in class `AbstractSerializingCacheStorage<byte[],net.spy.memcached.CASValue<Object>>`
      Throws:
        `ResourceIOException`

    - #### updateCAS

          protected boolean updateCAS(String storageKey,
                                      net.spy.memcached.CASValue<Object> casValue,
                                      byte[] storageObject)
                               throws ResourceIOException

      Specified by:
        `updateCAS` in class `AbstractSerializingCacheStorage<byte[],net.spy.memcached.CASValue<Object>>`
      Throws:
        `ResourceIOException`

    - #### delete

          protected void delete(String storageKey)
                         throws ResourceIOException

      Specified by:
        `delete` in class `AbstractSerializingCacheStorage<byte[],net.spy.memcached.CASValue<Object>>`
      Throws:
        `ResourceIOException`

    - #### bulkRestore

          protected Map<String,byte[]> bulkRestore(Collection<String> storageKeys)
                                            throws ResourceIOException

      Specified by:
        `bulkRestore` in class `AbstractSerializingCacheStorage<byte[],net.spy.memcached.CASValue<Object>>`
      Throws:
        `ResourceIOException`

Copyright © 1999–2021 The Apache Software Foundation. All rights reserved.