Redis data types and their characteristics

Source: Internet
Author: User

Redis supports five types of data: string (String), hash (hash), list, set (set), and Zset (sorted set: Ordered set).

String (String)

The string is the most basic type of redis, and you can understand it as a type that is identical to memcached, a key that corresponds to a value.

The string type is binary safe. This means that a Redis string can contain any data. For example, JPG images or serialized objects.

The string type is the most basic data type of Redis, and a key can store up to 512MB.

Instance
127.0.  0.1:6379>"w3cschool.cc"127.0.  0.1:6379> GET name"w3cschool.cc"         

In the above example we used Redis's SET and GET commands. The key is name and the corresponding value is w3cschool.cc.

Note: a key can store up to 512MB.

Hash (hashed)

A Redis hash is a collection of key-value pairs.

Redis Hash is a string-type field and value mapping table, and hash is particularly useful for storing objects.

Instance
Redis127.0.0.1:6379>Hmset User:1Username W3cschool.CC Password W3cschool.CC points200Okredis127.0.0.1:6379>Hgetall User:11   "username" 2    "w3cschool.cc" 3< Span class= "pun" >)   "password" 4    "w3cschool.cc" 5< Span class= "pun" >)   "points" 6    "$" redis 127.0< Span class= "pun". 0.1:6379>     

The hash data type in the above example stores the user object that contains the user script information. In the example we used Redis hmset, hgetall command,user:1 as the key value.

Each hash can store 232-1 key-value pairs (more than 4 billion).

List (lists)

The Redis list is a simple list of strings, sorted by insertion order. You can add an element guide to the head (left) or tail (right) of the list.

Instance
Redis127.0.0.1:6379>Lpush W3cschool.CC Redis(Integer) 1Redis127.0.0.1:6379>Lpush W3cschool.CC MongoDB(Integer) 2Redis127.0.0.1:6379>Lpush W3cschool.CC RABITMQ(Integer) 3redis 127.0. 0.1:6379> Lrange W3cschool cc 0 101)   "RABITMQ" 2)   "MongoDB" 3)   "Redis" redis 127.0. 0.1:6379>        

The list can store up to 232-1 elements (4294967295, each of which can store more than 4 billion).

Set (SET)

Redis's set is an unordered collection of type string.

The collection is implemented by a hash table, so the complexity of adding, deleting, and finding is O (1).

Sadd command

Adds a string element to the set set of the key corresponding to the successful return 1, if the element and the returned 0,key in the collection, the corresponding set does not have a return error.

Sadd Key Member
Instance
Redis127.0.0.1:6379>Sadd W3cschool.CC Redis(Integer) 1Redis127.0.0.1:6379>Sadd W3cschool.CC MongoDB(Integer) 1Redis127.0.0.1:6379>Sadd W3cschool.CC RABITMQ(Integer) 1Redis127.0.0.1:6379> Sadd w3cschool. (integer 0< Span class= "PLN" >redis 127.0. 0.1:6379> Smembers W3cschool cc1)   "RABITMQ" 2)   "MongoDB" 3)   "Redis"    

Note: RABITMQ has been added two times in the above example, but the second inserted element is ignored based on the uniqueness of the elements within the collection.

The maximum number of members in the collection is 232-1 (4294967295, each of which can store 40多亿个 members).

Zset (sorted set: Ordered set)

Redis Zset and set are also collections of string-type elements and do not allow duplicate members.

The difference is that each element is associated with a double-type fraction. Redis is a small-to-large ordering of the members in a collection by fractions.

Zset members are unique, but fractions (score) can be duplicated.

Zadd command

Adds an element to the collection, the element exists in the collection, and updates the corresponding score

Instance
Redis127.0.0.1:6379>Zadd W3cschool.Cc0Redis(Integer) 1Redis127.0.0.1:6379>Zadd W3cschool.Cc0Mongodb(Integer) 1Redis127.0.0.1:6379>Zadd W3cschool.Cc0Rabitmq(Integer) 1Redis127.0.0.1:6379>Zadd W3cschool.Cc0 Rabitmq (integer 0redis 127.0. 0.1:6379> Zrangebyscore w3cschool< Span class= "pun". cc 0 10001)   "Redis" 2)   "MongoDB" 3)   "RABITMQ"       


Features Summary:

1, String: The most basic type, storing ordinary strings

2, Hash: storage structure for the name =>{key:value,key:value,key:value ...}, it is worth noting that when duplicate key:value occurs, the data that follows will replace the previous data. For example:

Command:127.0.0.1:6379> hmset user:2 name Zhangsan Age weight 60kg name Wangwu

Results:127.0.0.1:6379> Hgetall User:2

1) "Name"
2) "Wangwu"
3) "Age"
4) "20"
5) "Weight"
6) "60kg"

3. List: The data is rendered in the same order as the write order, and no change in order occurs. The stored data is a single string that allows data items to be duplicated.

4. Set: Stored as an unordered list of strings, which is characterized by being ignored when writing duplicate data items.

5, Zset: stored as an ordered list of strings, data items cannot be duplicated.

Summary: In addition to the hash type stored data can be more complex, other types of storage is a string type or string list, the specific characteristics and general programming language related data type characteristics are similar.

Reference: http://www.runoob.com/redis/redis-data-types.html

Redis data types and their characteristics

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.