Redis Object System
All of the major data structures used by Redis are described earlier, such as simple dynamic string (SDS), double-ended linked list, dictionaries, compression lists, integer collections, etc.
Redis does not directly use these data structures to implement key-value pairs of databases, but instead creates an object system based on these data structures, which contains five types of objects, such as String objects, list objects, hash objects, collection objects, and ordered collection objects. Each of these objects uses at least one of the data structures we described earlier.
The Redis object system also implements the mechanism of memory recycling and object sharing. The Redis object has access time logging information that can be used to calculate the idle time of the database keys, and those keys that are larger at idling time may be first removed by the server.
The SET command creates a new key-value pair, set msg "Hello World"
Contains two objects, key objects, and value objects, each of which is represented by a Redis object structure, with 3 properties related to the save data, type types, encoding: encoding and PTR: pointers to the underlying implementation data structures.
The key is always a string object, and the value can be one of a string object, a list object, a hash object, a collection object, or an ordered collection object.
Redis > SET msg "Hello World" string Object
Redis >rpushnumbers 1 3 5 list objects
redis> hmsetprofile name Tom age career Programmer Hash
Redis>saddfruits Apple Banana Cherry Collection
Redis>zaddprice 8.5 Apple 5.0 Banana 6.0 Cherry ordered collection
Use the object Encoding command to view the encoding of a value object for a database key
String--int shaping, raw simple dynamic string
List--ziplist compression list, LinkedList double-ended linked lists
Hash--ziplist compression list, HT dictionary
Set--intset integer set, HT dictionary
Zset--ziplist compression lists, skiplist jumping tables, and dictionaries
Using the Encoding property to set the encoding used by an object rather than associating a fixed encoding for a particular type of object greatly improves the flexibility and efficiency of Redis because Redis can set different encodings for an object based on different usage scenarios. This optimizes the efficiency of the object in a given scenario.
For example, when the list object contains fewer elements, Redis uses a compressed list as the underlying implementation of the List object:
Because the compression list is more memory-efficient than the double-ended lists, and when the number of elements is low, a compressed list saved in memory in contiguous chunks can be loaded into the cache more quickly than a double-ended linked table.
As the list object contains more and more elements, the advantage of using a compressed list to save elements fades away, and the object moves the underlying implementation from the compression list to the more powerful, and more suitable, double-ended linked lists that hold a large number of elements.
Redis Dive into the object