美文网首页
自定义 RedisTemplate

自定义 RedisTemplate

作者: Tinyspot | 来源:发表于2023-12-10 13:51 被阅读0次

    1. 示例

    @RestController
    @RequestMapping("/redis/manager")
    public class RedisManagerController {
    
        @Resource
        private RedisTemplateManager redisTemplateManager;
    
        @RequestMapping("/all")
        public String getAllKey() {
            return JSON.toJSONString(redisTemplateManager.getRedisTemplate().keys("*"));
        }
    
        @GetMapping("/setValue")
        public String setValue(String key, String value){
            redisTemplateManager.getRedisTemplate().opsForValue().set(key, value);
            return "success";
        }
    
        @RequestMapping("/getValue")
        public String getValue(String key) {
            return redisTemplateManager.getRedisTemplate().opsForValue().get(key).toString();
        }
    
        @RequestMapping("/opsForZSet")
        public String opsForZSet(String key) {
            ZSetOperations zSetOperations = redisTemplateManager.getRedisTemplate().opsForZSet();
            zSetOperations.add(key, "aaa", 1);
            zSetOperations.add(key, "bbb", 2);
            zSetOperations.add(key, "ccc", 3);
    
            Set range = zSetOperations.range(key, 0, zSetOperations.size(key));
            return JSON.toJSONString(range);
        }
    
    }
    

    1.1 RedisTemplate 配置

    @Configuration
    public class RedisConfig {
        @Bean(name = "redisTemplate")
        public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(factory);
    
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
            redisTemplate.setKeySerializer(stringRedisSerializer);
            redisTemplate.setValueSerializer(stringRedisSerializer);
    
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
    
        @Bean(name = "redisTemplateNew")
        public RedisTemplate<String, Object> getRedisTemplateNew(RedisConnectionFactory factory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(factory);
    
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
            redisTemplate.setKeySerializer(stringRedisSerializer);
            redisTemplate.setValueSerializer(stringRedisSerializer);
    
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
    }
    

    2. RedisTemplateManager 类

    2.1 RedisTemplateManager

    @Component
    public class RedisTemplateManager {
    
        @Resource
        private RedisTemplate<String, String> redisTemplate;
        @Resource
        private RedisTemplate<String, String> redisTemplateNew;
    
        private LogRedisTemplate logRedisTemplateNew;
        private LogRedisTemplate logRedisTemplateOld;
    
        public RedisTemplate<String, String> getRedisTemplate() {
            return getNewRedisTemplate();
        }
    
        private RedisTemplate<String, String> getNewRedisTemplate() {
            if (logRedisTemplateNew == null) {
                logRedisTemplateNew = new LogRedisTemplate(redisTemplateNew, "new");
            }
            return logRedisTemplateNew;
        }
    
        private RedisTemplate<String, String> getOldRedisTemplate() {
            if(logRedisTemplateOld == null){
                logRedisTemplateOld = new LogRedisTemplate(redisTemplate,"old");
            }
            return logRedisTemplateOld;
        }
    }
    

    2.2 LogRedisTemplate

    /**
     * 参考 RedisTemplate 类,需要重写 RedisOperations 的方法
     */
    public class LogRedisTemplate extends RedisTemplate<String, String> implements RedisOperations<String, String> {
    
        private final String tag;
        private final RedisTemplate<String, String> redisTemplate;
        private LogZSetOperations logZSetOperations;
    
        public LogRedisTemplate(RedisTemplate<String, String> redisTemplate, String tag) {
            this.redisTemplate = redisTemplate;
            this.tag = tag;
        }
    
        @Override
        public Set<String> keys(String pattern) {
            return redisTemplate.keys(pattern);
        }
    
        @Override
        public ValueOperations<String, String> opsForValue() {
            return redisTemplate.opsForValue();
        }
    
        @Override
        public ZSetOperations<String, String> opsForZSet() {
            if(logZSetOperations == null){
                logZSetOperations = new LogZSetOperations(redisTemplate.opsForZSet(),tag);
            }
            return logZSetOperations;
        }
    
        @Override
        public RedisSerializer<?> getKeySerializer() {
            return redisTemplate.getKeySerializer();
        }
    
        @Override
        public RedisSerializer<?> getValueSerializer() {
            return redisTemplate.getValueSerializer();
        }
    
        // ...
    }
    

    2.3 处理 Zset 类型

    public class LogZSetOperations implements ZSetOperations<String, String> {
    
        private final ZSetOperations<String, String> zsetOperations;
        private final String tag;
    
        public LogZSetOperations(ZSetOperations<String, String> zsetOperations, String tag) {
            this.zsetOperations = zsetOperations;
            this.tag = tag;
        }
    
        @Override
        public Boolean add(String key, String value, double score) {
            return zsetOperations.add(key, value, score);
        }
    
        @Override
        public Set<String> range(String key, long start, long end) {
            return zsetOperations.range(key, start, end);
        }
    
        @Override
        public Long size(String key) {
            return zsetOperations.size(key);
        }
    
        // ...
    }
    

    相关文章

      网友评论

          本文标题:自定义 RedisTemplate

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