Redis 中的键值对是如何存储的?
How are key-value pairs stored in redis?
假设在redis中,有如下字符串类型的键值对:
键1 值1
键2 值2
我知道它们内部存储在 table.
这些键值对是否存储在一个单一的table中?
或者每个键值对是否有不同的table?
即只有一个 table 包含两个键值对,还是一个 table 存储 key1-val1 而另一个 table 存储 key2-val2?
同一个Redis数据库中的所有键值对只有一个table。
实际上,键值对存储在一个大散列中table。
https://github.com/antirez/redis/blob/unstable/src/redis.h#L469
/* Redis database representation. There are multiple databases identified
* by integers from 0 (the default database) up to the max configured
* database. The database number is the 'id' field in the structure. */
typedef struct redisDb {
dict *dict; /* The keyspace for this DB */
dict *expires; /* Timeout of keys with a timeout set */
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */
dict *ready_keys; /* Blocked keys that received a PUSH */
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
struct evictionPoolEntry *eviction_pool; /* Eviction pool of keys */
int id; /* Database ID */
long long avg_ttl; /* Average TTL, just for stats */
} redisDb;
所有键值对都存储在字典中。
假设在redis中,有如下字符串类型的键值对: 键1 值1 键2 值2 我知道它们内部存储在 table.
这些键值对是否存储在一个单一的table中? 或者每个键值对是否有不同的table?
即只有一个 table 包含两个键值对,还是一个 table 存储 key1-val1 而另一个 table 存储 key2-val2?
同一个Redis数据库中的所有键值对只有一个table。
实际上,键值对存储在一个大散列中table。
https://github.com/antirez/redis/blob/unstable/src/redis.h#L469
/* Redis database representation. There are multiple databases identified
* by integers from 0 (the default database) up to the max configured
* database. The database number is the 'id' field in the structure. */
typedef struct redisDb {
dict *dict; /* The keyspace for this DB */
dict *expires; /* Timeout of keys with a timeout set */
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */
dict *ready_keys; /* Blocked keys that received a PUSH */
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
struct evictionPoolEntry *eviction_pool; /* Eviction pool of keys */
int id; /* Database ID */
long long avg_ttl; /* Average TTL, just for stats */
} redisDb;
所有键值对都存储在字典中。