springboot连接redis并动态切换database

本文仅供个人学习使用,怕原链接失效,原链接地址:
https://blog.csdn.net/jlh912008548/article/details/78982008

springboot连接redis并动态切换database

众所周知,redis多有个db,在jedis中可以使用select方法去动态的选择redis的database,但在springboot提供的StringRedisTemplate中,没有该方法,好在StringRedisTemplate预留了一个setConnectionFactory方法,本文主为通过修改ConnectionFactory从而达到动态切换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-cli,在redis的0、1、2三个库中,分别设置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);
    }

    @Test
    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切换成功

本文仅供个人学习使用,怕原链接失效,原链接地址:
https://blog.csdn.net/jlh912008548/article/details/78982008

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值