Learning notes on five types of Redis objects
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:
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;
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:
127.0.0.1: 6379> set name "hell world"
OK
127.0.0.1: 6379> get name
"Hell world"
127.0.0.1: 6379> type name
String
127.0.0.1: 6379> sadd sname "zhangsan" lisi "" wangwu"
(Integer) 3
127.0.0.1: 6379> smembers sname
1) "wangwu"
2) "zhangsan"
3) "lisi"
127.0.0.1: 6379> type sname
Set
127.0.0.1: 6379> lpush lname "zhang" "wang" "zhao" "li"
(Integer) 4
127.0.0.1: 6379> lrange lname 0-1
1) "li"
2) "zhao"
3) "wang"
4) "zhang"
127.0.0.1: 6379> type lname
List
Wagner. 0.0.1: 6379>
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"
Wagner. 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 100
OK
127.0.0.1: 6379> object refcount aa
(Integer) 2
127.0.0.1: 6379> set bb 100
OK
127.0.0.1: 6379> object refcount aa
(Integer) 3
127.0.0.1: 6379> object refcount bb
(Integer) 3
127.0.0.1: 6379> set cc 100
OK
127.0.0.1: 6379> object refcount aa
(Integer) 4
Wagner. 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) 1775
127.0.0.1: 6379> object idletime bb
(Integer) 1781
127.0.0.1: 6379> object idletime cc
(Integer) 1787
127.0.0.1: 6379> set cc "zhangsan"
OK
127.0.0.1: 6379> set cc "zhangsan"
OK
127.0.0.1: 6379> object idletime cc
(Integer) 3
127.0.0.1: 6379> object idletime cc
(Integer) 5
127.0.0.1: 6379> object idletime cc
(Integer) 6
127.0.0.1: 6379> object idletime cc
(Integer) 7
Wagner. 0.0.1: 6379>
You may need the following content:
Install and test Redis in Ubuntu 14.04
Redis cluster details
Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis
Redis series-installation, deployment, and maintenance
Install Redis in CentOS 6.3
Learning notes on Redis installation and deployment
Redis. conf
Redis details: click here
Redis: click here
This article permanently updates the link address: