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语句进数据库的切换。视频讲解如下:
例如:
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的设计结构如下所示:
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;
其中:
因此在向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);
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。