Redis five kinds of structural analysis data

Redis is an open source Key-Value storage engine that supports string, hash, list, set and sorted set other value types. Because of its superior performance, stability and rich data types, it is widely used for various scenes stored k v /. Even in some distributed caching system, also used it as the underlying storage engine. This paper redis most commonly used type of data to analyze, thus allowing users to select the appropriate data type in each scene, and thus play its best advantage.

1、String

String is the most commonly used type of data, Common k / v storage can be classified as such, it is defined by the inner sds.h. On the type of design, in addition to containing buf character array, and the remaining length of buf also additional storage space actual string, thereby providing a more flexible management. In particular should be mentioned that, redis own memory management, which itself may have more information on memory, improving performance and memory allocation basis as other functions. In addition, the structure also provides some of the operations related to the function string, feature-rich, to achieve a transparent, easy to use.

Commonly used commands:

 

command

time complexity

description

return value

APPEND

O (1)

If the Key already exists, APPEND command to append data to the end of parameter Value of Value of existing. If the Key does not exist, APPEND command will create a new Key / Value.

After adding the length of Value

INCR

O (1)

The atoms of the specified Key Value is incremented by one. If the Key does not exist, its initial value is 0, a value 1 after incr. If the value of Value can not be converted to an integer value, the operation will fail and return an appropriate error message.

Value value incremented

INCRBY

O (1)

The atoms of the specified Key Value increase increment. If the Key does not exist, its initial value is 0, a value after incrby increment. If the value of Value can not be converted to an integer value, the operation will fail and return an appropriate error message.

Value increased value

DECR

O (1)

The atoms of the specified Key Value decremented. If the Key does not exist, its initial value is 0, a value of -1 after decr. If the value of Value can not be converted to an integer value, such as the Hello, the operation will fail and return an appropriate error message.

Value of decreasing value.

DECRBY

O (1)

The specified Key Value atomic decrease decrement. If the Key does not exist, its initial value is 0, a value after decrby -decrement. If the value of Value can not be converted to an integer value, such as the Hello, the operation will fail and return an appropriate error message.

Value value decreases.

GET

O (1)

Gets the specified Key Value. If Value is not a string type associated with this Key, Redis will return an error message.

Associated with this Key Value, if the Key does not exist, it returns nil.

SET

O (1)

Key holder sets the specified string Value, if the Key already exists, its previous value is overwritten.

Return "OK"

 

2、Hash

Hash type field is a string value and the mapping table is defined by dict.h. Key may correspond to a plurality of field, a field corresponding to a value. An object is stored as a hash type, each field are compared to stored as string type could save memory, and can be more convenient access to the entire object. hash value is stored inside a hashMap, and provides direct access to members of the Map interface.

For example: we want to store a user details, you set a userID as a key for each user, and stored value contains the name, including name, age, age, date of birth birth, address and other information addr. If ordinary k / v storage structure, and in general is userID value as a combination of each of a key, and the corresponding content as a value. As the command

mset user1_name "Joe Smith" user1_age 18 user1_addr "Hangzhou"

This model, the large amount of data when there is an obvious waste of memory. The hash structure redis provided a good solution to this problem. For the above example, by executing the command hmset user1 name "John Doe" age 18 addr "Hangzhou" 

As user1 key, the name, age, birth as an attribute and the like. In such case, by the way + key field, to avoid duplication of stored data.

Hash mentioned structure, I have to say that the key conflict resolution expansion program approach and hash tables. In Redis, when a conflict occurs, it takes the chain conflict resolution. Further, redis a dual structure hash table (ht [2]). Simply put, the initial k / v saved ht [0], when the conflict is serious, ht [1] in the bucket size is set to ht [0] twice, and gradually ht [0] in elements migrate to ht [1]. After completion of migration until all elements, then ht [0] and ht [1] exchange address.

Commonly used commands:

 

command

time complexity

description

return value

HSET

O (1)

Setting Field / Value pair specified Key, if Key is not present, the command to create a new Key parameters Field / Value pair, if the parameters in the Key Field already exists, it is overwritten with the new value of the original It has a value.

1 represents the new value of a new Field was provided, Field 0 indicates already exists, the old value with the new coverage value.

hget

O (1)

Returns the specified Key Field of the associated value.

Returns the associated value of the parameter in the Field, if the parameters or Key Field does not exist, it returns nil.

HEXISTS

O (1)

Analyzing the designation of designated Field Key exists.

1 indicates the presence and 0 indicates parameters Key Field or absent.

 

3、List

List is implemented as a doubly linked list that supports reverse insertion, search and traversal, defined by adlist.h. We can push, pop operations to add or remove elements from the head end of the list. When such a data structure needed queue, List provide very rich interface, widely used buffer queue, the message queue and other scenarios. Data structures learned friend, this is certainly not unfamiliar.

