Redis is now a popular cache database, and it is generally found that it can store strings (string), hash tables (hashes), lists, collections (set), ordered collections (sorted set), and so on. Redis is a Key-value store, and value can contain many of the structures listed above, but key is a string. This means that key is a string type and value is one of the above types.
Since the storage instructions for each of these data structures are not the same in Redis, it seems that a single redis must differentiate the structure of the objects to be stored and then choose the appropriate instructions. But it's really bad to use it, and if you want to deposit multiple forms of value at once, I'm going to implement a variety of storage methods.
To facilitate the development and use of Redis introduced objects, namely object storage. Each of the above data structures is an object, so you only need to implement the object's storage in your project.
Each object in Redis has a redisobject structure in which the three properties associated with saving data are the type of the data being stored, the encoding attribute of the value encoding, and the pointer ptr property:
typedef struct redisobject{
//type
unsigned type:4;
Coding
unsigned encoding:4;
Pointer to the underlying implementation data structure
void *ptr
//virtual memory and other information etc....
} RobJ;
Type Constants |
the name of the object |
Type value |
Redis_string |
String Object |
String |
Redis_list |
List objects |
List |
Redis_hash |
Hash object |
Hash |
Redis_set |
Collection Object |
Set |
Redis_zset |
Ordered collection objects |
Zset |
Gets the type encoding directive that stores the value:
TYPE Key
For example, I'm storing a string value in Redis:
[root@iz8vb8r420ejxfron03cj7z ~]# redis-cli
127.0.0.1:6379> set msg "Rhett"
OK
127.0.0.1:6379> get Msg
"Rhett"
127.0.0.1:6379> type msg
string
Encoding Constants |
the name of the object |
Type value |
Redis_encoding_int |
Integer |
Int |
Redis_encoding_embstr |
Embstr coded simple dynamic string (SDS) |
List |
Redis_encoding_raw |
Simple dynamic string |
Raw |
Redis_encoding_ht |
Dictionary |
Hashtable |
Redis_encoding_linkedlist |
Double-ended linked list |
LinkedList |
Redis_encoding_ziplist |
Compression list |
Ziplist |
Redis_encoding_intset |
Integer collection |
Intset |
Redis_encoding_skiplist |
Jumping Tables and dictionaries |
Skiplist |
Examples in Redis:
[root@iz8vb8r420ejxfron03cj7z ~]# redis-cli
127.0.0.1:6379> set msg "Rhett"
OK
127.0.0.1:6379> Object encoding msg
"EMBSTR"
Reference book: Design and implementation of Redis