|
@@ -1,6 +1,11 @@
|
|
|
package com.energy.manage.common.constant.redis;
|
|
|
|
|
|
+import com.energy.manage.common.enums.IdPrefixEnum;
|
|
|
+import com.energy.manage.common.util.IdGeneratorUtil;
|
|
|
+
|
|
|
import java.io.Serializable;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* redis常量
|
|
@@ -18,24 +23,43 @@ public class ManagerRedisKeyConstant {
|
|
|
/**
|
|
|
* id 工具缓存
|
|
|
*/
|
|
|
- public static String IDGENERATOR_CONSTANTS_KEY = "energy:Idgenerator:{id}";
|
|
|
+ public static final String IDGENERATOR_CONSTANTS_KEY = "energy:Idgenerator:{id}";
|
|
|
|
|
|
|
|
|
- private static final String PLACE_HOLDER = "\\{id\\}";
|
|
|
+ private static final String PLACE_HOLDER = "{id}";
|
|
|
|
|
|
|
|
|
- public static String build(String key, Serializable... id) {
|
|
|
- Serializable[] var2 = id;
|
|
|
- int var3 = id.length;
|
|
|
+ /**
|
|
|
+ * 字符串分隔符
|
|
|
+ */
|
|
|
+ private static final String SEPARATOR = "-";
|
|
|
|
|
|
- for (int var4 = 0; var4 < var3; ++var4) {
|
|
|
- Serializable i = var2[var4];
|
|
|
- key = key.replaceFirst("\\{id\\}", String.valueOf(i));
|
|
|
- }
|
|
|
- return key;
|
|
|
+ /**
|
|
|
+ * redis key的构建方法
|
|
|
+ * @param key
|
|
|
+ * @param ids
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String build(String key, Serializable... ids) {
|
|
|
+ //换成buffer, 为了 1.lambda中能用 2.方便字符串拼接
|
|
|
+ StringBuffer keyBuffer = new StringBuffer(key);
|
|
|
+ //数组转换为list,为了使用stream和获取下标
|
|
|
+ List<Serializable> idList = Arrays.asList(ids);
|
|
|
+ idList.stream().forEachOrdered(id -> {
|
|
|
+ //首个id进行替换 后面的累加
|
|
|
+ if(idList.indexOf(id) == 0){
|
|
|
+ int start = keyBuffer.indexOf(PLACE_HOLDER);
|
|
|
+ int end = start + PLACE_HOLDER.length();
|
|
|
+ keyBuffer.replace(start, end, id.toString());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ keyBuffer.append(SEPARATOR + id);
|
|
|
+ });
|
|
|
+ return keyBuffer.toString();
|
|
|
}
|
|
|
|
|
|
- static {
|
|
|
- IDGENERATOR_CONSTANTS_KEY = "energy:Idgenerator:{id}";
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String a = ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, IdPrefixEnum.WIND_FIELD_NUMBER.getCode(),IdPrefixEnum.WIND_ENGINE_BATCH_NUMBER.getCode());
|
|
|
+ System.out.println(a);
|
|
|
}
|
|
|
}
|