springboot 缓存@Cachealbe 集成 Redis
前言:
缓存注解解析: https://blog.csdn.net/Dreamhai/article/details/80642010
详细的配置步骤
pom文件添加spring-boot-starter-data-redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.properties 文件配置
spring.cache.type=REDIS
spring.cache.redis.time-to-live:3600000
spring.redis.host=192.168.93.210
spring.redis.port=6379
spring.redis.password=password123
spring.redis.database=0
spring.redis.timeout=5000
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=3000
Redis缓存配置
包括
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.databind.ObjectMapper;
// 1. 开启缓存注解
@EnableCaching
@Configuration
public class RedisConfig {
public static final int NO_PARAM_KEY = 0;
public static final int NULL_PARAM_KEY = 53;
/**
* 指定key的生成策略
* @return
*/
@Bean
public KeyGenerator simpleKeyGenerator() {
return (o, method, objects) -> {
StringBuilder key = new StringBuilder();
key.append(o.getClass().getSimpleName());
key.append(".");
key.append(method.getName());
key.append(": [");
// 当参数为空时
if (objects.length == 0) {
key.append(NO_PARAM_KEY).append("]");
key.append("]");
return key.toString();
}
for (Object obj : objects) {
if (obj == null)
key.append(NULL_PARAM_KEY);
else
key.append(obj.toString());
}
key.append("]");
return key.toString();
};
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
this.getRedisCacheConfigurationWithTtl(600), // 默认策略,未配置的 key,会使用这个
this.getRedisCacheConfigurationMap() // 指定 key 策略
);
}
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
// 指定缓存名为 getDiagSoftByPdtTypeId 的失效时间为1800秒
redisCacheConfigurationMap.put("diagdevice_app_service", this.getRedisCacheConfigurationWithTtl(1800));
//redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(18000));
return redisCacheConfigurationMap;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
Jackson2JsonRedisSerializer<Object> 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);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.entryTtl(Duration.ofSeconds(seconds));
return redisCacheConfiguration;
}
}
service类
@Service(interfaceClass = DiagSoftService.class)
@Transactional(readOnly = true)
public class DiagSoftServiceImpl implements DiagSoftService {
@Autowired
private DiagSoftDao diagSoftDao;
@Override
// cacheNames 和 value 是同一个熟悉,查看源码可知
// @Cacheable(cacheNames="diagdevice_app_service",keyGenerator="simpleKeyGenerator",unless="#result == null")
@Cacheable(value="diagdevice_app_service",keyGenerator="simpleKeyGenerator",unless="#result == null")
public List<DiagSoft> selectByCondition(Map<String,Object> condition) {
List<DiagSoft> temp = new ArrayList<DiagSoft>();
List<DiagSoft> list = diagSoftDao.selectByCondition(condition);
return (list == null) ? temp : list;
}
}