Code example for Cache

Methods: getIfPresentput

0
    public static final Format DECIMAL_FORMAT = new DecimalFormat("0.00");
    private static final Cache<String, Integer> shardCache = CacheBuilder.newBuilder().expireAfterAccess(10,
            TimeUnit.MINUTES).concurrencyLevel(30).build();
 
    public static Integer getShard(String s) {
        Integer shard = shardCache.getIfPresent(s);
        if (shard == null) {
            shard = computeShard(s);
            shardCache.put(s, shard);
        } 
        return shard;
    } 
 
    public static int computeShard(String s) {
        return (int)Long.parseLong(DigestUtils.md5Hex(s).substring(30), 16) % Constants.NUMBER_OF_SHARDS;
    } 
     
    public static Collection<Integer> parseShards(String s) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        if ("ALL".equalsIgnoreCase(s)) {