提交 8bd8ac73 编写于 作者: S Sam Judd

Fix safe key caching.

上级 be180a0e
...@@ -9,10 +9,13 @@ import java.security.MessageDigest; ...@@ -9,10 +9,13 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
public class SafeKeyGenerator { public class SafeKeyGenerator {
private final LruCache<Key, String> loadIdToSafeHash = new LruCache<Key, String>(250); private final LruCache<Key, String> loadIdToSafeHash = new LruCache<Key, String>(1000);
public String getSafeKey(Key key) { public String getSafeKey(Key key) {
String safeKey = loadIdToSafeHash.get(key); String safeKey;
synchronized (loadIdToSafeHash) {
safeKey = loadIdToSafeHash.get(key);
}
if (safeKey == null) { if (safeKey == null) {
try { try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
...@@ -23,6 +26,9 @@ public class SafeKeyGenerator { ...@@ -23,6 +26,9 @@ public class SafeKeyGenerator {
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }
synchronized (loadIdToSafeHash) {
loadIdToSafeHash.put(key, safeKey);
}
} }
return safeKey; return safeKey;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册