All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.greencheek.caching.herdcache.memcached.keyhashing.MessageDigestHashing Maven / Gradle / Ivy

Go to download

A cache that uses futures to prevent thundering herds to your backend service

There is a newer version: 2.0.19
Show newest version
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