redis存储数据的基本类型有:string
(字符串类型)、hash
(散列类型)、list
(列表类型)、set
(集合类型)、zset
(有序集合类型)。
redis中的每个对象都由一个redisObject结构来表示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/* * Redis 对象 */ typedef struct redisObject { // 类型 unsigned type: 4 ; // 不使用(对齐位) unsigned notused: 2 ; // 编码方式 unsigned encoding: 4 ; // LRU 时间(相对于 server.lruclock) unsigned lru: 22 ; // 引用计数 int refcount; // 指向对象的值 void *ptr; } robj; |
和保存数据有关的三个属性是type
、encoding
和ptr
属性。
对象类型分为下边这几个,我们是用type命令可以查看对象类型
1
2
3
4
5
6
7
8
|
/* * 对象类型 */ #define REDIS_STRING 0 #define REDIS_LIST 1 #define REDIS_SET 2 #define REDIS_ZSET 3 #define REDIS_HASH 4 |
对象的ptr
指针指向对象的底层实现数据结构,这些数据结构由对象的encoding
属性决定。
编码类型:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/* * 对象编码 * * 像 String 和 Hash 这样的对象,可以有多种内部表示。 * 对象的 encoding 属性可以设置为以下域的任意一种。 */ #define REDIS_ENCODING_RAW 0 /* Raw representation 简单动态字符串*/ #define REDIS_ENCODING_INT 1 /* Encoded as integer long类型的整数*/ #define REDIS_ENCODING_HT 2 /* Encoded as hash table 字典*/ #define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap 压缩字典*/ #define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list 双端链表*/ #define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist 压缩列表*/ #define REDIS_ENCODING_INTSET 6 /* Encoded as intset 整数集合*/ #define REDIS_ENCODING_SKIPLIST 7 /* Encoded as skiplist 跳跃表和字典*/ #define REDIS_ENCODING_EMBSTR 8 /* Embedded sds string encoding embstr编码的简单动态字符串*/ |
可以通过object encoding
命令查看数据库键的值对象的编码
通过encoding
属性来设定对象所使用的编码,而不是为特定类型的对象关联一种固定的编码,极大提升了redis的灵活性和效率,根据不同的使用场景来为对象设置不同的编码
不同数据结构的编码方式:
到此这篇关于Java中redis的基本类型的文章就介绍到这了,更多相关redis类型内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.51cto.com/u_15460453/5115535