Learning notes for five types of Redis objects and five learning notes for redis

Source: Internet
Author: User

Learning notes for five types of Redis objects and five learning notes for redis

When using Redis, we directly access String, List, Hash, and Set objects ), sortedSet. Basic commands include String (get set) List (lpush rpush lpop rpop lrange) Hash (hget hset hlen hgetall) set (sadd smembers smov) SortedSet (Zadd Zrange.

First, let's take a look at the data structure of the redis object:

<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>
The type attribute is the record object type. It can be the REDIS_STRING REDIS_LIST REDIS_HASH REDIS_SET REDIS_ZSET constants.

You can use the command type to view the type of a key, as shown below:

<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 attribute records the encoding used by the object. This requires note that redis can set different data structure implementations for the object in different use cases. For example, all objects are hash objects, different underlying implementations can be used for different data. If there are few keys in the hash and the stored elements are strings that are relatively short, the underlying implementation is to compress the list. Conversely, the dictionary is used as the underlying implementation. You can use the object encoding command to view the underlying implementation.

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 so? It improves flexibility and efficiency, sets different encodings for Objects Based on different application scenarios, and optimizes the efficiency of objects in a specific scenario.

What is the next use of the refcount attribute? Two contents are designed: memory collection and object sharing.

This counter is when an object is created, refcount + 1, when the object is referenced by a new program, then the counter is + 1, and the opposite is-1, but this counter is 0, indicates that the resource can be released.

For object sharing, [share an integer String object with a redis value]

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>
Two other attributes are not described in the redisObject data structure. One is ptr and the other is lru.

Ptr is an object pointer pointing to the underlying implementation data structure of the object, which is determined by the encoding attribute.

The lru attribute is designed to define the concept of an object idling duration. The object idletime command can obtain the idling duration of a key, that is, how long the command has not been accessed, lru records the last accessed time.

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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.