RedisKey Template 管理
2019-03-30 本文已影响0人
jumper996
1.创建KeyTemplate枚举
public enum RedisKey {
UK("TEST:{userId}:TYPE:{name}", new String[]{ "userId", "name" });
public String keyTemplate;
public String[] keyNames;
RedisKey(String keyTemplate, String[] keyNames) {
this.keyTemplate = keyTemplate;
this.keyNames = keyNames;
}
}
2.创建KeyBuilder
import java.util.Map;
public class RedisKeyBuilder {
public static String build(RedisKey redisKey, Map<String, String> params) {
String key = redisKey.keyTemplate;
String[] keyNames = redisKey.keyNames;
for (String keyName : keyNames) {
String param = params.get(keyName);
param = param==null?"*":param;
key = key.replace("{"+ keyName +"}", param);
}
return key;
}
}
3.使用
import java.util.HashMap;
import java.util.Map;
public class Test66 {
public static void main(String[] args) {
Map<String, String> params = new HashMap<>();
params.put("userId", "0");
params.put("name", "疾风剑豪");
String build = RedisKeyBuilder.build(RedisKey.UK, params);
System.out.println(build);
}
}
TEST:0:TYPE:疾风剑豪