目录
前言
集成
maven依赖
CacheManagerConfig配置
redis配置
使用
Springboot 集成使用缓存
@Cacheable
@CacheEvict
前言
现有项目中经常遇到的缓存集成问题,Springboot提供了统一的接口抽象与缓存管理器,可集成多种缓存类型,如 ConcurrentMap 缓存、redis缓存,并可以随时无缝替换底层缓存存储。
集成
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
CacheManagerConfig配置
下面配置了2个,一个是localCacheManager,底层用的是ConcurrentMap,另一个是 RedisCacheManager,底层用的是redis。
通过 @Primary 注解,可以控制那个是主缓存。
@Configuration
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
public class CacheManagerConfig {
public static final String CACHE_MANAGER_LOCAL = "localCacheManager";
public static final String CACHE_MANAGER_REDIS = "redisCacheManager";
@Autowired
private CacheProperties cacheProperties;
@Bean(name = CACHE_MANAGER_LOCAL)
public CacheManager localCacheManager() {
return new ConcurrentMapCacheManager();
}
@Primary
@Bean(name = CACHE_MANAGER_REDIS)
public RedisCacheManager redisCacheManager() {
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(cacheProperties.getRedis().getTimeToLive()).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer));
return RedisCacheManager.builder(jedisConnectionFactory())
.cacheDefaults(config).build();
}
}
redis配置
配置文件
spring:
redis:
host: 192.168.1.252
port: 6379
database: 1
password: 123456
cache:
redis:
time-to-live: 600s
redisconfig
/**
* jedis连接工厂
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());
redisStandaloneConfiguration.setPort(redisProperties.getPort());
redisStandaloneConfiguration.setHostName(redisProperties.getHost());
redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration);
return factory;
}
redisTemplate
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setDefaultSerializer(genericJackson2JsonRedisSerializer);
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
使用
注入cacheManager,并使用
@Autowired
private CacheManager cacheManager;
public void test() {
Cache ca = cacheManager.getCache("userCache");
ca.put("key", "value");
ca.get("key");
}
Springboot 集成使用缓存
@Cacheable
注解为
@Cacheable 标记当前方法, 可将数据缓存与缓存,一般用于查询类方法
@Cacheable(value = "firm", key = "#id", condition="#id!=null")
public FirmBo getFirm(Long id) {
return get(id);
}
@CacheEvict
销毁缓存,一般用于编辑、新增或删除时候销毁缓存,下次查询时,会重新走数据库
@CacheEvict(value = "person", key = "#person.id", condition = "#person.id!=null")
public PersonBo savePerson(PersonBo person) {
save(person);
}
redis缓存存储实例