redis string有3种编码格式:int、embstr(redis5.0版本长度小于等于44时)、 raw
redis hash键由字典构成,字典中包含哈希表结构
list列表对象的编码可以是ziplist或者linkedlist
string字符串对象的编码可以是int、raw或者embstr
hash哈希对象的编码可以是ziplist或者hashtable
set集合对象的编码可以是intset或者hashtable
zset有序集合的编码可以是ziplist或者skiplist
typedef struct dictht {
//
哈希表数组
dictEntry **table;
//
哈希表大小
unsigned long size;
//
哈希表大小掩码,用于计算索引值
//
总是等于size-1
unsigned long sizemask;
//
该哈希表已有节点的数量
unsigned long used;
} dictht;
typedef struct dict {
//
类型特定函数
dictType *type;
//
私有数据
void *privdata;
//
哈希表
dictht ht[2];
// rehash
索引
//
当rehash
不在进行时,值为-1
in trehashidx; /* rehashing not in progress if rehashidx == -1 */
} dict;
##zset结构(使用字典和跳表)
typedef struct zset {
dict *dict;
zskiplist *zsl;
} zset;
常用命令
##后台保存rdb
bgsave
##查看bgsave耗时等信息
info status
##查看redis键对应的值对象编码
OBJECT ENCODING key
##查看redis键对应的值对象的类型
TYPE key
WATCH\MULTI的使用:WATCH用于监听key,如果MULTI开启事务之前key被修改会导致整个事务执行失败(前提是事务中包含watch key操作)。
###事务开始前未修改key的情况,事务正常执行
127.0.0.1:6379> set key "java"
OK
127.0.0.1:6379> watch key
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> append key python
QUEUED
127.0.0.1:6379(TX)> append key jedis
QUEUED
127.0.0.1:6379(TX)> exec
1) (integer) 10
2) (integer) 15
127.0.0.1:6379> get key
"javapythonjedis"
127.0.0.1:6379>
###事务开始前修改key的情况,事务未成功执行
127.0.0.1:6379> set key "java"
OK
127.0.0.1:6379> watch key
OK
127.0.0.1:6379> append key python
(integer) 10
127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> append key jedis
QUEUED
127.0.0.1:6379(TX)> exec
(nil)
127.0.0.1:6379> get key
"javapython"
127.0.0.1:6379>