Commit aea678c1 authored by lizhiheng's avatar lizhiheng

feat:新增方法(对目标字符串中对第一个数字进行取模从而获取到分片key)

parent 3b0342a7
package cn.com.duiba.spring.boot.starter.dsp.util; package cn.com.duiba.spring.boot.starter.dsp.util;
import cn.com.duiba.spring.boot.starter.dsp.enums.RedisBalance8KeyEnum; import cn.com.duiba.spring.boot.starter.dsp.enums.RedisBalance8KeyEnum;
import com.google.common.base.Stopwatch;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.*;
import java.util.List; import java.util.concurrent.TimeUnit;
import java.util.Objects; import java.util.stream.IntStream;
/** /**
* @author jiangyangyang * @author jiangyangyang
...@@ -58,6 +60,33 @@ public class RedisBalanceKeyUtil { ...@@ -58,6 +60,33 @@ public class RedisBalanceKeyUtil {
return null; return null;
} }
/**
* getRedisBalance8key的非hash版
* 从目标字符串中获取从左到右第一个数字并且取模得到redisKey
*
* @param targetStr 目标字符串
* @return 返回指定节点对应的rediskey
*/
public static String getRedisKeyByFirstNumMold(String prefix, String targetStr) {
int randomNum = 0;
for (int i = 0; i < targetStr.length(); i++) {
char c = targetStr.charAt(i);
if (c >= 48 && c <= 57) {
randomNum = c;
break;
}
}
RedisBalance8KeyEnum[] values = RedisBalance8KeyEnum.values();
int mold = randomNum % (values.length);
for (RedisBalance8KeyEnum keyEnum : values) {
if (Objects.equals(keyEnum.order, mold)) {
String key = keyEnum.hashTag;
return StringUtils.join(prefix, "_", keyEnum.order, START, key, END);
}
}
return null;
}
/** /**
* 通过hash 然后取模获取指定的redis的suffix * 通过hash 然后取模获取指定的redis的suffix
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment