Introduction to the

As a non-relational database, Redis is widely used because of its high performance and concurrent read and write characteristics. Spring, as a Java application framework, provides a lightweight container and non-invasive programming model. The Spring Data Redis (SDR) framework simplifies the process of writing Spring applications that use the Redis key and value repository by eliminating redundant tasks and boilerplate code required to interact with the repository through Spring’s excellent infrastructure support.

Data type of Redis vs data type of data stored in Redis

  • Redis supports five data types: string, hash, list, set, and zset(sorted set). The data type here refers to how Redis stores data, and how it is stored.
  • At the same time, the data stored in Redis has its own data types. If Java references are stored in Redis, the data types stored can be Long, List, String, and so on. Regardless of the data type, Redis is ultimately stored as bytes. The process of converting various data types to bytes is serialization.

RedisTemplate and its serialization

The RedisTemplate provides a high-level abstraction for Redis interactions, leaving the user to deal only with connection management and serialization and no other details, leaving the underlying methods such as receiving and returning binary values (byte arrays) to RedisConnection. The way to use RedisTemplate in a Spring application is

  @Autowired
  private RedisTemplate<String, Long> template;
Copy the code

Spring Data Redis default JdkSerializationRedisSerializer as its serialization tool

// RedisTemplate.java

	if(defaultSerializer == null) { defaultSerializer = new JdkSerializationRedisSerializer( classLoader ! = null ? classLoader : this.getClass().getClassLoader()); }Copy the code

Cautions for using StringRedisTemplate

Since the keys and values stored in Redis are usually java.lang.String, the Redis module provides two extensions to RedisConnection and RedisTemplate, StringRedisConnection and StringRedisTemplate, respectively, are serialized using StringRedisSerializer, which means that the stored keys and values are human-readable.

A StringRedisTemplate is injected in the same way as a normal RedisTemplate:

  @Autowired
  private StringRedisTemplate redisTemplate;
Copy the code

But there’s actually a more convenient way to inject StringRedisTemplate. Spring’s RedisAutoConfiguration is configured to automatically register a StringRedisTemplate Bean of that type when there is no custom StringRedisTemplate Bean.

// RedisAutoConfiguration.java

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

When using StringRedisTemplate, inject the RedisTemplate Bean directly into the code, In fact, StringRedisTemplate also inherits from RedisTemplate

. Spring assemples the self-registered Bean when automating the Bean assembly. This code omits the manual registration of the StringRedisTemplate.
,>

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
Copy the code
// StringRedisTemplate.java

public class StringRedisTemplate extends RedisTemplate<String, String> {
    ...
}
Copy the code