1. Import dependencies

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

Source code analysis

After spring 2.x, use is no longer redis, but lettuce

  • Jedis direct connection, multithreading is not safe, avoid by jedis pool thread pool, like bio mode
  • The oracle uses Netty multiplexing asynchronous non-blocking connections, like niO mode

Click on the spring-boot-starter-data-redis dependency package and you can see it

<dependency>
      <groupId>io.lettuce</groupId>
      <artifactId>lettuce-core</artifactId>
      <version>. 6.0.4 RELEASE</version>
      <scope>compile</scope>
    </dependency>
Copy the code

The specification is implemented via lettuce

public class RedisAutoConfiguration {

	@Bean
        // only works when there is no redisTemplate
	@ConditionalOnMissingBean(name = "redisTemplate")
	@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
                // The default redisTemplate doesn't do much, whereas our redis object needs to be serialized
                // Hence the need to customize the reisTemplate
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
		StringRedisTemplate template = new StringRedisTemplate();
		template.setConnectionFactory(redisConnectionFactory);
		returntemplate; }}Copy the code

2. Configure properties

See more property configurations in the RedisProperties class
spring.redis.host= 192.168.252.128
spring.redis.port= 6379
Copy the code

3. Integration testing

class DemoApplicationTests {
        @Autowired
	RedisTemplate redisTemplate=new RedisTemplate();
	@Test
	void contextLoads(a) {
        // redisTemplate operates like redis
        // opsForValue Operation String, similar to String
        // opsForList operates List
		redisTemplate.opsForValue().set("user"."lin");
                // Although the object was successfully obtained in Java, there would be garbled characters in redis server due to no serialization
		System.out.println(redisTemplate.opsForValue().get("user"));
        // Get the connection objectRedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); connection.flushAll(); connection.flushDb(); }}Copy the code

4. Customize the Template

And when we serialize, although we can get objects normally on the console, we can’t get them on the server

So we need to set up our own serialization

@Configuration
public class RedisConfig {
    @Bean
    // Use String as the key
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // Customize the serialization scheme
        // Configure String serialization
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // Configure Json serialization
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om=new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // enableDefaultTyping() is out of date
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // Key uses string serialization
        template.setKeySerializer(stringRedisSerializer);
        // Hashkey uses string serialization
        template.setHashKeySerializer(stringRedisSerializer);
        // value uses Jason serialization
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // Hash Value uses Jason serialization
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();
        returntemplate; }}Copy the code

Now let’s test it again

@SpringBootTest
class DemoApplicationTests {
   @Resource(name = "redisTemplate") // Since there is a default RedisTemplate, we need to match it by name
   RedisTemplate redisTemplate=new RedisTemplate();
	@Test
	void contextLoads(a) {
		User lin = new User("lin".18);
		RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
		connection.flushDb();
		redisTemplate.opsForValue().set("user1",lin);
		System.out.println(redisTemplate.opsForValue().get("user1")); connection.close(); }}Copy the code