Object handling mechanisms
Redis builds its own type system, and the main features of this system include:
The Redisobject object.
• Type checking based on the Redisobject object.
• Explicit polymorphic functions based on Redisobject objects.
• Mechanisms for distribution, sharing and destruction of Redisobject.
(Redisobject is actually just a struct type.) )
The definition of Redisobject is located in Redis.h:
/*
* Redis Object
*/
typedef struct REDISOBJECT {
Type
unsigned type:4;
Align Bits
unsigned notused:2;
Encoding method
unsigned encoding:4;
LRU time (relative to Server.lruclock)
unsigned lru:22;
Reference count
int refcount;
Point to the value of the object
void *ptr;
} robj;
Type, encoding, and PTR are the most important three properties.
The type records the types of values that the object holds, and its value may be one of the following constants (defined in Redis.h):
/*
* Object Type
*/
#define REDIS_STRING 0//String
#define REDIS_LIST 1//List
#define REDIS_SET 2//Collection
#define REDIS_ZSET 3//ordered set
#define REDIS_HASH 4//hash table
Encoding records the encoding of the value that the object holds, and its value may be one of the following constants (defined in Redis.h):
/*
* Object encoding
*/
#define REDIS_ENCODING_RAW 0//encoded as String
#define REDIS_ENCODING_INT 1//encoded as an integer
#define REDIS_ENCODING_HT 2//encoded as a hash table
#define REDIS_ENCODING_ZIPMAP 3//encoded as Zipmap
#define REDIS_ENCODING_LINKEDLIST 4//encoded as double-ended linked list
#define REDIS_ENCODING_ZIPLIST 5//encoded as compression list
#define REDIS_ENCODING_INTSET 6//encoded as an integer set
#define REDIS_ENCODING_SKIPLIST 7//coded as jump table
PTR is a pointer to the data structure that actually holds the value, which is determined by the type attribute and the encoding property.
For example, if the Type property of a redisobject is redis_list and the encoding property is Redis_encoding_linkedlist, then this object is a Redis list whose values are stored in a double-ended linked list. The PTR pointer points to the double-ended linked list;
Type checking and polymorphism of commands
When you execute a command that handles data types, Redis performs the following steps:
1. According to the given key, look in the database dictionary and it is like the corresponding redisobject, if not found, return null.
2. Check that the Type property of the Redisobject matches the types required to execute the command, and if not, the return type is incorrect.
3. According to the encoding specified by the Encoding property of Redisobject, select the appropriate operation function to handle the underlying data structure.
4. Returns the operation result of the data structure as the return value of the command.
Object sharing
Some objects are very common in redis, such as the command's return value OK, ERROR, wrongtype and other characters, in addition, some small range of integers, such as single-digit, 10-bit, hundreds of integers are very common.
To take advantage of this common situation, Redis uses a flyweight pattern internally: by pre-allocating some common value objects and sharing them among multiple data structures, the program avoids the hassle of duplication and saves some CPU time.
The Redis pre-allocated value objects have the following:
• Return values for various commands, such as OK when execution succeeds, error returned when executing an error, Rongtype returned when the type is wrong, queued returned when the command enqueued the transaction, and so on.
• include 0, all integers less than redis.h/redis_shared_integers (default value of Redis_shared_integers is 10000)
Note: Shared objects can only be used by data structures with pointers.
One thing to be reminded of is that shared objects can only be used by data structures with pointers, such as dictionaries and double-ended lists.
Like an integer collection and a compressed list that can only hold memory data structures for strings, integers, and so on, cannot use shared
Object.
Reference counting and destruction of objects
The Redis object system uses a reference counting technique to maintain and destroy objects, which
The operating mechanism is as follows:
• Each redisobject structure has a refcount attribute that indicates how many times the object has been referenced.
• When a new object is created, its RefCount property is set to 1.
• When sharing an object, Redis increases the object's RefCount by one.
• After an object is used, or after a reference to the shared object is canceled, the program refcount the object by one.
• When the refcount of an object drops to 0 o'clock, the redisobject structure, and the memory of the data structure it references, are freed.
String
String encoding
String types are encoded using Redis_encoding_int and Redis_encoding_raw, respectively: two
Redis_encoding_int uses a long type to hold a Long value.
Redis_encoding_raw uses the SDSHDR structure to hold the SDS (also known as char*), a long long, double, and long double type values.
In other words, in Redis, only values that can be represented as long are saved as integers, and other types of integers, decimals, and strings are saved using the SDSHDR structure.
Selection of encodings
The newly created string uses Redis_encoding_raw encoding by default, and when you save the string as a key or a value into the database, the program attempts to convert the string to Redis_encoding_int encoding.
Hash table
The Redis_hash (hash table) is an operand of a command such as Hset, Hlen, which uses redis_encoding_ziplist and Redis_encoding_ht two encoding methods:
Dictionary-encoded hash table
When the hash table is encoded using a dictionary, the program saves the key (key) of the Hashtable as the key to the dictionary, saving the value of the Hashtable as the value of the dictionary.
The key and value of the dictionary used by the hash table are string objects. Shows a hash table with three key-value pairs:
Compressed list-encoded hash table
When using the Redis_encoding_ziplist encoding hash table, the program forms the key-value pair structure needed to save the hash table by pushing the keys and values together into the compression list:
The newly added Key-value pair will be added to the footer of the compression list.
When a Find/delete or update operation is performed, the program navigates to the position of the key and then positions the value by the position of the key.
Selection of encodings
When you create a blank hash table, the program uses Redis_encoding_ziplist encoding by default, and when any of the following conditions are met, the program will encode from switch to REDIS_ENCODING_HT:
• A key or value in the hash table is longer than Server.hash_max_ziplist_value (the default value is 64).
• The number of nodes in the compression list is greater than server.hash_max_ziplist_entries (the default value is 512).
List
Redis_list (list) is an operand of a command such as Lpush, Lrange, which is encoded using both Redis_encoding_ziplist and redis_encoding_linkedlist:
Selection of encodings
When you create a new list, Redis uses redis_encoding_ziplist encoding by default, and when any of the following conditions are met, the list is converted to Redis_encoding_linkedlist encoding:
• Attempt to add a new string value to the list, and the string is longer than Server.list_max_ziplist_value (the default value is 64).
ziplist contains more nodes than Server.list_max_ziplist_entries (the default is 512).
Blocking
The Blpop, Brpop, and Brpoplpush three commands can cause the client to be blocked, and these commands are collectively referred to as the blocking primitives of the list.
Blocking primitives does not necessarily cause client blocking:
• Only when these commands are used for empty lists will they block the client.
• If the list being processed is not empty, they execute a nonblocking version of the Lpop, Rpop, or Rpoplpush commands.
Redis Learning notes-data types