Introduction to Redis data types

Source: Internet
Author: User

Redis Data types

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
    1. Redis 127.0. 0.1:6379> SET name "Shouce.ren"
    2. Ok
    3. Redis 127.0. 0.1:6379> GET name
    4. "Shouce.ren"

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

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
  1. Redis 127.0. 0.1:6379> hmset user:1 username shouce. ren password shouce. ren points
  2. Ok
  3. Redis 127.0. 0.1:6379> hgetall user:1
  4. 1) "username"
  5. 2) "Shouce.ren"
  6. 3) "password"
  7. 4) "Shouce.ren"
  8. 5) "points"
  9. 6) "$"
  10. Redis 127.0. 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, hegtall 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
  1. Redis 127.0. 0.1:6379> lpush shouce. ren redis
  2. (integer) 1
  3. Redis 127.0. 0.1:6379> lpush shouce. ren mongodb
  4. (integer) 2
  5. Redis 127.0. 0.1:6379> lpush shouce. ren rabitmq
  6. (integer) 3
  7. Redis 127.0. 0.1:6379> lrange shouce. ren 0
  8. 1) "RABITMQ"
  9. 2) "MongoDB"
  10. 3) "Redis"
  11. 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.

    1. Sadd Key Member
Instance
  1. Redis 127.0. 0.1:6379> sadd shouce. ren redis
  2. (integer) 1
  3. Redis 127.0. 0.1:6379> sadd shouce. ren mongodb
  4. (integer) 1
  5. Redis 127.0. 0.1:6379> sadd shouce. ren rabitmq
  6. (integer) 1
  7. Redis 127.0. 0.1:6379> sadd shouce. ren rabitmq
  8. (integer) 0
  9. Redis 127.0. 0.1:6379> smembers shouce. ren
  10. 1) "RABITMQ"
  11. 2) "MongoDB"
  12. 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

    1. Zadd Key Score Member
Instance
  1. Redis 127.0. 0.1:6379> zadd shouce. ren 0 redis
  2. (integer) 1
  3. Redis 127.0. 0.1:6379> zadd shouce. ren 0 mongodb
  4. (integer) 1
  5. Redis 127.0. 0.1:6379> zadd shouce. ren 0 rabitmq
  6. (integer) 1
  7. Redis 127.0. 0.1:6379> zadd shouce. ren 0 rabitmq
  8. (integer) 0
  9. Redis 127.0. 0.1:6379> zrangebyscore shouce. ren 0
  10. 1) "Redis"
  11. 2) "MongoDB"
  12. 3) "RABITMQ"

Introduction to Redis data types

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.