HTTPCLIENT-1154: MemcachedHttpCacheStorage should allow client
to specify custom prefix string for keys. This is implemented by providing a decorator KeyHashingScheme that adds the prefix. git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1232588 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d47c30baae
commit
cac1432472
|
@ -1,5 +1,13 @@
|
|||
Changes since 4.2 ALPHA1
|
||||
-------------------
|
||||
* [HTTPCLIENT-1154] MemcachedHttpCacheStorage should allow client to
|
||||
specify custom prefix string for keys.
|
||||
Contributed by Jon Moore <jonm at apache dot org>.
|
||||
|
||||
* [HTTPCLIENT-1153] MemcachedHttpCacheStorage uses URL as cache key;
|
||||
shouldn't due to fixed maximum-length memcached keys.
|
||||
Contributed by Jon Moore <jonm at apache dot org>.
|
||||
|
||||
* [HTTPCLIENT-1157] MemcachedHttpCacheStroage should throw IOExceptions
|
||||
instead of RuntimeExceptions.
|
||||
Contributed by James Miller <jamesmiller01 at gmail dot com>.
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package org.apache.http.impl.client.cache.memcached;
|
||||
|
||||
import org.apache.http.client.cache.memcached.KeyHashingScheme;
|
||||
|
||||
/**
|
||||
* This is a {@link KeyHashingScheme} decorator that simply adds
|
||||
* a known prefix to the results of another <code>KeyHashingScheme</code>.
|
||||
* Primarily useful for namespacing a shared memcached cluster, for
|
||||
* example.
|
||||
*/
|
||||
public class PrefixKeyHashingScheme implements KeyHashingScheme {
|
||||
|
||||
private String prefix;
|
||||
private KeyHashingScheme backingScheme;
|
||||
|
||||
/**
|
||||
* Creates a new {@link KeyHashingScheme} that prepends the given
|
||||
* prefix to the results of hashes from the given backing scheme.
|
||||
* Users should be aware that memcached has a fixed maximum key
|
||||
* length, so the combination of this prefix plus the results of
|
||||
* the backing hashing scheme must still fit within these limits.
|
||||
* @param prefix
|
||||
* @param backingScheme
|
||||
*/
|
||||
public PrefixKeyHashingScheme(String prefix, KeyHashingScheme backingScheme) {
|
||||
this.prefix = prefix;
|
||||
this.backingScheme = backingScheme;
|
||||
}
|
||||
|
||||
public String hash(String storageKey) {
|
||||
return prefix + backingScheme.hash(storageKey);
|
||||
}
|
||||
|
||||
}
|
|
@ -37,7 +37,9 @@ import org.apache.http.client.cache.memcached.KeyHashingScheme;
|
|||
/**
|
||||
* This is a {@link KeyHashingScheme} based on the
|
||||
* <a href="http://en.wikipedia.org/wiki/SHA-2">SHA-256</a>
|
||||
* algorithm.
|
||||
* algorithm. The hashes produced are hex-encoded SHA-256
|
||||
* digests and hence are always 64-character hexadecimal
|
||||
* strings.
|
||||
*/
|
||||
public class SHA256KeyHashingScheme implements KeyHashingScheme {
|
||||
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package org.apache.http.impl.client.cache.memcached;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.apache.http.client.cache.memcached.KeyHashingScheme;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class TestPrefixKeyHashingScheme {
|
||||
|
||||
private static final String KEY = "key";
|
||||
private static final String PREFIX = "prefix";
|
||||
private PrefixKeyHashingScheme impl;
|
||||
private KeyHashingScheme scheme;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
scheme = new KeyHashingScheme() {
|
||||
public String hash(String storageKey) {
|
||||
assertEquals(KEY, storageKey);
|
||||
return "hash";
|
||||
}
|
||||
};
|
||||
impl = new PrefixKeyHashingScheme(PREFIX, scheme);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addsPrefixToBackingScheme() {
|
||||
assertEquals("prefixhash", impl.hash(KEY));
|
||||
}
|
||||
}
|
|
@ -13,4 +13,5 @@ public class TestSHA256HashingScheme {
|
|||
String result = impl.hash("hello, hashing world");
|
||||
assertTrue(result != null && result.length() > 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue