RedisConfig配置类-jedis\lettuce

Redis作为键值对内存数据,那么我们怎么访问数据库?回想之前访问mysql数据库的流程:数据库连接-预编译-访问数据,针对公共方法提取成配置类。那么Redis也同样,需要先获取连接对象才能做数据的增删改查。

那么这个对象-RedisTemplate

那怎么配置和使用的?

一、Jedis连接方式配置类

大致流程如下:

    // 1、获取redis连接池
    @Bean
    public JedisPoolConfig getJedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(100);
        jedisPoolConfig.setMinIdle(1); // 配置项当然也可以在application.properties文件中配置
        return jedisPoolConfig;
    }

    // 2、配置哨兵模式
    @Bean
    public RedisSentinelConfiguration getRedisConfiguration() {
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        return redisSentinelConfiguration;
    }

    // 3、配置工厂
    @Bean
    public RedisConnectionFactory getRedisConnectionFactory(JedisPoolConfig jedisPoolConfig,
                                                            RedisSentinelConfiguration sentinelConfiguration) {
        return new JedisConnectionFactory(sentinelConfiguration, jedisPoolConfig);
    }

    // 4、实例化对象
    @Bean(name = "myStringRedisTemplate")
    public StringRedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setEnableTransactionSupport(true);
        ... // 省略序列化配置
   return redisTemplate; }
JedisPoolConfig配置类参数说明如下:

 

二、Lettuce连接方式配置类

spring-boot-starter-data-redis 2.0之后默认启动就是lettuce方式

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //便于开发自己编写redis
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //序列化配置
        Jackson2JsonRedisSerializer<Object> objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();//转义
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        objectJackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        //string序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //key 采用string的序列化
        template.setKeySerializer(stringRedisSerializer);
        //hansh的key也采用string的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        //value序列化方式采用jackson
        template.setValueSerializer(objectJackson2JsonRedisSerializer);
        //hansh的value也采用string的序列化方式
        template.setHashValueSerializer(objectJackson2JsonRedisSerializer);

        template.afterPropertiesSet();

        return template;
    }

 其中相关配置可以在application.properties配置文件中配置,前缀spring.redis.lettuce,参数具体含义基本和jedis一致

 

posted @ 2022-05-17 21:18  chch213  阅读(1872)  评论(0编辑  收藏  举报