前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【赵渝强老师】Redis的存储结构

【赵渝强老师】Redis的存储结构

原创
作者头像
赵渝强老师
发布2024-08-13 10:13:40
1040
发布2024-08-13 10:13:40
举报
文章被收录于专栏:NoSQL数据库

  Redis内部默认存在16个数据库,这是通过在redis.conf文件中的参数databases决定的。

代码语言:powershell
复制
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT<dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16

  每个Redis数据库都会有一个编号,该编号从0开始计算。当使用Redis客户端连接Redis服务器时,默认将连接到0号数据库中。可以通过使用select语句进数据库的切换。视频讲解如下:

视频内容

  例如:

代码语言:powershell
复制
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 7
OK
127.0.0.1:6379[7]> select 15
OK
**127.0.0.1:6379[15]> select 18**
**(error) ERR DB index is out of range 不存在18号数据库**
127.0.0.1:6379[15]> select 0
OK
127.0.0.1:6379>

  Redis的存储结构如下图所示。

  RedisDB的设计结构如下所示:

代码语言:powershell
复制
typedef struct redisDb {
  dict *dict;         			
  dict *expires;        		
  dict *blocking_keys;   		
  dict *ready_keys;      		
  dict *watched_keys;     		
  int id;           			
  long long avg_ttl;      		
  unsigned long expires_cursor; 
  list *defrag_later;     		
} redisDb;

  其中:

  • dict为核心存储。
  • expires用来处理键的过期行为。
  • blocking_keys使用较少。
  • ready_keys与blocking_keys搭配使用。当下次执行push时,Redis会检查blocking_keys当中是否存在对应的key,再采取相对应的操作。
  • watched_keys负责实现watch功能,但watch对redis性能影响极大,线上环境禁止使用。

  因此在向Redis数据库中添加Key时,需要指定对应的数据库信息,如下源码所示。

代码语言:powershell
复制
/* Add the key to the DB. It's up to the caller 
* to increment the reference
* counter of the value if needed.
* The program is aborted if the key already exists. */
void dbAdd(redisDb *db, robj *key, robj *val) {
  sds copy = sdsdup(key->ptr);
  int retval = dictAdd(db->dict, copy, val);
  serverAssertWithInfo(NULL,key,retval == DICT_OK);
  signalKeyAsReady(db, key, val->type);
  if (server.cluster_enabled) slotToKeyAdd(key->ptr);
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档