添加 依赖
<!-- 缓存: spring cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- 缓存: redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
添加配置
# Redis
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.database=0
spring.redis.password=xxx
配置缓存
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
@Configuration
public class KeyGeneratorCacheConfig extends CachingConfigurerSupport {
private final RedisTemplate redisTemplate;
@Autowired
public KeyGeneratorCacheConfig(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public CacheManager cacheManager() {
// 设置key的序列化方式为String
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 设置value的序列化方式为JSON
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// 设置默认过期时间为600秒
cacheManager.setDefaultExpiration(600);
return cacheManager;
}
/**
* key值为className+methodName+参数值列表
* @return
*/
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... args) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName()).append("#");
sb.append(method.getName()).append("(");
for (Object obj : args) {
if(obj != null) { // 在可选参数未给出时时,会出现null,此时需要跳过
sb.append(obj.toString()).append(",");
}
}
sb.append(")");
return sb.toString();
}
};
}
}
在serviceImpl中加入@CacheConfig并且给给每个方法加入缓存
@Service
@CacheConfig(cacheNames = "constant")
public class ConstantServiceImpl implements ConstantService {
@Autowired
private ConstantMapper constantMapper;
@Cacheable
@Override
public List<Constant> alertMessage() {
ConstantExample constantExample = new ConstantExample();
ConstantExample.Criteria criteria = constantExample.createCriteria();
criteria.andTypeEqualTo("alert");
return constantMapper.selectByExample(constantExample);
}
@Cacheable
@Override
public List<Constant> noteMessage() {
ConstantExample constantExample = new ConstantExample();
ConstantExample.Criteria criteria = constantExample.createCriteria();
criteria.andTypeEqualTo("note");
return constantMapper.selectByExample(constantExample);
}
@Cacheable
@Override
public List<Constant> banner() {
ConstantExample constantExample = new ConstantExample();
ConstantExample.Criteria criteria = constantExample.createCriteria();
criteria.andTypeEqualTo("banner");
return constantMapper.selectByExample(constantExample);
}
}