public class MemcachedHttpCacheStorage extends java.lang.Object implements HttpCacheStorage
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:
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
.
Because this hashing scheme can potentially result in key collisions (though
highly unlikely), we need to store the higher-level logical storage key along
with the HttpCacheEntry
so that we can re-check it on retrieval. There
is a default serialization scheme provided for this, although you can provide
your own implementations of MemcachedCacheEntry
and
MemcachedCacheEntryFactory
to customize this serialization.
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.
Modifier and Type | Field and Description |
---|---|
private net.spy.memcached.MemcachedClientIF |
client |
private KeyHashingScheme |
keyHashingScheme |
private static org.apache.commons.logging.Log |
log |
private int |
maxUpdateRetries |
private MemcachedCacheEntryFactory |
memcachedCacheEntryFactory |
Constructor and Description |
---|
MemcachedHttpCacheStorage(java.net.InetSocketAddress address)
Create a storage backend talking to a memcached instance
listening on the specified host and port.
|
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 serializer)
Deprecated.
(4.2) do not use
|
MemcachedHttpCacheStorage(net.spy.memcached.MemcachedClientIF client,
CacheConfig config,
MemcachedCacheEntryFactory memcachedCacheEntryFactory,
KeyHashingScheme keyHashingScheme)
Create a storage backend using the given memcached client and
applying the given cache configuration, serialization, and hashing
mechanisms.
|
Modifier and Type | Method and Description |
---|---|
private byte[] |
convertToByteArray(java.lang.Object o) |
private java.lang.String |
getCacheKey(java.lang.String url) |
HttpCacheEntry |
getEntry(java.lang.String url)
Retrieves the cache entry stored under the given key
or null if no entry exists under that key.
|
void |
putEntry(java.lang.String url,
HttpCacheEntry entry)
Store a given cache entry under the given key.
|
private MemcachedCacheEntry |
reconstituteEntry(java.lang.Object o) |
void |
removeEntry(java.lang.String url)
Deletes/invalidates/removes any cache entries currently
stored under the given key.
|
private byte[] |
serializeEntry(java.lang.String url,
HttpCacheEntry hce) |
void |
updateEntry(java.lang.String url,
HttpCacheUpdateCallback callback)
Atomically applies the given callback to update an existing cache
entry under a given key.
|
private static final org.apache.commons.logging.Log log
private final net.spy.memcached.MemcachedClientIF client
private final KeyHashingScheme keyHashingScheme
private final MemcachedCacheEntryFactory memcachedCacheEntryFactory
private final int maxUpdateRetries
public MemcachedHttpCacheStorage(java.net.InetSocketAddress address) throws java.io.IOException
address
- where the memcached daemon is runningjava.io.IOException
- in case of an errorpublic MemcachedHttpCacheStorage(net.spy.memcached.MemcachedClientIF cache)
cache
- client to use for communicating with memcached@Deprecated public MemcachedHttpCacheStorage(net.spy.memcached.MemcachedClientIF client, CacheConfig config, HttpCacheEntrySerializer serializer)
MemcachedHttpCacheStorage(MemcachedClientIF, CacheConfig,
MemcachedCacheEntryFactory, KeyHashingScheme)
constructor.client
- how to talk to memcachedconfig
- apply HTTP cache-related optionsserializer
- ignoredpublic MemcachedHttpCacheStorage(net.spy.memcached.MemcachedClientIF client, CacheConfig config, MemcachedCacheEntryFactory memcachedCacheEntryFactory, KeyHashingScheme keyHashingScheme)
client
- how to talk to memcachedconfig
- apply HTTP cache-related optionsmemcachedCacheEntryFactory
- Factory pattern used for obtaining
instances of alternative cache entry serialization mechanismskeyHashingScheme
- how to map higher-level logical "storage keys"
onto "cache keys" suitable for use with memcachedpublic void putEntry(java.lang.String url, HttpCacheEntry entry) throws java.io.IOException
HttpCacheStorage
putEntry
in interface HttpCacheStorage
url
- where in the cache to store the entryentry
- cached response to storejava.io.IOException
private java.lang.String getCacheKey(java.lang.String url)
private byte[] serializeEntry(java.lang.String url, HttpCacheEntry hce) throws java.io.IOException
java.io.IOException
private byte[] convertToByteArray(java.lang.Object o)
private MemcachedCacheEntry reconstituteEntry(java.lang.Object o)
public HttpCacheEntry getEntry(java.lang.String url) throws java.io.IOException
HttpCacheStorage
getEntry
in interface HttpCacheStorage
url
- cache keyHttpCacheEntry
or null
if no
entry existsjava.io.IOException
public void removeEntry(java.lang.String url) throws java.io.IOException
HttpCacheStorage
removeEntry
in interface HttpCacheStorage
java.io.IOException
public void updateEntry(java.lang.String url, HttpCacheUpdateCallback callback) throws HttpCacheUpdateException, java.io.IOException
HttpCacheStorage
updateEntry
in interface HttpCacheStorage
url
- indicates which entry to modifycallback
- performs the update; see
HttpCacheUpdateCallback
for details, but roughly the
callback expects to be handed the current entry and will return
the new value for the entry.HttpCacheUpdateException
java.io.IOException