springboot連接redis并動(dòng)態(tài)切換database的實(shí)現(xiàn)方法
眾所周知,redis多有個(gè)db,在jedis中可以使用select方法去動(dòng)態(tài)的選擇redis的database,但在springboot提供的StringRedisTemplate中確,沒(méi)有該方法,好在StringRedisTemplate預(yù)留了一個(gè)setConnectionFactory方法,本文主為通過(guò)修改ConnectionFactory從而達(dá)到動(dòng)態(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); } }
運(yùn)行TestCRedis.t1(),控制臺(tái)打印“testvalue”redis連接成功
redis動(dòng)態(tài)切換database
首先使用redis-cli,在redis的0、1、2三個(gè)庫(kù)中,分別設(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); } }
運(yùn)行TestCRedis.t2(),控制臺(tái)分別打印 “0、1、2”,database切換成功
到此這篇關(guān)于springboot連接redis并動(dòng)態(tài)切換database的文章就介紹到這了,更多相關(guān)springboot連接redis動(dòng)態(tài)切換database內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)自定義Redis的連接的流程步驟
- SpringBoot無(wú)法連接redis的解決方案
- springBoot連接遠(yuǎn)程Redis連接失敗的問(wèn)題解決
- 關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
- springboot連接不上redis的三種解決辦法
- springboot 如何使用jedis連接Redis數(shù)據(jù)庫(kù)
- springboot連接Redis的教程詳解
- springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問(wèn)題)
- 基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作
- Springboot2.X集成redis集群(Lettuce)連接的方法
- Spring Boot2 整合連接 Redis的操作方法
相關(guān)文章
myeclipse中使用maven前常見(jiàn)錯(cuò)誤及解決辦法
這篇文章主要介紹了myeclipse中使用maven前常見(jiàn)錯(cuò)誤及解決辦法 的相關(guān)資料,需要的朋友可以參考下2016-05-05java開(kāi)發(fā)之File類詳細(xì)使用方法介紹
這篇文章主要介紹了java開(kāi)發(fā)之File類詳細(xì)使用方法介紹,需要的朋友可以參考下2020-02-02簡(jiǎn)單快速對(duì)@RequestParam聲明的參數(shù)作校驗(yàn)操作
這篇文章主要介紹了簡(jiǎn)單快速對(duì)@RequestParam聲明的參數(shù)作校驗(yàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Spring boot 整合CXF開(kāi)發(fā)web service示例
這篇文章主要介紹了Spring boot 整合CXF開(kāi)發(fā)web service示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05java.net.UnknownHostException異常的一般原因及解決步驟
關(guān)于java.net.UnknownHostException大家也許都比較熟悉,這篇文章主要給大家介紹了關(guān)于java.net.UnknownHostException異常的一般原因及解決步驟,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02