Commonly used commands:

 

command

time complexity

description

return value

LPUSH

O (1)

Values ​​of all parameters given in the insert associated with the specified Key List Value of the head. If the Key does not exist, the command creates an empty list associated with the Key before insertion then after insertion of data from the head of the list. If Value is not a list of the type of the key, the command will return the associated error message.

After inserting the number of elements in the list.

Hrifusः

O (1)

Values ​​of all parameters given in the inserted tail specified associated List Value of the Key. If the Key does not exist, the command creates an empty linked list associated with the Key before inserting, then after the data is inserted from the end of the list. If Value is not a list of the type of the key, the command will return the associated error message.

After inserting the number of elements in the list.

LPOP

O (1)

Pop back and specifies the first element of the linked list associated Key, i.e., the head element. If the Key does not exist, it returns nil.

The head of the list elements.

RPOP

O (1)

Go back and pop the last element of the specified Key associated with the list, that is trailing elements. If the Key does not exist, it returns nil.

List element tail.

Fill

O (1)

Returns the specified number associated Key elements in the list, if the Key does not exist, 0 is returned. If the type of the Key Value is not associated with the list, the associated error message is returned.

The number of elements in the list.

 

4、Set

Set collection type offered is a list of features, but also can automatically go heavy. Its internal implementation is actually a null value value HashMap, and therefore it is also used to enable the data to a simple weight determination, and the like whether or not a member in the other set of interfaces becomes important. When some scenarios need to store a list, and do not want to duplicate data, you can choose to set this structure to deal with.

Commonly used commands:

 

command

time complexity

description

return value

SADD

O (N)

The time complexity of N represents the number of operation members. If the insertion process with the parameters already exist in some member in the Set, the member will be ignored, while other members will still be properly inserted. If the command is executed before the Key does not exist, the command will create a new Set, then the parameters members thereafter successively inserted. If the Key is not Set Value type, the command will return the associated error message.

The operation of the actual number of members inserted.

SREM

O (N)

The time complexity of N represents the number of members to be deleted. Deleted from the Set the parameters associated with the Key of members specified, non-existent parameters members will be ignored if the Key does not exist, it will be treated as an empty Set processing.

The actual number of members removed from the Set, if there is no return to zero.

SCARD

O (1)

Gets Set the number of members.

Set the number of members in return, if the Key does not exist, 0 is returned.

SISMEMBER

O (1)

Determine whether the parameters specified member already exists in the collection and Key Set associated.

1表示已经存在,0表示不存在,或该Key本身并不存在。

 

5、Sort Set

Sort Set的功能与Set非常相似,只不过它是可以通过用户提供一个优先级参数来实现自动排序的,而Set结构不会做自动排序。Sort set内部使用HashMap和SkipList来实现数据的有序存储,保证查询的效率以及元素有序性。在某些应用场景,比如需要为某个班级的学生根据成绩来排序,就可以将优先级参数设置为成绩分数,这样在插入到这个结构时,就可以实现自动的排序。

 

 

命令 

时间复杂度 

描述 

返回值 

APPEND 

O(1) 

如果该Key已经存在,APPEND命令将参数Value的数据追加到已存在Value的末尾。如果该Key不存在,APPEND命令将会创建一个新的Key/Value。

追加后Value的长度 

INCR 

O(1) 

将指定Key的Value原子性的递增1。如果该Key不存在,其初始值为0,在incr之后其值为1。如果Value的值不能转换为整型值,该操作将执行失败并返回相应的错误信息。 

递增后的Value值 

INCRBY 

O(1) 

将指定Key的Value原子性的增加increment。如果该Key不存在,其初始值为0,在incrby之后其值为increment。如果Value的值不能转换为整型值,该操作将执行失败并返回相应的错误信息。 

增加后的Value值 

DECR 

O(1) 

将指定Key的Value原子性的递减1。如果该Key不存在,其初始值为0,在decr之后其值为-1。如果Value的值不能转换为整型值,如Hello,该操作将执行失败并返回相应的错误信息。 

递减后的Value值。 

DECRBY 

O(1) 

将指定Key的Value原子性的减少decrement。如果该Key不存在,其初始值为0,在decrby之后其值为-decrement。如果Value的值不能转换为整型值,如Hello,该操作将执行失败并返回相应的错误信息。 

减少后的Value值。 

GET 

O(1) 

获取指定Key的Value。如果与该Key关联的Value不是string类型,Redis将返回错误信息。 

与该Key相关的Value,如果该Key不存在,返回nil。 

SET 

O(1) 

设定该Key持有指定的字符串Value,如果该Key已经存在,则覆盖其原有值。 

返回"OK" 

 


 

Guess you like

Origin www.cnblogs.com/breka/p/11081111.html