org.greencheek.caching.herdcache.memcached.keyhashing.MessageDigestHashing Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of herdcache Show documentation
Show all versions of herdcache Show documentation
A cache that uses futures to prevent thundering herds to your backend service
package org.greencheek.caching.herdcache.memcached.keyhashing;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ArrayBlockingQueue;
/**
* Created by dominictootell on 09/04/2014.
*/
public class MessageDigestHashing implements KeyHashing {
private final boolean upperCase;
private final ToHexString byteToHexStringConverter;
private final ArrayBlockingQueue digesters;
public MessageDigestHashing() {
this(KeyHashing.MD5);
}
public MessageDigestHashing(String algorithm) {
this(algorithm, Runtime.getRuntime().availableProcessors()*2);
}
public MessageDigestHashing(String algorithm,int messageDigests) {
this(algorithm,messageDigests,true);
}
public MessageDigestHashing(String algorithm,int messageDigests,boolean toUpper) {
this.upperCase = toUpper;
if(upperCase) {
byteToHexStringConverter = UpperCaseToHexString.INSTANCE;
} else {
byteToHexStringConverter = LowerCaseToHexString.INSTANCE;
}
try {
digesters = createDigests(messageDigests, algorithm);
} catch (NoSuchAlgorithmException e) {
throw new InstantiationError("Unable to create MessageDigest for algo: " + algorithm);
}
}
private final ArrayBlockingQueue createDigests(int numToCreate, String algorithm)
throws NoSuchAlgorithmException {
ArrayBlockingQueue queue = new ArrayBlockingQueue(numToCreate);
for(int i = 0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy