Skip to content

Commit 70881ae

Browse files
committed
Brand new KeyStrategy work
Md5KeyStrategy and Sha1KeyStrategy now both work independently of the HashCodeKeyStrategy. StringKeyStrategy and HashCodeKeyStrategy will now both throw exceptions if the key is too long, no more auto truncated for people doing bad things.
1 parent 67dcd2a commit 70881ae

File tree

9 files changed

+54
-62
lines changed

9 files changed

+54
-62
lines changed

src/main/java/com/googlecode/hibernate/memcached/AbstractKeyStrategy.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,27 @@
1010
* KeyStrategy base class that handles concatenation, cleaning, and truncating the final cache key.
1111
* <p/>
1212
* Concatenates the three key components; regionName, clearIndex and key.<br/>
13-
* Subclasses are responsible for transforming the Key object into something identifyable.<br/>
14-
* If the key total length, including region and clearIndex, are greater than the maxKeyLength, the key's hashCode
15-
* will be used as the key. Subclasses can override this behavior.
13+
* Subclasses are responsible for transforming the Key object into something identifyable.
1614
*
1715
* @author Ray Krueger
1816
*/
1917
public abstract class AbstractKeyStrategy implements KeyStrategy {
2018

21-
public static final int DEFAULT_MAX_KEY_LENGTH = 250;
19+
public static final int MAX_KEY_LENGTH = 250;
2220

2321
protected final Logger log = LoggerFactory.getLogger(getClass());
2422

2523
private static final Pattern CLEAN_PATTERN = Pattern.compile("\\s");
2624

27-
private int maxKeyLength = DEFAULT_MAX_KEY_LENGTH;
28-
2925
public String toKey(String regionName, long clearIndex, Object key) {
3026
if (key == null) {
3127
throw new IllegalArgumentException("key must not be null");
3228
}
3329

3430
String keyString = concatenateKey(regionName, clearIndex, transformKeyObject(key));
3531

36-
if (keyString.length() > maxKeyLength) {
37-
return truncateKey(keyString);
32+
if (keyString.length() > MAX_KEY_LENGTH) {
33+
throw new IllegalArgumentException("Key is longer than " + MAX_KEY_LENGTH + " characters, try using the Sha1KeyStrategy: " + keyString);
3834
}
3935

4036
String finalKey = CLEAN_PATTERN.matcher(keyString).replaceAll("");
@@ -44,26 +40,6 @@ public String toKey(String regionName, long clearIndex, Object key) {
4440

4541
protected abstract String transformKeyObject(Object key);
4642

47-
protected String truncateKey(String key) {
48-
49-
String keyHashCode = StringUtils.md5Hex(key);
50-
51-
log.warn("Encoded key [{}] to md5 hash [{}]. " +
52-
"Be sure to set cache region names whenever possible as the names Hibernate generates are really long.",
53-
key, keyHashCode
54-
);
55-
56-
return keyHashCode;
57-
}
58-
59-
public int getMaxKeyLength() {
60-
return maxKeyLength;
61-
}
62-
63-
public void setMaxKeyLength(int maxKeyLength) {
64-
this.maxKeyLength = maxKeyLength;
65-
}
66-
6743
protected String concatenateKey(String regionName, long clearIndex, Object key) {
6844
return new StringBuilder()
6945
.append(regionName)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.googlecode.hibernate.memcached;
2+
3+
import com.googlecode.hibernate.memcached.utils.StringUtils;
4+
5+
/**
6+
* @author Ray Krueger
7+
*/
8+
public abstract class DigestKeyStrategy extends AbstractKeyStrategy {
9+
10+
protected String transformKeyObject(Object key) {
11+
return key.toString() + ":" + key.hashCode();
12+
}
13+
14+
protected String concatenateKey(String regionName, long clearIndex, Object key) {
15+
String longKey = super.concatenateKey(regionName, clearIndex, key);
16+
return digest(longKey);
17+
}
18+
19+
protected abstract String digest(String string);
20+
}

src/main/java/com/googlecode/hibernate/memcached/Md5KeyStrategy.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
/**
66
* @author Ray Krueger
77
*/
8-
public class Md5KeyStrategy extends HashCodeKeyStrategy {
9-
protected String concatenateKey(String regionName, long clearIndex, Object key) {
10-
String longKey = super.concatenateKey(regionName, clearIndex, key);
11-
return StringUtils.md5Hex(longKey);
12-
}
8+
public class Md5KeyStrategy extends DigestKeyStrategy {
9+
protected String digest(String key) {
10+
return StringUtils.md5Hex(key);
11+
}
1312
}

src/main/java/com/googlecode/hibernate/memcached/Sha1KeyStrategy.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
/**
66
* @author Ray Krueger
77
*/
8-
public class Sha1KeyStrategy extends HashCodeKeyStrategy {
9-
protected String concatenateKey(String regionName, long clearIndex, Object key) {
10-
String longKey = super.concatenateKey(regionName, clearIndex, key);
11-
return StringUtils.sha1Hex(longKey);
12-
}
13-
}
8+
public class Sha1KeyStrategy extends DigestKeyStrategy {
9+
protected String digest(String key) {
10+
return StringUtils.sha1Hex(key);
11+
}
12+
}

src/test/groovy/com/googlecode/hibernate/memcached/AbstractKeyStrategyTestCase.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ abstract class AbstractKeyStrategyTestCase extends BaseTestCase {
2626

2727
abstract KeyStrategy getKeyStrategy();
2828

29-
}
29+
}

src/test/groovy/com/googlecode/hibernate/memcached/HashCodeKeyStrategyTest.groovy

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ class HashCodeKeyStrategyTest extends AbstractKeyStrategyTestCase {
2727
assert_cache_key_equals "Ihavespaces:0:-2100783816", "I have spaces", 0, "so do I"
2828
}
2929

30-
void test_really_long_keys_get_truncated() {
30+
void test_really_long_key_throws_exception() {
3131
String regionName = ""
3232
250.times {regionName += "x"}
33-
assert_cache_key_equals "e2e82011e3d56dd6be564fdcb72a8d64", regionName, 0, "blah blah blah"
33+
shouldFail(IllegalArgumentException) {
34+
getKeyStrategy().toKey(regionName, 0, "blah blah blah")
35+
}
3436
}
3537

36-
}
38+
}

src/test/groovy/com/googlecode/hibernate/memcached/Md5KeyStrategyTest.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ class Md5KeyStrategyTest extends AbstractKeyStrategyTestCase {
1212
}
1313

1414
void test() {
15-
assert_cache_key_equals "dfbb1717f813ecccac747d5076e2a6d5", "test", 0, "boing"
15+
assert_cache_key_equals "a088ce3b48a12c8a8f26058240f4518d", "test", 0, "boing"
1616
}
1717

1818
void test_null_region() {
19-
assert_cache_key_equals "71b3dae5a0a8d765658a6c27bed071fd", null, 0, "boing"
19+
assert_cache_key_equals "cf23c7bb0c99979d4be1129adc959e6f", null, 0, "boing"
2020
}
2121

2222
void test_null_key_does_not_validate() {
2323
assert_null_key_does_not_validate()
2424
}
2525

2626
void test_spaces() {
27-
assert_cache_key_equals "23c5e5682b9a9fad5b30a95fae4ff299", "I have spaces", 0, "so do I"
27+
assert_cache_key_equals "0564810c2fd4e86dc6f355ad99e7d01b", "I have spaces", 0, "so do I"
2828
}
2929

3030
void test_really_long_keys_get_truncated() {
3131
String regionName = ""
3232
250.times {regionName += "x"}
33-
assert_cache_key_equals "e2e82011e3d56dd6be564fdcb72a8d64", regionName, 0, "blah blah blah"
33+
assert_cache_key_equals "16df3d87c2f8bde43fcdbb545be10626", regionName, 0, "blah blah blah"
3434
}
3535

36-
}
36+
}

src/test/groovy/com/googlecode/hibernate/memcached/Sha1KeyStrategyTest.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ class Sha1KeyStrategyTest extends AbstractKeyStrategyTestCase {
1212
}
1313

1414
void test() {
15-
assert_cache_key_equals "5c2adf57badcd5d923228b96dd1aee3bf0d5bf2c", "test", 0, "boing"
15+
assert_cache_key_equals "cd23e26dd7ab1d052e1c0a04daa27a03f6cd5d1c", "test", 0, "boing"
1616
}
1717

1818
void test_null_region() {
19-
assert_cache_key_equals "a0e96499b9522edc2807f4189e1cfdd65a4dad0d", null, 0, "boing"
19+
assert_cache_key_equals "6afcec5614479d46a1ec6d73dabbc2cea154da3c", null, 0, "boing"
2020
}
2121

2222
void test_null_key_does_not_validate() {
2323
assert_null_key_does_not_validate()
2424
}
2525

2626
void test_spaces() {
27-
assert_cache_key_equals "3344a9dadb9f405a39924d593592be1bf400e978", "I have spaces", 0, "so do I"
27+
assert_cache_key_equals "949b2a6fce917d85bd56e6197c93b3affa694e50", "I have spaces", 0, "so do I"
2828
}
2929

3030
void test_really_long_keys_get_truncated() {
3131
String regionName = ""
3232
250.times {regionName += "x"}
33-
assert_cache_key_equals "3c64cd962343bc26ea73c78ba59eeed88491f439", regionName, 0, "blah blah blah"
33+
assert_cache_key_equals "7f00c6faf1fefaf62cabb512285cc60ce641d5c8", regionName, 0, "blah blah blah"
3434
}
3535

36-
}
36+
}

