美文网首页
RedisKey Template 管理

RedisKey Template 管理

作者: jumper996 | 来源:发表于2019-03-30 13:02 被阅读0次

    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:疾风剑豪
    

    4.需要分类什么的再自行拓展

    相关文章

      网友评论

          本文标题:RedisKey Template 管理

          本文链接:https://www.haomeiwen.com/subject/uqvjbqtx.html