Object of Redis depth, redis depth
Redis Object System
We have introduced all the major data structures used by Redis, such as simple dynamic strings (SDS), double-ended linked lists, dictionaries, compressed lists, and integer sets.
Redis does not directly use these data structures to implement key-Value Pair databases. Instead, it creates an object system based on these data structures, this system contains five types of objects: String object, list object, hash object, set object, and ordered set object. Each object uses at least one data structure we described earlier.
The Redis object system also implements the memory recycle mechanism and object sharing mechanism. Redis objects have access time record information, which can be used to calculate the idling duration of database keys. Keys with a long idling duration may be preferentially deleted by the server.
The SET command creates a new key-value pair, SET msg "hello world"
Contains two objects, key object and value object. Each object is represented by a redis object structure. Three attributes are related to the data storage. type, encoding: encoding, and ptr: pointer to the underlying data structure.
A key is always a String object. A value can be a string object, list object, hash object, set object, or sorted set object.
Redis> SET msg "hello world" String object
Redis> RPUSHnumbers 1 3 5 list object
Redis> HMSETprofile name Tom age 25 career Programmer hash
Redis> SADDfruits apple banana cherry collection
Redis> ZADDprice 8.5 apple 5.0 banana 6.0 cherry ordered collection
You can use the object encoding command to view the ENCODING of a database key value OBJECT.
String -- INT integer, RAW simple dynamic String
LIST--ZIPLIST compressed list, two-end linked list
HASH--ZIPLIST compression list, HT dictionary
SET--INTSET Integer Set, HT dictionary
ZSET--ZIPLIST compression list, SKIPLIST skip table, and Dictionary
The encoding attribute is used to set the encoding used by the object, instead of associating a fixed encoding for a specific type of object, which greatly improves the flexibility and efficiency of Redis, because Redis can set different encodings for an object based on different application scenarios, so as to optimize the efficiency of the object in a specific scenario.
For example, if the list object contains few elements, Redis uses the compressed list as the underlying implementation of the List object:
Because the compressed list saves more memory than the double-ended linked list, and when the number of elements is small, the compressed list stored in the memory in the continuous block mode can be loaded into the cache faster than the double-ended linked list.
As the list object contains more and more elements, the advantage of compressing the list to save elements gradually disappears, the object will transform the underlying implementation from the compressed list to a more powerful function, and is more suitable for storing a large number of elements on the double-ended linked list.