src/test/groovy/com/googlecode/hibernate/memcached/StringKeyStrategyTest.groovy

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ class StringKeyStrategyTest extends AbstractKeyStrategyTestCase {
1515
assert_cache_key_equals "test:0:boing", "test", 0, "boing"
1616
}
1717

18-
void test_config() {
19-
assertEquals(StringKeyStrategy.DEFAULT_MAX_KEY_LENGTH, strategy.maxKeyLength)
20-
strategy.maxKeyLength = 1
21-
assertEquals(1, strategy.maxKeyLength)
22-
}
23-
2418
void test_null_region() {
2519
assert_cache_key_equals "null:0:boing", null, 0, "boing"
2620
}
@@ -33,10 +27,12 @@ class StringKeyStrategyTest extends AbstractKeyStrategyTestCase {
3327
assert_cache_key_equals "Ihavespaces:0:sodoI", "I have spaces", 0, "so do I"
3428
}
3529

36-
void test_really_long_keys_get_truncated() {
30+
void test_really_long_key_throws_exception() {
3731
String regionName = ""
3832
250.times {regionName += "x"}
39-
assert_cache_key_equals "fe009b44a903277f4b8e07f2cb03e96f", regionName, 0, "blah blah blah"
33+
shouldFail(IllegalArgumentException) {
34+
getKeyStrategy().toKey(regionName, 0, "blah blah blah")
35+
}
4036
}
4137

42-
}
38+
}

0 commit comments

Comments
 (0)