SpringBoot集成Redis缓存

SpringBoot 集成 Redis 缓存

查询操作是应用中最常见的操作,如果每次查询都从 MySQL 中查询则会影响效率,通常需要引入缓存来实现查询性能的优化。缓存可以选择本地缓存,远程缓存或本地缓存结合远程缓存。本地缓存可以使用 Guava 或 Caffeine 提供的解决方案,而远程缓存则可以选择 Redis 这样的内存数据库。本文记录一下 SpringBoot 集成 Redis 做缓存的相关配置。

1 引入依赖

引入相应 Starter。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2 缓存配置

SpringBoot 中缓存由 CacheManager 管理,实现自己的 CacheManager 即可。

@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    //解决查询缓存转换异常的问题,为Jackson配置ObjectMapper
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.activateDefaultTyping(
            LaissezFaireSubTypeValidator.instance,
            ObjectMapper.DefaultTyping.NON_FINAL,
            JsonTypeInfo.As.WRAPPER_ARRAY);
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    jackson2JsonRedisSerializer.setObjectMapper(om);


    // 配置序列化(解决乱码的问题)
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
            .disableCachingNullValues();
	
    Set<String> cacheNames = new HashSet<>();
    cacheNames.add("FlashActivity");
    cacheNames.add("FlashItem");

    Map<String, RedisCacheConfiguration> configMap = new ConcurrentHashMap<>();
    configMap.put("FlashActivity", config.entryTtl(Duration.ofMinutes(60)));
    configMap.put("FlashItem", config.entryTtl(Duration.ofSeconds(3)));

    return RedisCacheManager.builder(factory)
            .cacheDefaults(config)
            .initialCacheNames(cacheNames)
            .withInitialCacheConfigurations(configMap)
            .build();
}

2.1 配置 ObjectMapper

ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 必要配置,否则反序列化得到的是LinkedHashMap对象
om.activateDefaultTyping(
        LaissezFaireSubTypeValidator.instance,
        ObjectMapper.DefaultTyping.NON_FINAL,
        JsonTypeInfo.As.WRAPPER_ARRAY);
// 出现未知字段不报错
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

2.2 配置 CacheConfig

// 配置序列化(解决乱码的问题)
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
        .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
        .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
        .disableCachingNullValues();

2.3 不同 Cache 设置不同 TTL

Set cacheNames = new HashSet<>();
cacheNames.add("FlashActivity");
cacheNames.add("FlashItem");

Map configMap = new ConcurrentHashMap<>();
configMap.put("FlashActivity", config.entryTtl(Duration.ofMinutes(60)));
configMap.put("FlashItem", config.entryTtl(Duration.ofSeconds(3)));

3 注解使用

// 类上指定缓存到哪些 Cache
@CacheConfig(cacheNames = "FlashItem")
@CacheEvict(key = "'FlashItemCache'.concat(#itemId)")
// sync指定为true,缓存失效只会有一个线程取请求数据库,其他线程使用请求回的数据
@Cacheable(key = "'ActivityFlashItemCache'.concat(#activityId)", sync = true)

4 RedisTemplate 配置

使用 RedisTemplate 可以实现手动缓存或其他对 Redis 操作,但之前只是配置缓存相关配置,直接使用RedisTemplate 时不会生效,需要单独配置。

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);


    ObjectMapper objectMapper = new ObjectMapper();

    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    // key-value结构序列化数据结构
    redisTemplate.setKeySerializer(stringRedisSerializer);
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    // hash数据结构序列化方式,必须这样否则存hash 就是基于jdk序列化的
    redisTemplate.setHashKeySerializer(stringRedisSerializer);
    redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    redisTemplate.afterPropertiesSet();
    return redisTemplate;
}

你可能感兴趣的:(Java技术学习,spring,boot,redis)