springboot連接redis并動態(tài)切換database的實現(xiàn)方法
眾所周知,redis多有個db,在jedis中可以使用select方法去動態(tài)的選擇redis的database,但在springboot提供的StringRedisTemplate中確,沒有該方法,好在StringRedisTemplate預(yù)留了一個setConnectionFactory方法,本文主為通過修改ConnectionFactory從而達到動態(tài)切換database的效果。
springboot連接redis
pom.xml文件中引入spring-boot-starter-redis,版本可自行選擇
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.3.8.RELEASE</version> </dependency>
application.properties
#redis配置 spring.redis.database=0 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=pwd spring.redis.timeout=0 spring.redis.pool.max-active=8 spring.redis.pool.max-idle=8 spring.redis.pool.max-wait=-1 spring.redis.pool.min-idle=0
TestCRedis.java
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) public class TestCRedis{ protected static Logger LOGGER = LoggerFactory.getLogger(TestCRedis.class); @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void t1(){ ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue(); stringStringValueOperations.set("testkey","testvalue"); String testkey = stringStringValueOperations.get("testkey"); LOGGER.info(testkey); } }
運行TestCRedis.t1(),控制臺打印“testvalue”redis連接成功
redis動態(tài)切換database
首先使用redis-cli,在redis的0、1、2三個庫中,分別設(shè)置test 的值,分別為;0、1、2
TestCRedis.java
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) public class TestCRedis{ protected static Logger LOGGER = LoggerFactory.getLogger(TestCRedis.class); @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void t1(){ ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue(); stringStringValueOperations.set("testkey","testvalue"); String testkey = stringStringValueOperations.get("testkey"); LOGGER.info(testkey); } public void t2() { for (int i = 0; i <= 2; i++) { JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) stringRedisTemplate.getConnectionFactory(); jedisConnectionFactory.setDatabase(i); stringRedisTemplate.setConnectionFactory(jedisConnectionFactory); ValueOperations valueOperations = stringRedisTemplate.opsForValue(); String test = (String) valueOperations.get("test"); LOGGER.info(test); } }
運行TestCRedis.t2(),控制臺分別打印 “0、1、2”,database切換成功
到此這篇關(guān)于springboot連接redis并動態(tài)切換database的文章就介紹到這了,更多相關(guān)springboot連接redis動態(tài)切換database內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實現(xiàn)自定義Redis的連接的流程步驟
- SpringBoot無法連接redis的解決方案
- springBoot連接遠程Redis連接失敗的問題解決
- 關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
- springboot連接不上redis的三種解決辦法
- springboot 如何使用jedis連接Redis數(shù)據(jù)庫
- springboot連接Redis的教程詳解
- springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池無效問題)
- 基于SpringBoot2.0默認使用Redis連接池的配置操作
- Springboot2.X集成redis集群(Lettuce)連接的方法
- Spring Boot2 整合連接 Redis的操作方法
相關(guān)文章
簡單快速對@RequestParam聲明的參數(shù)作校驗操作
這篇文章主要介紹了簡單快速對@RequestParam聲明的參數(shù)作校驗操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Spring boot 整合CXF開發(fā)web service示例
這篇文章主要介紹了Spring boot 整合CXF開發(fā)web service示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05java.net.UnknownHostException異常的一般原因及解決步驟
關(guān)于java.net.UnknownHostException大家也許都比較熟悉,這篇文章主要給大家介紹了關(guān)于java.net.UnknownHostException異常的一般原因及解決步驟,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-02-02