导入依赖
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
redis yml
spring:
redis:
database: 0
host: 10.0.0.163
port: 6379
timeout: 3000
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Value("${redis-timeout}")
private Long redisTimeout;
//缓存管理器
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
cacheManager.setDefaultExpiration(redisTimeout);
return cacheManager;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){
StringRedisTemplate template = new StringRedisTemplate(factory);
setSerializer(template);//设置序列化工具
template.afterPropertiesSet();
return template;
}
private void setSerializer(StringRedisTemplate template){
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
}
}
为了方便使用将redis写成工具包
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
/**
* @Description: 批量删除缓存
* @Author: hj
* @Date: 17:13 2017/10/24
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* @Description: 批量删除缓存(通配符)
* @Author: hj
* @Date: 16:52 2017/10/24
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0)
redisTemplate.delete(keys);
}
/**
* @Description: 删除缓存
* @Author: hj
* @Date: 16:51 2017/10/24
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* @Description: 判断缓存中是否有对应的value
* @Author: hj
* @Date: 16:50 2017/10/24
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* @Description: 读取缓存
* @Author: hj
* @Date: 16:49 2017/10/24
*/
public Object get(final String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* @Description: 写入缓存
* @Author: hj
* @Date: 16:48 2017/10/24
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
redisTemplate.opsForValue().set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @Description: 写入缓存(可以配置过期时间)
* @Author: hj
* @Date: 16:46 2017/10/24
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
redisTemplate.opsForValue().set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
Controller就不写了,测试Service
@Service
public class TestService {
@Autowired
private TestMapper testMapper;
@Autowired
private RedisUtil redisUtil;
public Test test1(Test test) {
Test mytest = testMapper.getOneById(test.getId());
redisUtil.set("test",mytest,10L); //10秒过期
return mytest;
}
public Test test2() {
return (Test)redisUtil.get("test");
}
}