iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot连接redis并动态切换database的实现方法
  • 369
分享到

springboot连接redis并动态切换database的实现方法

2024-04-02 19:04:59 369人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

众所周知,Redis多有个db,在jedis中可以使用select方法去动态的选择redis的database,但在SpringBoot提供的StringRedisTemplate中

众所周知,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动态切换database

首先使用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);
    }
    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切换成功

到此这篇关于springboot连接redis并动态切换database的文章就介绍到这了,更多相关springboot连接redis动态切换database内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: springboot连接redis并动态切换database的实现方法

本文链接: https://www.lsjlt.com/news/141573.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作