SpringBoot整合Redis

一、RedisTemplate

1、依赖

        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            redis.clients
            jedis
            4.3.0
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.apache.commons
            commons-pool2
        
        
            com.fasterxml.jackson.core
            jackson-databind
        

2、配置文件

spring:
  redis:
    host: 192.168.88.99
    port: 6379
    password: 123456
    lettuce:
      pool:
        max-idle: 8
        min-idle: 0
        max-active: 8
        max-wait: 1000ms

3、工具类

//自定义RedisTemplate序列化方式
//主要是采用json序列化代替jdk序列化
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        //创建RedisTemplate对象
        RedisTemplate redisTemplate = new RedisTemplate<>();
        //设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //创建JSON序列化工具
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer =
                new GenericJackson2JsonRedisSerializer();
        //设置key的序列化
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        //设置value的序列化
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        //返回
        return redisTemplate;
    }
}

4、测试类

@SpringBootTest
class RedisBootApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void test1() {
        redisTemplate.opsForValue().set("age",20);
        Object age = redisTemplate.opsForValue().get("age");
        System.out.println("age = " + age);
    }
    @Test
    void test2(){
        redisTemplate.opsForValue().set("user:01",new User("haibiao",18));
        User o = (User) redisTemplate.opsForValue().get("user:01");
        System.out.println("o = " + o);
    }
}

5、问题

 整体可读性有了很大提升,并且能将Java对象自动的序列化为JSON字符串,并且查询时能自动把JSON反序列化为Java对象。不过,其中记录了序列化时对应的class名称,目的是为了查询时实现自动反序列化。这会带来额外的内存开销。

二、StringRedisTemplate

1、目的

为了节省内存空间,使用String序列化器,要求只能存储String类型的key和value。当需要存储Java对象时,手动完成对象的序列化和反序列化。StringRedisTemplate,它的key和value的序列化方式默认就是String方式。

2、实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
    private Integer age;
}

3、测试类 

@SpringBootTest
class RedisBootApplicationTests {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //Json序列化工具
    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Test
    void test() throws JsonProcessingException {
        //创建对象
        User user = new User("haibiao", 20);
        //手动序列化
        String str = objectMapper.writeValueAsString(user);
        //写入数据
        stringRedisTemplate.opsForValue().set("user:02",str);
        //获取数据
        String string = stringRedisTemplate.opsForValue().get("user:02");
        //手动反序列化
        User user1 = objectMapper.readValue(string, User.class);
        System.out.println("user1 = " + user1);

    }

}

你可能感兴趣的:(redis,spring,boot,redis,java)