SpringBoot 配置和加载 redis


redis在SpringBoot中的配置及加载
// spring framework中的注解驱动的缓存管理功能
// 与该注解联合使用@Cacheable(cacheNames = "缓存名称", key = "redis中的key")
@EnableCaching
// 标识该类为一个配置类
@Configuration
public class RedisConfig {

    @Value("${spring.redis.decryptPassword}")
    private String password;
    @Value("${aes.key}")
    private String key;

    // 读取数据源配置属性,生成JedisPoolConfig对象(redis线程池配置)
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.pool")
    public JedisPoolConfig getRedisConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    // 读取数据源配置属性,生成JedisConnectionFactory对象(redis属性对象)
    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public JedisConnectionFactory getConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setUsePool(true);
        JedisPoolConfig config = getRedisConfig();
        factory.setPoolConfig(config);
        factory.setPassword(new String(AesUtil.decrypt(Base64Utils.decodeFromString(password), key)));
        return factory;
    }

    // StringRedisTemplate类继承于RedisTemplate<String, String>
    @Bean
    public StringRedisTemplate stringRedisTemplate() {
        JedisConnectionFactory factory = getConnectionFactory();
        StringRedisTemplate template = new StringRedisTemplate(factory);
        return template;
    }

    // 自定义实现RedisTemplate<String, String>
    // 因为redis中的数据类型有String、hash、list、set、zset(有序集合)
    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        JedisConnectionFactory factory = getConnectionFactory();
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    /**
     * 设置Cacheable缓存失效策略,与@EnableCaching,@Cacheable配合使用
     */
    @Primary
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
        redisCacheManager.setDefaultExpiration(60 * 60 * 6L); //设置缓存失效时间
        return redisCacheManager;
    }

}

文章作者: Huowy
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Huowy !
评论
  目录