Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to switch redis Library in Springboot2.X

Shulou Source: shulou.com Published: 2022-06-02 05:48:41 09月11日 Update

Use the Redis native method to use 16 databases at the same time, and continue to use the above pom file dependency

9. Redis tool class

Remember: the next version for Springboot2.1.X, including 2.0.X, 2.2.X, currently does not switch databases through the following.

Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component;@Componentpublic class RedisUtil {@ Autowired private StringRedisTemplate redisTemplate; public void setRedisTemplate (StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;} public void setDataBase (int num) {LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory () If (connectionFactory! = null & & num! = connectionFactory.getDatabase ()) {connectionFactory.setDatabase (num); this.redisTemplate.setConnectionFactory (connectionFactory); connectionFactory.resetConnection ();}} public StringRedisTemplate getRedisTemplate () {return this.redisTemplate;} public void set (String key, String value) {redisTemplate.opsForValue () .set (key, value) } / / get the value of the specified key public String get (String key) {return redisTemplate.opsForValue (). Get (key);}} 10, Controller test @ AutowiredRedisUtil redisUtil;@GetMapping ("/ test") public String test (String key) {for (int I = 1; I < 3; iSue +) {redisUtil.setDataBase (I); redisUtil.set (key, "" + I + key) } / / the development environment uses the following code, which simply tests to different redis databases / / redisUtil.setDataBase (RedisPartition.EIGHT.getOrdinal ()); return redisUtil.get (key);}

11. Summary

There are two ways to switch databases between jedis and StringRedisTemplate. I prefer to use jedis. With SringRedisTemplate, I don't copy all the redis methods too much.

12. Attach

The following is the configuration of ssm framework switching to use redis database at the same time, as for redis.xml and redis.properties to copy from the Internet.

@ Resource (name= "stringRedisTemplate") private StringRedisTemplate stringRedisTemplate;private StringRedisTemplate choseConnection (RedisConnectionEnum redisEnum) {JedisConnectionFactory factory = (JedisConnectionFactory) stringRedisTemplate.getConnectionFactory (); factory.setDatabase (redisEnum.getDbNum ()); stringRedisTemplate.setConnectionFactory (factory); return stringRedisTemplate;} is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Data database switch different content method faceted test configuration at the same time tools files versions allocation help query clear and then code represent Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL vpn macOS Docker