In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to use redis correctly in springboot", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use redis correctly in springboot" article.
Redis implements the caching of data. In the project, some dictionary data, session data, and temporary data will be stored in redis, and redis is also supported in springboot. Generally speaking, there is a thread safety risk for multiple threads to use one redis implementation together, and one thread for each implementation is too wasteful and it is very dangerous to control the number of threads, so there are some Redis thread pool components. Let's talk about the two main components.
The jedis thread pool is mainly because each instance has its own thread, which can be obtained from the pool created by it.
Lettuce lettuce is a thread pool tool introduced by apache. Its redis instance can be shared and accessed by multiple threads, which improves the utilization of resources.
Redis serialization configuration
Generally speaking, redis-key uses string serialization; redis-value uses json serialization, json is small and readable, and there is no need to implement the serializer interface.
/ * configuration of redis. * / @ Configurationpublic class RedisConfig {@ Autowired private RedisConnectionFactory redisConnectionFactory; / * redis rewrites RedisTemplate. * / @ Bean public RedisTemplate redisTemplate () {RedisTemplate redisTemplate = new RedisTemplate (); RedisSerializer stringSerializer = new StringRedisSerializer (); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); ObjectMapper om = new ObjectMapper (); om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY) / / allow non-constant fields to output types when serializing, that is, serialization of redis with types om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper (om); / / redis key serialized redisTemplate.setKeySerializer (stringSerializer); redisTemplate.setHashKeySerializer (stringSerializer); / / redis value serialized redisTemplate.setValueSerializer (jackson2JsonRedisSerializer); redisTemplate.setHashValueSerializer (jackson2JsonRedisSerializer) RedisTemplate.setConnectionFactory (redisConnectionFactory); return redisTemplate;}}
In the above code, the json string generated by om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL) is typed so that it can be inferred directly from the type when deserialized.
["com.lind.basic.entity.Token", {"credentials": "ok", "region": "hello", "bucket": null}]
Take a look at the added and read code
@ GetMapping ("set") public String set () throws JsonProcessingException {Token token = Token.builder () .regions ("hello") .build (); redisTemplate.opsForValue () .set ("test:user", token); / / redisTemplate helps us serialize redisTemplate.opsForHash () .put ("author", "zzl", token) Return "OK"; @ GetMapping ("get") public Token get () throws IOException {return (Token) redisTemplate.opsForValue () .get ("test:user");}
Note: for the entity class Token, it should have a no-parameter constructor, which is required for deserialization.
The above is about the content of this article on "how to correctly use redis in springboot". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
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.