Redis可以存储键和5种不同类型的数据结构,它们是STRING(字符串),lIST(列表),SET(集合),HASH(散列表),ZSET(有序集合)。
1.字符串(String)可以是字符串、整数或者浮点数。Redis对整个字符串或者其中部分执行操作。对整数和浮点数执行自增或者自减操作。
2.链表(List)链表的每个节点都包含一个字符串.Redis可以从链表两端插入或者弹出元素。
/*链表节点*/
typedef struct listNode {
struct listNode *prev;
struct listNode *next;
void *value;
} listNode;
/*节点迭代器*/
typedef struct listIter {
listNode *next;
int direction;
} listIter;
/*链表*/
typedef struct list {
listNode *head;/*头节点*/
listNode *tail;/*尾节点*/
void *(*dup)(void *ptr);
void (*free)(void *ptr);
int (*match)(void *ptr, void *key);
unsigned long len;/*链表长度*/
} list;
3.集合(SET)包含字符串的无序收集器,集合中的字符串唯一。
4.散列表(HASH)包含键值对的无序列表
5.有序集合(ZSET)元素按照分值的大小顺序排列。
typedef struct zset {
dict *dict;/*字典*/
zskiplist *zsl;
} zset;
5.1字典项定义
/*dictEntry是字典的一条记录*/
typedef struct dictEntry {
void *key;
union {
void *val;
uint64_t u64;
int64_t s64;
double d;
} v;
struct dictEntry *next;/*下一条记录*/
} dictEntry;
typedef struct dictType {
uint64_t (*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];
long rehashidx; /* rehashing not in progress if rehashidx == -1 */
unsigned long iterators; /* number of iterators currently running */
} dict;