- 这里是SpringBoot2集成RedisCacheManager的方式
SpringBoot1配置CacheManager有区别
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
RedisConfig
@Configuration
@ConfigurationProperties(prefix = "redis")
@EnableCaching // 需要这个注解才能启用注解驱动的缓存管理功能
public class RedisConfig {
private String clusterNodes;
private String redisHost;
private int redisPort;
private String redisPasswd;
private int timeOut = 2000;
private int redirects = 8;
@Bean // redis配置
public RedisClusterConfiguration getClusterConfiguration() {
Map<String, Object> source = Maps.newHashMap();
source.put("spring.redis.cluster.nodes", clusterNodes);
source.put("spring.redis.cluster.timeout", timeOut);
source.put("spring.redis.cluster.max-redirects", redirects);
return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
}
@Bean // redis连接
public RedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory cf = null;
if (clusterNodes != null && !clusterNodes.isEmpty()) {
cf = new JedisConnectionFactory(getClusterConfiguration());
} else {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(redisHost);
redisStandaloneConfiguration.setPort(redisPort);
redisStandaloneConfiguration.setPassword(RedisPassword.of(redisPasswd));
cf = new JedisConnectionFactory(redisStandaloneConfiguration);
}
cf.afterPropertiesSet();
return cf;
}
@Bean // 实际使用的redisTemplate,可以直接注入到代码中,直接操作redis
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
@Bean // 关联redis到注解
CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
// 默认配置,过期时间指定是30分钟
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
defaultCacheConfig.entryTtl(Duration.ofMinutes(30));
// redisExpire1h cache配置,过期时间指定是1小时,缓存key的前缀指定成prefixaaa_(存到redis的key会自动添加这个前缀)
RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().
entryTtl(Duration.ofHours(1)).prefixKeysWith("prefixaaa_");
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
redisCacheConfigurationMap.put("redisExpire1h", userCacheConfiguration);
RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
return cacheManager;
}
使用注解驱动的缓存管理功能
-
TestUser
必须实现implements Serializable
,不然无法序列化到redis;而且serialVersionUID
务必指定,不然增加字段,会导致反序列化回来失败 -
key
的语法是SpEL
注解使用在interface接口上,key
需要使用#p0
#p0.id
这种index下标的方式访问,0表示第一个入参,以此类推;
其他具体的bean,key
可以使用#id
#testUser.id
方式访问,interface接口用这种方式访问会报错:java.lang.IllegalArgumentException: Null key returned for cache operation
-
value
对应的是cacheManager
中的redisCacheConfigurationMap
中的配置(map可以放多个配置),这里指定的是redisExpire1h
;
不写或者匹配不上,使用的是cacheManager
中默认的defaultCacheConfig
@Repository("testUserDAO")
public interface TestUserDAO {
// 查询操作---缓存
@Cacheable(value = "redisExpire1h", key = "'test_user_'.concat(#p0)")
TestUser selectById(@Param("id") String id);
// 更新操作---清除缓存
@CacheEvict(value = "redisExpire1h", key = "'test_user_'.concat(#p0.id)")
Integer updateConfigByCorp(TestUser testUser);
}
null值处理
- 如上配置的话,null值会被缓存,所以insert的时候也需要CacheEvict;不然先查(缓存了null),再插,后查询可能一直返回null
nullValue缓存如下: "\xac\xed\x00\x05sr\x00+org.springframework.cache.support.NullValue\x00\x00\x00\x00\x00\x00\x00\x01\x02\x00\x00xp"
参考
扩展
Mysql缓存机制,Mybatis缓存机制