In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to solve the problems found in the use of spring redisTemplate". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Background
Today, I found a problem when using the redis feature that comes with spring. When using the redisTemplate implementation class, set a string key is, set is successful, and then through the program get key, you can also get it successfully. But when you get the above key from the redis command line, you will find a strange problem. In any case, you can't find the corresponding key.
I don't understand!
Analysis.
No way, the local environment, check the cache redis key, and finally found the problem, the original set to redis in the key, and we specified key is not quite the same. The key we specified is like this.
Xxx:xxx:001
But the actual key stored by redis is like this
\ xac\ xed\ x00\ x05t\ X00) xxx:xxx:001
Where did this prefix come from? it certainly has nothing to do with redisTemplate.
Breakpoint tracking:
First of all, track the link from the program to redisTemplate hasKey.
Is there a problem here that redisTemplate converts the specified key to binary processing?
Continue to follow up
The original redisTemplate is the specified key, which is serialized into binary by keySerializer.
So where did this keySerializer come from? keep tracking.
It turns out that when redisTemplate does not specify the serialization scheme for key, the default serialization method is used, and the default serialization method is to use JdkSerializationRedisSerializer.
The serialization of JdkSerializationRedisSerializer.serialize is as follows
You will find that the key part is the serializer.convert (object) method. Serializer is an attribute of the jdk serialization scheme, which is specified when the object is constructed
The conversion method of SerializingConverter is as follows
The DefaultSerializer.serialize method is as follows
In this object output stream, a type prefix is added.
It is convenient to identify the type of stream when deserializing later, which is why we will find that there is an extra string of characters in front of key.
For example, I output an empty string object into a stream, and the result is converted to hexadecimal, also known as ACED0005740000.
Therefore, because an object (Object) is serialized here, it is necessary to add relevant information such as the type of the object to facilitate future deserialization.
Solution.
1. RedisTemplate specifies keySerializer and replaces JdkSerializationRedisSerializer with StringRedisSerializer
2. Spring also provides an implementation class for StringRedisTemplate, which treats key,value as a string. There's no problem with this.
This is the end of the content of "how to solve the problems found in the use of spring redisTemplate". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.