【赵渝强老师】Redis的存储结构_数据库


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

# 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语句进数据库的切换。视频讲解如下:

Redis的存储结构


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


  例如:

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的存储结构如下图所示。

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


  RedisDB的设计结构如下所示:

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时,需要指定对应的数据库信息,如下源码所示。
/* 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);
}