1 Redis 記憶體儲存結構
本文是基於 Redis-v2.2.4 版本進行分析.
1.1 Redis 記憶體儲存總體結構
Redis 是支援多key-value資料庫(表)的,並用 RedisDb 來表示一個key-value資料庫(表). redisServer 中有一個 redisDb *db; 成員變數, RedisServer 在初始化時,會根據設定檔的 db 數量來建立一個 redisDb 數組. 用戶端在串連後,通過 SELECT 指令來選擇一個 reidsDb,如果不指定,則預設是redisDb數組的第1個(即下標是 0 ) redisDb. 一個用戶端在選擇 redisDb 後,其後續操作都是在此 redisDb 上進行的. 下面會詳細介紹一下 redisDb 的記憶體結構.
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/2102144292-0.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011710.jpg" />
redis 的記憶體儲存結構
redisDb 的定義:
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 *io_keys; /* Keys with clients waiting for VM I/O */dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */int id;} redisDb;
redisDb 中 ,dict 成員是與實際儲存資料相關的. dict 的定義如下:
typedef struct dictEntry{void *key;void *val;struct dictEntry *next;} dictEntry;typedef struct dictType{unsigned int (*hashFunction)(const void *key);void *(*keyDup)(void *privdata, const void *key);void *(*valDup)(void *privdata, const void *obj);int (*keyCompare)(void *privdata, const void *key1, const void *key2);void (*keyDestructor)(void *privdata, void *key);void (*valDestructor)(void *privdata, void *obj);} dictType;/* This is our hash table structure. Every dictionary has two of this as we* implement incremental rehashing, for the old to the new table. */typedef struct dictht{dictEntry **table;unsigned long size;unsigned long sizemask;unsigned long used;} dictht;typedef struct dict{dictType *type;void *privdata;dictht ht[2];int rehashidx; /* rehashing not in progress if rehashidx == -1 */int iterators; /* number of iterators currently running */} dict;
本文出自 “螞蟻窩” 部落格,請務必保留此出處http://feihan21.blog.51cto.com/1364153/1300007