This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14268|Reply: 0

[Source] 【Practical Combat】Spring Boot integration Redis tutorial

[Copy link]
Posted on 3/11/2019 4:48:17 PM | | | |
The exception is resolved by injecting the RedisTemplate instance in SpringBoot
https://www.itsvse.com/thread-7224-1-1.html
(Source: Architect_Programmer)
Error creating bean with name 'stringRedisTemplate' defined in...
https://www.itsvse.com/thread-7225-1-1.html
(Source: Architect_Programmer)

Environment:spring boot 2.1.1.RELEASE
pom.xml Add a reference, configuring it as follows:



In application.properties, configure it as follows:


Let's talk about the configuration changes of spring-boot-starter-data-redis
https://www.itsvse.com/thread-7207-1-1.html
(Source: Architect_Programmer)

Write a test controller:


We access the controller through the browser and then view the cache through the redis client.

spring redis has two templates, StringRedisTemplate and RedisTemplate, we test the data through these two templates, and insert the itsvse and itsvse2 key-value pairs as follows:



We inserted the same value, why not only the value value format is different, but also the key generation rules are different.(Post says below)

Brief analysis of principles

Take a look at what serialization RedisTemplate and StringRedisTemplate use.

You can see that StringRedisTemplate is an inherited RedisTemplate. Then set the string serialization method to use StringRedisSerializer.

RedisSerializer.string() source code:


UTF_8 Definition:
Next, let's take a look at the serialization code of RedisTemplate, and you can see that the default is to use the JDK serialization method JdkSerializationRedisSerializer:



Querying the source code shows that RedisTemplate uses JdkSerializationRedisSerializer for serialization by default, while StringRedisTemplate uses StringRedisSerializer.

Also note:The data of the two are not common; This means that StringRedisTemplate can only manage the data in StringRedisTemplate, and RedisTemplate can only manage the data in RedisTemplate.

This explains that we just performed the addition operation through the template, and there are different data formats.

Toggle the serialization mode

If you don't want to use the default serialization method, if we use fastjson, you need to customize the RedisTemplate bean to replace the original bean injected by springboot:



(End)




Previous:The grassroots look at talent, the middle level looks at virtue, and the top level looks at the mind
Next:Failed to load files or assemblies from Microsoft.Web.Infrastructure solutions
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com