Redis five kinds of object types and their underlying implementation principle _redis

Source: Internet
Author: User
Tags set set
First, Redis data type

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

String (strings)
String is the most basic type of redis, and you can understand it as a type that is identical to memcached, and a key corresponds to a value.

The string type is binary safe. A string that means Redis can contain any data. such as JPG images or serialized objects.

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

Instance
redis 127.0.0.1:6379> SET name "Runoob"
OK
redis 127.0.0.1:6379> get name
"Runoob"

In the above example we used the Redis SET and get commands. The key is name and the corresponding value is Runoob.

Note: A key can store 512MB maximum.

Hash (hash)
Redis Hash is a set of key value (key=>value) pairs.

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

Instance
redis> hset myhash field1 "Hello" field2 "World"
"OK"
redis> hget-myhash
"Hello"
redis> hget myhash field2
"World"

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

Each hash can store a 232-1-key value pair (more than 4 billion).
List (listing)
The Redis list is a simple list of strings sorted by the insertion order. You can add an element to the head of the list (left) or to the tail (right).

Instance
redis 127.0.0.1:6379> lpush runoob redis
(integer) 1
redis 127.0.0.1:6379> lpush Runoob MongoDB
(integer) 2
redis 127.0.0.1:6379> lpush runoob rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange Runoob 0
1) "RABITMQ"
2) "MongoDB"
3) "Redis"
redis 127.0.0.1:6379>
list can store up to 232-1 elements (42949 67295, each list can store more than 4 billion).

Set (SET)
The set of Redis is a unordered collection of string types.

The collection is implemented through 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 key, returns 1 successfully if the element has returned 0 in the collection, and returns an error if the key's corresponding set does not exist.

Sadd Key member
instance
redis 127.0.0.1:6379> sadd runoob redis
(integer) 1
redis 127.0.0.1:6379> Runoob mongodb
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers runoob

1) "Redis"
2) "RABITMQ"
3 "MongoDB"

Note: RABITMQ has been added two times in the above instance, but the second inserted element will be ignored, depending 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 of score. Redis is the sort of small to large members of a collection through fractions.

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

Zadd command
add element to collection, element exists in collection update corresponding score

Zadd Key score member 
Instance
redis 127.0.0.1:6379> zadd runoob 0 redis
(integer) 1
redis 127.0.0.1:6379& Gt Zadd Runoob 0 MongoDB
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 1
redis 127.0. 0.1:6379> zadd Runoob 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> > Zrangebyscore runoob 0 1000
1) " MongoDB "
2" RABITMQ "3" "
Redis"
Second, the bottom of the implementation

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.