The concept of an object
Redis does not use data structure to achieve key-value database, but based on data structure to create an object system, including string object, list object, ordered collection object, hash object, collection object five types of objects.
The data structure of two objects
Typedef stuct redisObject {// Type unsigned type:4; // Unsigned encoding:4; // A pointer to the underlying data structure void * PTR; . unsigned lru:22 }Copy the code
-
type
String keys, list keys, ordered set keys, hash keys, set keys
-
coding
Corresponding to the underlying data structure
Two types of objects
-
String object
Int raw embstr
-
A list of objects
The encoding of a list object can be Ziplist or LinkedList if all string elements in the list are less than 64 bytes, or if the number of elements is less than 512 bytes, or if linkedList is used
-
The hash object
The encoding of a hash object can be ziplist or Hashtable (the underlying implementation is a dictionary, similar to Java hashMap). Use ziplist when all string elements in a hash object are less than 64 bytes, or the number of elements is less than 512, or hashtable is used otherwise
-
A collection of objects
Set object encodings can be intsert and hashtable insert is a set of integers. If hashtable is used then only the key of the hashtable is used and its value is null
-
Ordered set object
The ordered collection object encoding can be Ziplist or skiplist if the ordered collection has less than 128 elements or holds less than 64 bytes of elements in it, and the rest uses HashTable
3 Memory Reclamation
C language does not have the function of automatic memory collection, so Redis implements reference counting to achieve garbage collection, which is basically consistent with Java reference counting sending and collecting
Four-object Sharing
Create 1000 string objects in memory, these 1000 objects can actually be shared, such as different keys but the same value, so as to save space.
The idling duration of the object
The redisObject has an LRU attribute that records the last command access time of the object. The current time is subtracted from the LRU time of the key-value pair to obtain how long the key-value pair has not been used, namely its idle time.