依赖pom
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
RedisUtil.java
public class RedisUtil {
@Resource
RedisTemplate<String,String> redisTemplate;
//序列化,用于去除前缀
@Autowired(required = false)
public void setRedisTemplate(RedisTemplate<String,String> redisTemplate) {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer<String> 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);
}
}