Redis Data Type

Source: Internet
Author: User
Tags set set
Redis Data Type

Redis supports five data types: String, hash, list, set, and zset ).

String (string)

String is the most basic type of redis. You can think of it as the same type as memcached. A key corresponds to a value.

The string type is binary secure. It means that the 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 the string type value can be stored at most 512 MB.

Instance
redis 127.0.0.1:6379> SET name "runoob"OKredis 127.0.0.1:6379> GET name"runoob"

In the above example, we useSetAndGetCommand. The key is name and the corresponding value isRunoob.

Note:A single key can store up to 512 MB.

Hash)

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

Redis hash is a string-type field-value ing table. Hash is especially suitable for storing objects.

Instance
redis> HMSET myhash field1 "Hello" field2 "World""OK"redis> HGET myhash field1"Hello"redis> HGET myhash field2"World"

Redis is used in the instance.Hmset, hgetCommand,HmsetTwo field => value pairs are set, and hget gets the correspondingFieldCorrespondingValue.

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

 

List)

The redis list is a simple string list sorted by insertion order. You can add an element to the header (left) or tail (right) of the list ).

Instance
redis 127.0.0.1:6379> lpush runoob redis(integer) 1redis 127.0.0.1:6379> lpush runoob mongodb(integer) 2redis 127.0.0.1:6379> lpush runoob rabitmq(integer) 3redis 127.0.0.1:6379> lrange runoob 0 101) "rabitmq"2) "mongodb"3) "redis"redis 127.0.0.1:6379>

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

Set)

Redis's set is a unordered set of the string type.

A set is implemented through a hash table. Therefore, the complexity of adding, deleting, and searching is O (1 ).

Sadd command

When a string element is added to the Set set corresponding to the key, 1 is returned successfully. If the element is already in the Set, 0 is returned. If the set corresponding to the key does not exist, an error is returned.

sadd key member
Instance
redis 127.0.0.1:6379> sadd runoob redis(integer) 1redis 127.0.0.1:6379> sadd runoob mongodb(integer) 1redis 127.0.0.1:6379> sadd runoob rabitmq(integer) 1redis 127.0.0.1:6379> sadd runoob rabitmq(integer) 0redis 127.0.0.1:6379> smembers runoob1) "redis"2) "rabitmq"3) "mongodb"

Note:In the preceding example, rabitmq is added twice, but the elements inserted for the second time are ignored Based on the uniqueness of the elements in the set.

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

Zset (sorted set: ordered set)

Redis zset and set are also 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.

Zset members are unique, but scores can be repeated.

Zadd command

Add an element to the set. If an element exists in the Set, the corresponding score is updated.

zadd key score member 
Instance
redis 127.0.0.1:6379> zadd runoob 0 redis(integer) 1redis 127.0.0.1:6379> zadd runoob 0 mongodb(integer) 1redis 127.0.0.1:6379> zadd runoob 0 rabitmq(integer) 1redis 127.0.0.1:6379> zadd runoob 0 rabitmq(integer) 0redis 127.0.0.1:6379> > ZRANGEBYSCORE runoob 0 10001) "mongodb"2) "rabitmq"3) "redis"

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.