Redis Data Type

Source: Internet
Author: User
Redis Basic Data Type

Redis currently supports five data types:

String (string)

List)

Hash (dictionary)

Set)

Sorted set)

The following describes the five data types and their corresponding operation commands.

 

String (string)

String is a simple key-value pair. value can be a string or a number. String is a string by default stored in redis and is referenced by redisobject. When an incr, decr, or other operation is performed, it is converted to a numeric type for calculation. In this case, the encoding field of redisobject is int.

String is a string by default stored in redis and is referenced by redisobject. When an incr, decr, or other operation is performed, it is converted to a numeric type for calculation. In this case, the encoding field of redisobject is int.

Application scenarios:

String is the most common data type. Common Key/value storage can be classified as this type, so we will not explain it here.

Generally, many strings are used. Generally, JSON and multiple redis storage objects are JSON. JSON is obtained through redis and then deserialized.

 

List)

 

The redis list is a simple string list, which can be analogous to STD: List in C ++. Simply put, it is a linked list or a queue. You can add elements to the redis list from the header or tail. The maximum length of a list is 2 ^ 32-1, that is, each list supports more than 4 billion elements.

 

The implementation of redis list is a two-way linked list, that is, it supports reverse lookup and traversal to facilitate operations. However, it brings some additional memory overhead and many internal implementations of redis, this data structure is also used, including the sending Buffer Queue.

 

Application scenarios

Redis list has many application scenarios and is also one of redis's most important data structures. For example, the Twitter follow list and fan list can all be implemented using the redis list structure, for example, some applications use the redis list type to implement a simple lightweight message queue, producer push, consumer POP/BPOP.

Store three values to the list.

Query:

From 0 to 10

 

Hash (dictionary)

The value corresponding to redis hash is actually a hashmap. There are two different implementations here. When there are few Members of this hash, redis uses a one-dimensional array to compact storage to save memory, instead of using the real hashmap structure, the corresponding value redisobject's encoding is zipmap. When the number of members increases, it is automatically converted to a real hashmap, And the encoding is ht.

 

 

Set)

 

Redis's set is a unordered set of the string type. The Set members are unique, which means that duplicate data cannot appear in the set.

Redis sets are implemented through hash tables. Therefore, the complexity of adding, deleting, and searching is O (1 ).

The maximum number of members in a set is 232-1 (4294967295, each set can store more than 4 billion members ).

 

 

 

 

Sorted set)

 

The redis ordered set is the same as the set of string elements, and repeated members are not allowed.

The difference is that each element is associated with a score of the double type. Redis uses scores to sort members in a set from small to large.

The members of an ordered set are unique, but scores can be repeated.

A set is implemented through a hash table. Therefore, the complexity of adding, deleting, and searching is O (1 ). The maximum number of members in a set is 232-1 (4294967295, each set can store more than 4 billion members ).

 

 

 

Use the visualization tool redis client:

The default value is db0.

 

Redis Data Type

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.