Redis 的多个数据库

Redis 默认提供了16个数据库. 每个数据库都有一个id, 从 0 开始, [0,15]。 不同的数据库中数据隔离保存。我们可以通过修改redis的配置文件进行修改数据库的数量。

database 32
  • 使用 select <ID> 可以切换数据库.

示例如下:

127.0.0.1:6379> select 1
OK

# 演示数据隔离
127.0.0.1:6379[1]> set test 1
OK
127.0.0.1:6379[1]> select 2
Ok
127.0.0.1:6379[2]> get test
(nil)

可以通过命令之前的部分区分我们所在的数据库.
127.0.0.1:6379[2]> select 2执行这个命令的时候,我们是在数据库1中的。

  • 使用 flushdb只能删除 该数据库(一个库) 中的数据。
  • 使用 flushall 可以删除 所有库 中的数据。

redis使用分库的意义

  • 避免不同应用相同key的影响。
  • 更便于管理。
    一个实例上运行多个库,只运维这一个实例就可以了。
  • 也有不少文章说, redis的作者曾说过, “多数据库的设计可能是最糟糕的决定.”. Redis是单线程的,即使是多数据库也不会带来性能提升. 但是这个并没有和前面的两个好处冲突. 下面是作者的原话:

I understand how this can be useful, but unfortunately I consider Redis multiple database errors my worst decision in Redis design at all… without any kind of real gain, it makes the internals a lot more complex. The reality is that databases don’t scale well for a number of reason, like active expire of keys and VM. If the DB selection can be performed with a string I can see this feature being used as a scalable O(1) dictionary layer, that instead it is not.

With DB numbers, with a default of a few DBs, we are communication better what this feature is and how can be used I think. I hope that at some point we can drop the multiple DBs support at all, but I think it is probably too late as there is a number of people relying on this feature for their work.

Redis的分库是怎么实现?

Redis服务器间所有的数据库都保存在 服务器状态 redis.h/redisServer结构的db数组中。db数组中的每个元素都是一个 redis.h/redisDb结构. 每个redisDb结构代表一个数据库.

struct redisServer {
    // 保存所有数据库
    redisDb *db;
    // 数据库的数量(redis.conf文件中 database配置的)
    int dbnum;
}

每个Redis客户端都有自己的目标数据库,每个客户端执行数据库写命令或者数据库读命令的时候,目标数据库都会成为这些命令的操作对象.
每个客户端用 RedisClient 来描述。 RedisClient结构的db属性记录了客户端当前的目标数据库,这个属性是一个指向 redisDb 的指针.

typedef struct redisClient {
    // 记录客户端端正在使用的数据库id
    redisDb *db;
} redisClient;
举个例子:

假设我们客户端连接的是数据库1,那么客户端与服务器端之间的关系是这样的:
在这里插入图片描述
如果我们运行 select 2, 其实,就是改变的db的指针。让它指向了 db(2),这就是 select 命令的运行原理. 如下图。

在这里插入图片描述

以上就是 关于 Redis 多数据库的内容了。

最后

希望和你成为朋友!我们一起学习~
最新文章尽在公众号【方家小白】,期待和你相逢在【方家小白】
在这里插入图片描述

在Spring Boot 3中连接Redis多个数据库,你可以通过配置多个数据源(`spring.redis.cluster.nodes`)来实现。首先,你需要添加相应的依赖到你的项目中,并且每个数据库都需要有自己的配置: 1. 添加Redis客户端依赖: 对于Spring Data Redis,可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接: 在application.properties或application.yml文件中,为每个数据库创建独立的配置,比如: ```properties # 主数据库 spring.redis.host=master-host spring.redis.port=6379 spring.redis.database=0 # 数据库1 spring.redis.secondary_database_1.host=secondary_host1 spring.redis.secondary_database_1.port=6379 spring.redis.secondary_database_1.database=1 # 数据库2 spring.redis.secondary_database_2.host=secondary_host2 spring.redis.secondary_database_2.port=6379 spring.redis.secondary_database_2.database=2 ``` 3. 创建RedisTemplate实例: 使用`@Configuration`和`@EnableCaching`注解配置类,创建RedisTemplate实例,区分主从数据库: ```java @Configuration @EnableCaching public class RedisConfig { @Bean public StringRedisTemplate primaryRedisTemplate() { StringRedisTemplate template = new StringRedisTemplate(); // 设置主数据库连接 template.setConnectionFactory(primaryConnectionFactory()); return template; } @Bean public StringRedisTemplate secondaryDatabase1Template() { StringRedisTemplate template = new StringRedisTemplate(); // 设置数据库1连接 template.setConnectionFactory(secondaryDatabase1ConnectionFactory()); return template; } // 类似地,为其他数据库创建类似的方法 private RedisConnectionFactory primaryConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName("master-host"); factory.setPort(6379); factory.afterPropertiesSet(); return factory; } private RedisConnectionFactory secondaryDatabase1ConnectionFactory() { // 根据配置设置数据库1的连接信息 JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName("secondary_host1"); factory.setPort(6379); factory.setDatabase(1); // 记住要设置正确的数据库编号 factory.afterPropertiesSet(); return factory; } } ``` 4. 使用模板: 当需要操作特定数据库时,通过注入对应的RedisTemplate实例来访问: ```java @Autowired private StringRedisTemplate primaryTemplate; @Autowired private StringRedisTemplate secondaryDatabase1Template; // 使用时选择对应模板 primaryTemplate.opsForValue().set("key", "value"); secondaryDatabase1Template.opsForValue().get("anotherKey"); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方_小_白

谢谢金主子,记得关注方家小白哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值