2015-02-11 54 views
3

假设在redis中,存在以下键入字符串的键值对: key1 val1 key2 val2 我知道它们存储在表的内部。在redis中如何存储键值对?

这些键值对存储在一张表中吗? 或每个键值对有不同的表吗?

即,只有一个表既包含键值对,也包含一个表存储key1-val1和另一个表存储key2-val2?

回答

5

对于同一个Redis DB中的所有键值对,只有一个表。

实际上,键值对存储在一个大的哈希表中。

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; 

所有键值对存储在字典。