Learn notes on Redis five types of objects

Source: Internet
Author: User

When we use Redis, we have direct access to string objects (string), list objects (lists), hash objects (hashes), collection objects (set), Ordered collection objects (SortedSet), five types of objects, basic commands such as: string (get Set) List (Lpush rpush lpop rpop lrange) Hash (hget hset hlen hgetall) Set (Sadd smembers Smov) SortedSet (Zadd zrange) and so on.

Let's look at the data structure of the Redis object first:

<span style= "FONT-SIZE:14PX;" >typedef struct Redisobject {    unsigned type:4;    unsigned encoding:4;    unsigned lru:redis_lru_bits; /* LRU time (relative to Server.lruclock) */    int refcount;    void *ptr;} Robj;</span>
where Type property is the type of the Record object, which can be redis_string redis_list redis_hash redis_set redis_zset Five constants

You can use the command type to see what type a key belongs to, as follows:

<span style= "FONT-SIZE:14PX;" >127.0.0.1:6379> set name "Hell World" ok127.0.0.1:6379> get Name "Hell World" 127.0.0.1:6379> type Namestring127.0.0.1:6379> sadd sname "Zhangsan" "Lisi" "Wangwu" (integer) 3127.0.0.1:6379> smembers sname1) " Wangwu "2)" Zhangsan "3)" Lisi "127.0.0.1:6379> type snameset127.0.0.1:6379> lpush lname" Zhang "" Wang "" Zhao "" Li "( Integer) 4127.0.0.1:6379> lrange lname 0-11) "Li" 2) "Zhao" 3) "Wang" 4) "Zhang" 127.0.0.1:6379> type lnamelist127.0.0 .1:6379></span>

In addition, the encoding property records what encoding the object uses, and this needs to be explained by the fact that Redis can set many different implementations of the data structure for different usage scenarios, such as hash type objects, and different underlying implementations for different data. If the keys in the hash are less, and the stored elements are short strings, the underlying implementation is the compression list, and conversely, using the dictionary as the underlying implementation, you can use the object encoding command to see what the underlying implementation is used.

127.0.0.1:6379> object Encoding Name "EMBSTR" 127.0.0.1:6379> object Encoding Sname "Hashtable" 127.0.0.1:6379> Object encoding lname "Ziplist" 127.0.0.1:6379>
What are the benefits of doing this? You can increase flexibility and efficiency by setting different encodings for objects based on different usage scenarios, and optimizing the efficiency of objects in a given scenario.

What is the next RefCount property for? Designed to two content, one is memory recycling, one is object sharing

This counter is when an object is created, Refcount+1, when the object is referenced by a new program, then counter +1, vice versa-1, but this counter is 0, indicating that the resource can be released

As for object sharing, "Redis values are shared with string objects that contain integer types"

127.0.0.1:6379> set AA 100ok127.0.0.1:6379> object refcount aa (integer) 2127.0.0.1:6379> set BB 100ok127.0.0.1 :6379> object refcount aa (integer) 3127.0.0.1:6379> object refcount bb (integer) 3127.0.0.1:6379> set cc 100ok127 .0.0.1:6379> object refcount aa (integer) 4127.0.0.1:6379>
There are two properties in the Redisobject data structure that are not introduced, one is the PTR, the other is the LRU

PTR is an object pointer that points to the underlying implementation data structure of the object, which is determined by the encoding property.

LRU attribute design to an object idling time long concept object Idletime command can get the idle time of a certain key, that is, how long the command has not been accessed, the LRU record is the last time to be visited

127.0.0.1:6379> object idletime aa (integer) 1775127.0.0.1:6379> object idletime bb (integer) 1781127.0.0.1:6379 > Object idletime cc (integer) 1787127.0.0.1:6379> set cc "Zhangsan" ok127.0.0.1:6379> set cc "Zhangsan" OK127.0.0 .1:6379> object idletime cc (integer) 3127.0.0.1:6379> object idletime cc (integer) 5127.0.0.1:6379> object Idletime cc (integer) 6127.0.0.1:6379> object idletime cc (integer) 7127.0.0.1:6379>

======================================================================================================


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Learn notes on Redis five types of objects

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.