How to configure Redis High concurrency caching by SpringBoot
Today, I would like to share with you the relevant knowledge points about how SpringBoot configures Redis high concurrent cache. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
1. Introduce dependency
Org.springframework.boot spring-boot-starter-data-redis
two。 Configuration
# start the database index of redis#redis (default is 0) server address of spring.redis.database=2#redis spring.redis.host=127.0.0.1# password (no empty) maximum number of connections of spring.redis.password=# connection pool maximum blocking wait time of spring.redis.jedis.pool.max-active=2000# connection pool (use negative values for unlimited) spring.redis.jedis.pool.max-wait=-1# connection pool Minimum idle connection spring.redis.jedis.pool.min-idle=50# connection timeout (milliseconds) spring.redis.timeout=1000# cluster mode configuration # spring.redis.cluster.nodes=106.54.79.43:7001106.54.79.43:7002106.54.79.43:7003106.54.79.43:7004106.54.79.43:7005106.54.79.43:7006
3. Automatically assembled object
@ AutowiredStringRedisTemplate stringRedisTemplate;// only supports string data @ AutowiredRedisTemplate redisTemplate;// supports object data, but the object needs to be serialized
4. Serialization
What is serialization?
Serialization is the process of transforming the state of an object into a format that can be maintained or transmitted. The opposite of serialization is deserialization, which converts the stream into an object. Combined with these two processes, data can be easily stored and transferred.
Why serialize objects?
The process of converting an object into a byte sequence is called object serialization, and the process of restoring a byte sequence to an object is called object deserialization.
@ Configuration@AutoConfigureAfter (RedisAutoConfiguration.class) public class RedisConfig {/ * * java project www.1b23.com * serializes attributes and creates a connection factory * @ param connectionFactory * @ return * / @ Beanpublic RedisTemplate redisTemplate (LettuceConnectionFactory connectionFactory) {RedisTemplate template = new RedisTemplate (); template.setKeySerializer (new StringRedisSerializer ()); template.setValueSerializer (new GenericJackson2JsonRedisSerializer ()); template.setConnectionFactory (connectionFactory); return template;}}
5. test
/ / java project www.1b23.com@RequestMapping ("/ user") @ RestControllerpublic class UserController {@ AutowiredStringRedisTemplate stringRedisTemplate;// only supports string data @ AutowiredRedisTemplate redisTemplate;// supports object data, provided that @ GetMappingpublic User user () {User user = new User (); user.setId ("1"); user.setName ("zhangshan"); user.setPhone ("133333333"); / / insert data stringRedisTemplate.opsForValue (). Set ("1", user.toString ()) RedisTemplate.opsForValue () .set ("user", user); / / return stringRedisTemplate.opsForValue () .get ("1"); return (User) redisTemplate.opsForValue () .get ("user");}} that's all about "how SpringBoot configures Redis high concurrency caching". 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.