In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to operate Redis in SpringBoot. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Solution 1: Spring Data Redis creation project
Create a project and introduce Redis dependencies:
After the creation is successful, you also need to manually introduce the commos-pool2 dependency, so the final complete pom.xml dependency is as follows:
Org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-web org.apache.commons commons-pool2
Spring Data Redis + connection pool is mainly introduced here.
Configure Redis information
Next, configure the Redis information, which includes two aspects: the basic information of Redis on the one hand and the connection pool information on the other:
Spring.redis.database=0spring.redis.password=123spring.redis.port=6379spring.redis.host=192.168.66.128spring.redis.lettuce.pool.min-idle=5spring.redis.lettuce.pool.max-idle=10spring.redis.lettuce.pool.max-active=8spring.redis.lettuce.pool.max-wait=1msspring.redis.lettuce.shutdown-timeout=100ms
Automatic configuration
Automatic configuration takes effect when the developer introduces Spring Data Redis into the project and configures the basic information of Redis.
We can see this in the automatic configuration class of Redis in Spring Boot:
Configuration@ConditionalOnClass (RedisOperations.class) @ EnableConfigurationProperties (RedisProperties.class) @ Import ({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) public class RedisAutoConfiguration {@ Bean @ ConditionalOnMissingBean (name = "redisTemplate") public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {RedisTemplate 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;}}
This automated configuration class is easy to understand:
First of all, mark that this is a configuration class, and the configuration will not take effect until RedisOperations exists (that is, Spring Data Redis is introduced into the project)
Then import the properties configured in application.properties
Then import connection pool information (if any)
Finally, two Bean, RedisTemplate and StringRedisTemplate are provided, in which StringRedisTemplate is a subclass of RedisTemplate, and the two methods are basically the same, but the difference is mainly reflected in the different data types of operation. The two generics in RedisTemplate are both Object, which means that the stored key and value can both be an object, while the two generics of StringRedisTemplate are String, which means that both key and value of StringRedisTemplate can only be strings. If the developer does not provide the relevant Bean, these two configurations will take effect, otherwise they will not.
Use
Next, you can directly inject StringRedisTemplate or RedisTemplate into Service to use:
@ Servicepublic class HelloService {@ Autowired RedisTemplate redisTemplate; public void hello () {ValueOperations ops = redisTemplate.opsForValue (); ops.set ("K1", "v1"); Object K1 = ops.get ("K1"); System.out.println (K1);}}
Data manipulation in Redis, in general, can be divided into two types:
For the operation of key, the relevant methods are in RedisTemplate
For the operation of a specific data type, the relevant method needs to obtain the corresponding data type first, and the operation method to obtain the corresponding data type is opsForXXX
Call this method to store the data in Redis, as follows:
The characters before K1 are caused by the use of RedisTemplate, which is the result of RedisTemplate serializing key.
In RedisTemplate, the default serialization scheme for key is JdkSerializationRedisSerializer.
In StringRedisTemplate, the default serialization scheme for key is StringRedisSerializer, so if you use StringRedisTemplate, key is not prefixed by default.
However, developers can also modify the serialization scheme in RedisTemplate, as follows:
@ Servicepublic class HelloService {@ Autowired RedisTemplate redisTemplate; public void hello () {redisTemplate.setKeySerializer (new StringRedisSerializer ()); ValueOperations ops = redisTemplate.opsForValue (); ops.set ("K1", "v1"); Object K1 = ops.get ("K1"); System.out.println (K1);}}
Of course, you can use StringRedisTemplate directly:
@ Servicepublic class HelloService {@ Autowired StringRedisTemplate stringRedisTemplate; public void hello2 () {ValueOperations ops = stringRedisTemplate.opsForValue (); ops.set ("K2", "v2"); Object K1 = ops.get ("K2"); System.out.println (K1);}}
In addition, it should be noted that the automatic configuration of Spring Boot can only configure stand-alone Redis. If it is a Redis cluster, everything needs to be manually configured. Brother Song will share with you later on how to operate the Redis cluster.
Option 2: Spring Cache
Operating Redis,Spring Cache in the form of Spring Cache unifies the facade of cache. Brother Song has previously had a special article on this solution, and friends can move here: Redis cache can still be used in Spring Boot!
Plan 3: return to primitive times
The third scheme is to use Jedis or other client tools to operate Redis directly. This scheme is also supported in Spring Boot. Although it is troublesome to operate, it supports.
These are all the contents of the article "how to operate Redis in SpringBoot". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more 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.