Skip to content

Commit 0f63d06

Browse files
committed
Fixes #1. Memcached key is too long.
1 parent 1ec2ea4 commit 0f63d06

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<parent>
2121
<groupId>org.mybatis</groupId>
2222
<artifactId>mybatis-parent</artifactId>
23-
<version>19-SNAPSHOT</version>
23+
<version>21</version>
2424
</parent>
2525

2626
<groupId>org.mybatis.caches</groupId>

src/main/java/org/mybatis/caches/memcached/MemcachedClientWrapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public MemcachedClientWrapper() {
5757
}
5858

5959
/**
60-
* Converts the iBatis object key in the proper string representation.
61-
*
62-
* @param key the iBatis object key.
60+
* Converts the MyBatis object key in the proper string representation.
61+
*
62+
* @param key the MyBatis object key.
6363
* @return the proper string representation.
6464
*/
6565
private String toKeyString(final Object key) {
66-
String keyString = configuration.getKeyPrefix() + key; // issue #660, key collision
66+
String keyString = configuration.getKeyPrefix() + StringUtils.sha1Hex(key.toString()); // issue #1, key too long
6767
if (log.isDebugEnabled()) {
6868
log.debug("Object key '"
6969
+ key
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.mybatis.caches.memcached;
2+
3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
5+
6+
/**
7+
* Got from https://github.com/raykrueger/hibernate-memcached
8+
*
9+
* @author Ray Krueger
10+
*/
11+
public class StringUtils {
12+
13+
private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
14+
15+
public static String join(Object[] array, String separator) {
16+
if (array == null) {
17+
return null;
18+
}
19+
int arraySize = array.length;
20+
StringBuilder buffer = new StringBuilder();
21+
22+
for (int i = 0; i < arraySize; i++) {
23+
if (i > 0) {
24+
buffer.append(separator);
25+
}
26+
if (array[i] != null) {
27+
buffer.append(array[i]);
28+
}
29+
}
30+
return buffer.toString();
31+
}
32+
33+
public static String md5Hex(String data) {
34+
if (data == null) {
35+
throw new IllegalArgumentException("data must not be null");
36+
}
37+
38+
byte[] bytes = digest("MD5", data);
39+
40+
return toHexString(bytes);
41+
}
42+
43+
public static String sha1Hex(String data) {
44+
if (data == null) {
45+
throw new IllegalArgumentException("data must not be null");
46+
}
47+
48+
byte[] bytes = digest("SHA1", data);
49+
50+
return toHexString(bytes);
51+
}
52+
53+
private static String toHexString(byte[] bytes) {
54+
int l = bytes.length;
55+
56+
char[] out = new char[l << 1];
57+
58+
for (int i = 0, j = 0; i < l; i++) {
59+
out[j++] = DIGITS[(0xF0 & bytes[i]) >>> 4];
60+
out[j++] = DIGITS[0x0F & bytes[i]];
61+
}
62+
63+
return new String(out);
64+
}
65+
66+
private static byte[] digest(String algorithm, String data) {
67+
MessageDigest digest;
68+
try {
69+
digest = MessageDigest.getInstance(algorithm);
70+
} catch (NoSuchAlgorithmException e) {
71+
throw new RuntimeException(e);
72+
}
73+
74+
return digest.digest(data.getBytes());
75+
}
76+
}

0 commit comments

Comments
 (0)