基于RedisTemplate的redis工具类

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/rewerma/article/details/74505589

RedisTempla是一个spring-redis模块下的操作redis的工具类,有点类似于jdbcTemplate。

但是操作起来略显麻烦,于是封装一个稍微简单点的redis工具类,直接上代码:

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("unchecked")
public class RedisUtil {
    private RedisTemplate<String, Object> redisTemplate;
    private StringRedisTemplate stringRedisTemplate;
    private String namespace = "work:";


    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public void setNamespace(String namespace) {
        if (namespace != null)
            this.namespace = namespace + ":";
    }

    public void set(String k, Object v, long time) {
        String key = namespace + k;
        if (v instanceof String && stringRedisTemplate != null) {
            stringRedisTemplate.opsForValue().set(key, (String) v);
        } else {
            redisTemplate.opsForValue().set(key, v);
        }
        if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    public void set(String k, Object v) {
        set(k, v, -1);
    }

    public boolean contains(String key) {
        return redisTemplate.hasKey(namespace + key);
    }

    public String get(String k) {
        if (stringRedisTemplate != null) {
            return stringRedisTemplate.opsForValue().get(namespace + k);
        } else {
            return (String) redisTemplate.opsForValue().get(namespace + k);
        }
    }

    public <T> T getObject(String k) {
        ValueOperations<String, Object> valueOps = redisTemplate.opsForValue();
        return (T) valueOps.get(namespace + k);
    }

    public void remove(String key) {
        redisTemplate.delete(namespace + key);
    }

    public long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }

    public Set<String> keys(String pattern) {
        return redisTemplate.keys(namespace + pattern);
    }

    public Long increment(String key, long delta) {
        return redisTemplate.opsForValue().increment(namespace + key, delta);
    }

    public Double increment(String key, double delta) {
        return redisTemplate.opsForValue().increment(namespace + key, delta);
    }
}

spring boot的配置:

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setKeySerializer(stringSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(stringSerializer);
        template.setHashKeySerializer(stringSerializer);
        template.setKeySerializer(stringSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate, StringRedisTemplate stringRedisTemplate) {
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        redisUtil.setStringRedisTemplate(stringRedisTemplate);
        redisUtil.setNamespace("xxx");
        return redisUtil;
    }


调用:

@Autowired
private RedisUtil redisUtil;

redisUtil.set("test", "abc");
redisUtil.get("test");

User user = new User();
user.setId(1L);
user.setName("Eric");
redisUtil.set("user", user );
User user1 = redisUtil.getObject("user");


展开阅读全文

没有更多推荐了,返回首页