In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "the method of SpringBoot integrating Redis to operate API". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope that this article "SpringBoot Integration Redis Operation API method" can help you solve the problem.
The underlying interpretation of SpringDataRedis calling Redis
Before SpringBoot2.X, the official recommended Jedis-connected Redis was used directly.
Change to lettuce after 2.x
Jedis: using direct connection, multi-thread operation is not safe, if you want to avoid unsafe, use Jedis pool connection pool; BIO
Lettuce: the bottom layer uses Netty, and the instance can be shared among multiple threads. There is no thread unsafe situation, so the number of threads can be reduced. NIO
SpringBoot integrates Redis (source code analysis)
All SpringBoot configuration classes have an automatic configuration class
Automatic configuration classes are bound to a properties file
Find Spring.factories in the source code
Search for redis inside and find AutoConfiguration.
Press ctrl+ to click to enter the class
Find redisproperties.class.
Click ctrl+ to enter
There are all the redis-related configurations. Take a brief look at them first and then talk about the rest later.
Bean injected by default
But there are some problems with the default redisTemplate. His key is of type Object, but the general key we expect is of type String, which requires forced type conversion, so it is proposed above that you can define the RedisTemplate yourself.
When configuring the configuration file, if you need to configure the connection pool, use lettuce, do not configure Redis directly, and the configuration will not take effect
View the RedisConnectionFactory during injection
It has two subclasses, JedisConnectionFactory and LettuceConnectionFactory.
Why is it said that direct JedisConnectionFactory does not work? Because many of the dependent classes in the class do not exist
All of them are red lines, and the dependencies in lettuceConnectionFactory are all there.
So when configuring, use lettuce's
Do not configure jedis directly
SpringBoot integrates Redis (configuration) yml
Copy properties to create a configuration file in yml format, I still like yml very much
Spring: redis: host: localhost port: 6379Maven is selected when the project is created. If not, add org.springframework.boot spring-boot-starter-data-redis to write the test.
Open the test class created by default by SpringBoot
RedisTemp's methods for manipulating data types all start with opsFor, followed by types
For example, opsForValue manipulates strings.
Then the later application is the same as the API written before.
The common operation can be done directly.
About things.
RedisTemplate.unwatch (); redisTemplate.watch ("key"); redisTemplate.multi (); redisTemplate.discard (); redisTemplate.exec ()
About the operation of the database, you need to use the connection object operation after getting the link.
RedisConnection connection = redisTemplate.getConnectionFactory (). GetConnection (); connection.flushAll (); connection.flushDb (); connection.close (); test code and its execution result package co.flower.redis02springboot;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.RedisTemplate @ SpringBootTestclass Redis02SpringbootApplicationTests {/ * I actually reported an error directly when I specified the generic RedisTemplate. After deleting the generic, I succeeded * / @ Autowiredprivate RedisTemplate redisTemplate; @ Testvoid contextLoads () {/ / English test redisTemplate.opsForValue (). Set ("name", "xiaojiejie"); System.out.println (redisTemplate.opsForValue (). Get ("name")); / / Chinese test redisTemplate.opsForValue (). Set ("name", "little sister") System.out.println (redisTemplate.opsForValue (). Get ("name"));}} as a result of the execution, the startup loading and termination of SpringBoot are not pasted / * SpringBootStart****/xiaojiejie sister / * SpringBootStop*/ on "SpringBoot Integration Redis Operation API method" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.