当前位置: 移动技术网 > IT编程>数据库>Redis > Redis字符串对象实用笔记

Redis字符串对象实用笔记

2019年07月18日  | 移动技术网IT编程  | 我要评论

字符串对象

字符串数据类型是redis里最常用的类型了,它的键和值都是字符串,使用起来非常的方便。虽然字符串数据类型的值都统称为字符串了,但是在实际存储时会根据值的不同自动选择合适的编码。字符串对象的编码一共有三种:int、raw、embstr。

redis对象

redis用统一的数据结构来表示一个对象,具体定义如下:

typedef struct redisobject {
 unsigned type:4;
 unsigned encoding:4;
 // 当内存超限时采用lru算法清除内存中的对象
 unsigned lru:lru_bits; /* lru time (relative to global lru_clock) or
       * lfu data (least significant 8 bits frequency
       * and most significant 16 bits access time). */
 // 该对象被引用数
 int refcount;
 // 对象的值指针
 void *ptr;
} robj;

其中type字段代表对象的类型,取值一共有7种:

/* a redis object, that is a type able to hold a string / list / set */

/* the actual redis object */
#define obj_string 0 /* 字符串对象. */
#define obj_list 1  /* 列表对象. */
#define obj_set 2  /* 集合对象. */
#define obj_zset 3  /* 有序集合对象. */
#define obj_hash 4  /* 哈希对象. */

/* the "module" object type is a special one that signals that the object
 * is one directly managed by a redis module. in this case the value points
 * to a modulevalue struct, which contains the object value (which is only
 * handled by the module itself) and the redismoduletype struct which lists
 * function pointers in order to serialize, deserialize, aof-rewrite and
 * free the object.
 *
 * inside the rdb file, module types are encoded as obj_module followed
 * by a 64 bit module type id, which has a 54 bits module-specific signature
 * in order to dispatch the loading to the right module, plus a 10 bits
 * encoding version. */
#define obj_module 5 /* 模块对象. */
#define obj_stream 6 /* 流对象. */

然后是encoding字段,代表着对象值的实际编码类型,取值一共有11种:

/* objects encoding. some kind of objects like strings and hashes can be
 * internally represented in multiple ways. the 'encoding' field of the object
 * is set to one of this fields for this object. */
#define obj_encoding_raw 0  /* 简单动态字符串 */
#define obj_encoding_int 1  /* long类型的整数 */
#define obj_encoding_ht 2  /* 字典 */
#define obj_encoding_zipmap 3 /* 压缩字典 */
#define obj_encoding_linkedlist 4 /* 不再使用的旧列表,使用双端链表. */
#define obj_encoding_ziplist 5 /* 压缩列表 */
#define obj_encoding_intset 6 /* 整数集合 */
#define obj_encoding_skiplist 7 /* 跳跃表和字典 */
#define obj_encoding_embstr 8 /* embstr编码的简单动态字符串 */
#define obj_encoding_quicklist 9 /* 编码为ziplist的列表 */
#define obj_encoding_stream 10 /* 编码为listpacks的基数树 */

前面已经提到字符串对象只用到了long类型的整数、简单动态字符串、embstr编码的简单动态字符串这三种编码。

obj_encoding_int

当字符串对象的值是一个整数且可以用long来表示时,字符串对象的编码就会是obj_encoding_int编码。

可以看到,当值非常大的时候还是用obj_encoding_raw来存储的。

obj_encoding_raw

当字符串对象的值是一个字符串且长度大于44字节时,字符串对象的编码就会是obj_encoding_raw编码。具体结构在下文。

obj_encoding_embstr

当字符串对象的值是一个字符串且长度小于等于44字节时,字符串对象的编码就会是obj_encoding_embstr编码。obj_encoding_embstr编码和obj_encoding_raw编码的区别主要有以下几点:

  • obj_encoding_raw编码的对象在分配内存时会分配两次,分别创建redisobject对象和sds对象。而obj_encoding_embstr编码则是一次就分配好。
  • 同样的,obj_encoding_raw编码的对象释放内存也需要两次,obj_encoding_embstr编码则是一次。
  • obj_encoding_embstr编码的数据都存储在连续的内存上,obj_encoding_raw编码则不是。
/* create a string object with embstr encoding if it is smaller than
 * obj_encoding_embstr_size_limit, otherwise the raw encoding is
 * used.
 *
 * the current limit of 44 is chosen so that the biggest string object
 * we allocate as embstr will still fit into the 64 byte arena of jemalloc. */
#define obj_encoding_embstr_size_limit 44
robj *createstringobject(const char *ptr, size_t len) {
 if (len <= obj_encoding_embstr_size_limit)
  return createembeddedstringobject(ptr,len);
 else
  return createrawstringobject(ptr,len);
}

sds

字符串是redis里非常常见的类型,而用c实现的redis和java不一样。在c里字符串是用长度为n+1的字符数组实现的,且使用空字符串'\0'作为结束符号。获取字符串的长度需要遍历一遍,找到空字符串'\0'才知道字符串的长度,复杂度是o(n)。

如果有一个长度非常大的字符串,单线程的redis获取它的长度就可能会阻塞很久,这是不能接受的,所以redis需要一种更高效的字符串类型。

redis实现了一个叫sds(simple dynamic string)的字符串类型,其中有两个变量来分别代表字符串的长度和字符数组未使用的字符数量,这样就可以用o(1)的复杂度来获取字符串的长度了,而且同样也是使用空字符串'\0'作为结束符号。

struct sdshdr {
 // 字符串长度
 int len;
 // 字符数组未使用的字符数量
 int free;
 // 保存字符串的字符数组
 char buf[];
}

扩容机制

sds在字符数组空间不足于容纳新字符串的时候会自动扩容。

如果把一个c字符串拼接到一个sds后面,当字符数组空间不足时,sds会先扩容到刚好可以容纳新字符串的长度,然后再扩充新字符串的空字符长度,最终sds的字符数组长度等于 2 * 新字符串 + 1(结束符号'\0')。不过当新字符串的大小超过1mb后,扩充的空字符长度大小会固定为1mb。

之所以会有这个机制,是因为redis作为一个nosql数据库,会频繁的修改字符串,扩容机制相当于给sds做了一个缓冲池。把sds连续增长n次字符串需要内存重分配n次优化成了sds连续增长n次字符串最多需要内存重分配n次,这其实和java里的stringbuilder实现思想是一样的。

后记

我看过两本关于redis的书,里面都是讲redis如何实战的,并没有讲redis的设计和实现。这也就导致了面试很尴尬,因为面试官最喜欢问原理相关的东西了,所以以后学习技术的时候不要从实战类的书籍开始了,还是先看懂原理比较好。


这是《redis设计与实现》里字符串一节的总结。


总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网