RedisUtil工具类

依赖pom


      redis.clients
      jedis



    org.springframework.boot
    spring-boot-starter-redis
    1.0.0.RELEASE

 RedisUtil.java

public class RedisUtil {

	@Resource
	RedisTemplate redisTemplate;
	
        //序列化,用于去除前缀
	@Autowired(required = false)
        public void setRedisTemplate(RedisTemplate redisTemplate) {
            RedisSerializer stringSerializer = new StringRedisSerializer();
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);//序列化为Json
            redisTemplate.setKeySerializer(stringSerializer);
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            redisTemplate.setHashKeySerializer(stringSerializer);
            redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
            this.redisTemplate = redisTemplate;
        }
	
	public void set(String key,String value){
		redisTemplate.opsForValue().set(key, value);
	}
	
	public String get(String key){
		return redisTemplate.opsForValue().get(key);
	}
	
	public Boolean expire(String key,Integer timeout){
		return redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
	}
	
	public void delete(String key){
		redisTemplate.delete(key);
	}
}

 

你可能感兴趣的:(常用工具,redis,RedisUtil)