The following dependencies are introduced into the pom.xml of the project:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
To configure:
#redis host
spring.redis.host=192.168.3.18
View the RedisAutoConfiguration source code:
@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
It can be seen that two template tools, RedisTemplate and StringRedisTemplate, are provided for us to simplify the operation of redis.
RedisTemplate is used for object operation of key value, while StringRedisTemplate prefers string operation
Test:
Here is the key test. In fact, the use of these templates is similar to the command line command. Analogy is OK.
In general, data is saved in redis in the form of string, which is converted to json data.
- Convert the object to json data and store it in the form of string string in redis
- Customize the Serializer in RedisTemplate
Do not set it here, and directly serialize it into redis. Note: the serialized object needs to implement the Serializable interface
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootCacheApplicationTests {
@Autowired
private EmployeeMapper employeeMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedisTemplate(){
Employee employee = employeeMapper.selectEmployeeById(1);
redisTemplate.opsForHash().put("employee", employee.getId(), employee);
}
}
Result:
Reason:
In RedisTemplate, the default serializer is JdkSerializationRedisSerializer
if (this.defaultSerializer == null) {
this.defaultSerializer = new JdkSerializationRedisSerializer(this.classLoader != null ? this.classLoader : this.getClass().getClassLoader());
}
The data in redis is the result of using this serializer.
Custom serializer:
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Employee> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Employee> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> employeeJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
template.setDefaultSerializer(employeeJackson2JsonRedisSerializer);
return template;
}
}
We can see the implementation class of RedisSerializer:
Select Jackson 2json redisserializer to set it as the default serializer.
Result: