Redis数据类型-Strings

栏目: 数据库 · 发布时间: 7年前

内容简介:REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。Redis是一个开源的使用ANSI C语言编写的基于内存的可持久化的Key-Value数据库。string,list,hash,set,sorted set

Redis 简介

REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。

Redis是一个开源的使用ANSI C语言编写的基于内存的可持久化的Key-Value数据库。

Redis的数据类型

string,list,hash,set,sorted set

String

string是 redis 最基本的类型,而且string类型是二进制安全的。意思是redis的string可以包含任何数据,比如jpg图片或者序列化的对象。

与String相关的常用命令:

  • SET:为一个key设置value,可以配合EX/PX参数指定key的有效期,通过NX/XX参数针对key是否存在的情况进行区别操作,时间复杂度O(1)
  • GET:获取某个key对应的value,时间复杂度O(1)
  • GETSET:为一个key设置value,并返回该key的原value,时间复杂度O(1)
  • MSET:为多个key设置value,时间复杂度O(N)
  • MSETNX:同MSET,如果指定的key中有任意一个已存在,则不进行任何操作,时间复杂度O(N)
  • MGET:获取多个key对应的value,时间复杂度O(N)

如果保存的是整数值并且可以用long表示,那么编码会设置为INT,那么还有额外的命令

  • INCR:将key对应的value值自增1,并返回自增后的值。只对可以转换为整型的String数据起作用。时间复杂度O(1)
  • INCRBY:将key对应的value值自增指定的整型数值,并返回自增后的值。只对可以转换为整型的String数据起作用。时间复杂度O(1)
  • DECR/DECRBY:同INCR/INCRBY,自减函数。

用法1

 1 127.0.0.1:6379> set test-string hi
 2 OK
 3 127.0.0.1:6379> get test-string
 4 "hi"
 5 127.0.0.1:6379> getset test-string2 "how are you"
 6 (nil)
 7 127.0.0.1:6379> getset test-string2 "how are you"
 8 "how are you"
 9 127.0.0.1:6379> mset test-string hello test-string2 "good to see you"
10 OK
11 127.0.0.1:6379> get test-string
12 "hello"
13 127.0.0.1:6379> get test-string2
14 "good to see you"

用法2

 1 127.0.0.1:6379> mget test-string test-string2
 2 1) "hello"
 3 2) "good to see you"
 4 127.0.0.1:6379> append test-string2 "!"
 5 (integer) 16
 6 127.0.0.1:6379> get test-string2
 7 "good to see you!"
 8 127.0.0.1:6379> set test-string2 20
 9 OK
10 127.0.0.1:6379> incr test-string2
11 (integer) 21
12 127.0.0.1:6379> 

string的encoding

字符串对象的编码可以是 INT、RAW 或 EMBSTR。如果保存的是整数值并且可以用long表示,那么编码会设置为INT。当字符串值得长度大于 39 字节使用raw 并且用sds来保存,小于等于39字节使用embstr。

127.0.0.1:6379> object encoding test-string 
"embstr"
127.0.0.1:6379> object encoding test-string2
"int"
127.0.0.1:6379> 
1 127.0.0.1:6379> set l39 111111111122222222223333333333444444444
2 OK
3 127.0.0.1:6379> object encoding l39
4 "embstr"
5 127.0.0.1:6379> set l40 1111111111222222222233333333334444444444
6 OK
7 127.0.0.1:6379> object encoding l40
8 "raw"
9 127.0.0.1:6379> 

encoding的变换

1 127.0.0.1:6379> append test-string2 " is a raw"
2 (integer) 11
3 127.0.0.1:6379> object encoding test-string2
4 "raw"

向一个保存整数值的字符串对象追加了一个字符串,程序会先将之前保存的整数值 转换为字符串值  , 然后再执行追加操作, 操作的执行结果就是一个 raw 编码的、保存了字符串值的字符串对象。

 1 127.0.0.1:6379> set test-string2 "I'm a embstr"
 2 OK
 3 127.0.0.1:6379> set test-string3 "I'm a embstr"
 4 OK
 5 127.0.0.1:6379> get test-string3
 6 "I'm a embstr"
 7 127.0.0.1:6379> object encoding test-string3
 8 "embstr"
 9 127.0.0.1:6379> append test-string3 " I changed to raw"
10 (integer) 29
11 127.0.0.1:6379> get test-string3
12 "I'm a embstr I changed to raw"
13 127.0.0.1:6379> object encoding test-string3
14 "raw"

embstr 编码的字符串对象实际上是只读的, 当我们对  embstr 编码的字符串对象执行任何修改命令时, redis会先将encoding从  embstr 转换成  raw , 然后再执行修改命令; 因为这个原因,  embstr 编码的字符串对象在执行修改命令之后, 总会变成一个  raw 编码的字符串对象

源码

先看一下sds

 1 struct __attribute__ ((__packed__)) sdshdr5 {
 2     unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
 3     char buf[];
 4 };
 5 struct __attribute__ ((__packed__)) sdshdr8 {
 6     uint8_t len; /* used */
 7     uint8_t alloc; /* excluding the header and null terminator */
 8     unsigned char flags; /* 3 lsb of type, 5 unused bits */
 9     char buf[];
10 };
 1 /* Create a string object with encoding OBJ_ENCODING_RAW, that is a plain
 2  * string object where o->ptr points to a proper sds string. */
 3 robj *createRawStringObject(const char *ptr, size_t len) {
 4     return createObject(OBJ_STRING, sdsnewlen(ptr,len));
 5 }
 6 
 7 /* Create a string object with encoding OBJ_ENCODING_EMBSTR, that is
 8  * an object where the sds string is actually an unmodifiable string
 9  * allocated in the same chunk as the object itself. */
10 robj *createEmbeddedStringObject(const char *ptr, size_t len) {
11     robj *o = zmalloc(sizeof(robj)+sizeof(struct sdshdr8)+len+1);
12     struct sdshdr8 *sh = (void*)(o+1);
13 
14     o->type = OBJ_STRING;
15     o->encoding = OBJ_ENCODING_EMBSTR;
16     o->ptr = sh+1;
17     o->refcount = 1;
18     if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
19         o->lru = (LFUGetTimeInMinutes()<<8) | LFU_INIT_VAL;
20     } else {
21         o->lru = LRU_CLOCK();
22     }
23 
24     sh->len = len;
25     sh->alloc = len;
26     sh->flags = SDS_TYPE_8;
27     if (ptr == SDS_NOINIT)
28         sh->buf[len] = '\0';
29     else if (ptr) {
30         memcpy(sh->buf,ptr,len);
31         sh->buf[len] = '\0';
32     } else {
33         memset(sh->buf,0,len+1);
34     }
35     return o;
36 }
37 
38 /* Create a string object with EMBSTR encoding if it is smaller than
39  * OBJ_ENCODING_EMBSTR_SIZE_LIMIT, otherwise the RAW encoding is
40  * used.
41  *
42  * The current limit of 44 is chosen so that the biggest string object
43  * we allocate as EMBSTR will still fit into the 64 byte arena of jemalloc. */
44 #define OBJ_ENCODING_EMBSTR_SIZE_LIMIT 44
45 robj *createStringObject(const char *ptr, size_t len) {
46     if (len <= OBJ_ENCODING_EMBSTR_SIZE_LIMIT)
47         return createEmbeddedStringObject(ptr,len);
48     else
49         return createRawStringObject(ptr,len);
50 }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

jQuery实战(第2版)

jQuery实战(第2版)

[美]Bear Bibeault、[美]Yehuda Katz / 三生石上 / 人民邮电出版社 / 2012-3 / 69.00元

jQuery 是目前最受欢迎的JavaScript/Ajax 库之一,能用最少的代码实现最多的功能。本书全面介绍jQuery 知识,展示如何遍历HTML 文档、处理事件、执行动画、给网页添加Ajax 以及jQuery UI 。书中紧紧地围绕“用实际的示例来解释每一个新概念”这一宗旨,生动描述了jQuery 如何与其他工具和框架交互以及如何生成jQuery 插件。 本书适合各层次Web 开发人......一起来看看 《jQuery实战(第2版)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具