Redis Learning Note (9)---object robject

Source: Internet
Author: User

Robject

Previously, the basic data structure of Redis was briefly introduced, including string, linked list, hash table, integer collection, compression lists, compression dictionaries, etc., but Redis does not directly use these data structures to implement Key-value to the database. Instead, an object robject is created for each object based on these data structures. The Robject object then selects the appropriate underlying data structure to store the data, based on the datatype.

Robject is defined as follows:
typedefstruct redisObject {    unsigned type:4;    unsigned encoding:4;    unsigned/* lru time */    int refcount;    void *ptr;} robj;
  • Type:4bit, type of object
  • Encoding:4bit, how the object is encoded
  • Lru:24bit, recording access time
  • RefCount: Reference count
  • PTR: pointing to specific data
1) Type

There are 5 types of objects that can be selected:

/* Object types */#define REDIS_STRING 0#define REDIS_LIST 1#define REDIS_SET 2#define REDIS_ZSET 3#define REDIS_HASH 4

In Redis, the keys in Key-value pairs are always string types, and only values can be one of several types, so when we call a key a "string key", it means that the value of the key corresponds to the string type.

2) Encoding method

There are 9 ways to encode objects, each of which can be encoded in several different ways:

#Define redis_encoding_raw 0/* RAW representation * /#Define redis_encoding_int 1/* encoded As Integer * /#Define REDIS_ENCODING_HT 2 */encoded as hash table * /#Define REDIS_ENCODING_ZIPMAP 3 */encoded as Zipmap * /#Define redis_encoding_linkedlist 4/* encoded as regular linked list * /#Define redis_encoding_ziplist 5 */encoded as Ziplist * /#Define Redis_encoding_intset 6 */encoded as Intset * /#Define redis_encoding_skiplist 7 */encoded as Skiplist * /#Define redis_encoding_embstr 8/* Embedded SDS string ENCODING * /

For example, when we execute the set msg "Hello" command on the client, a Key-value pair is generated in the database. Redis creates an object Robject for key and value, the type of the object is string type redis_string, and the object is encoded as REDIS_ENCODING_EMBSTR. When the string length is longer, the encoding of the object becomes Redis_encoding_raw
  

Command

http://doc.redisfans.com/
This site summarizes the commands in the Redis database.
It can be found that for different commands, the underlying is implemented in different data structures, that is, different commands can cause different types of objects.
When executing commands such as set, get, and so on, the resulting object is of type string. The bottom layer then chooses the appropriate encoding based on the length of the object.
When you execute the hset, Hget command, the generated object is the hash table type, and then the different encoding is selected based on the specific object.



The source code referenced in this article is all from the Redis3.0.7 version

Redis Learning Resources:
https://github.com/huangz1990/redis-3.0-annotated
Redis Design and Implementation (second edition)
  
 
  
  

Redis Learning Note (9)---object robject

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.