Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis Data Types

Redis Data Types

Redis mainly supports the following data types:


String

string is the most basic type in redis, similar to the type in Memcached: one key corresponds to one value.

The string type is binary-safe. This means a redis string can contain any data, such as a jpg image or a serialized object.

The string type is the most basic data type in Redis; a string value can store up to 512MB.

Common Commands

Example



redis 127.0.0.1:6379> SET runoob "菜鸟教程"

OK

redis 127.0.0.1:6379> GET runoob

"菜鸟教程"

In the example above we used Redis's SET and GET command. The key is runoob and the corresponding value is Runoob Tutorial.

**Note:** A key can store a maximum of 512MB.


Hash

A Redis hash is a collection of key-value pairs, similar to a small NoSQL database.

A Redis hash is a mapping table of string-type fields and values; hash is especially suitable for storing objects.

Each hash can store up to 2^32 - 1 key-value pairs.

Example

DEL runoob is used to delete the key used in the previous test; otherwise an error will be reported:(error) WRONGTYPE Operation against a key holding the wrong kind of value



redis 127.0.0.1:6379> DEL runoob

redis 127.0.0.1:6379> HMSET runoob field1 "Hello" field2 "World"

"OK"

redis 127.0.0.1:6379> HGET runoob field1

"Hello"

redis 127.0.0.1:6379> HGET runoob field2

"World"

In the example we used Redis HMSET, HGET command,HMSET set two field=>value pairs, and HGET gets the corresponding field corresponding value.


List

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

A list can store up to 2^32 - 1 elements.

Common Commands

Example



redis 127.0.0.1:6379> DEL runoob

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 rabbitmq

(integer) 3

redis 127.0.0.1:6379> lrange runoob 0 10

1) "rabbitmq"

2) "mongodb"

3) "redis"

redis 127.0.0.1:6379>


Set

A Redis Set is an unordered collection of string type.

A set is implemented via a hash table, so the complexity of add, delete, and lookup operations is O(1).

Common Commands

sadd command

Adds a string element to the set at the given key; returns 1 on success, or 0 if the element already exists in the set.



sadd key member

Example



redis 127.0.0.1:6379> DEL runoob

redis 127.0.0.1:6379> sadd runoob redis

(integer) 1

redis 127.0.0.1:6379> sadd runoob mongodb

(integer) 1

redis 127.0.0.1:6379> sadd runoob rabbitmq

(integer) 1

redis 127.0.0.1:6379> sadd runoob rabbitmq

(integer) 0

redis 127.0.0.1:6379> smembers runoob



1) "redis"

2) "rabbitmq"

3) "mongodb"

**Note:** In the example above, rabbitmq was added twice, but because of the uniqueness of set elements, the second insertion is ignored.

The maximum number of members in a set is 2^32 - 1 (4294967295, each set can store over 4 billion members).


zset (sorted set)

A Redis zset, like a set, is a collection of string-type elements and does not allow duplicate members.

The difference is that each element is associated with a double-type score. Redis uses the score to sort the members of the set from smallest to largest.

zset members are unique, but scores can be duplicate.

Common Commands

zadd command

Add an element to the set; if the element exists in the set, update its corresponding score



zadd key score member 

Example



redis 127.0.0.1:6379> DEL runoob

redis 127.0.0.1:6379> zadd runoob 0 redis

(integer) 1

redis 127.0.0.1:6379> zadd runoob 0 mongodb

(integer) 1

redis 127.0.0.1:6379> zadd runoob 0 rabbitmq

(integer) 1

redis 127.0.0.1:6379> zadd runoob 0 rabbitmq

(integer) 0

redis 127.0.0.1:6379> ZRANGEBYSCORE runoob 0 1000

1) "mongodb"

2) "rabbitmq"

3) "redis"


Other advanced data types

HyperLogLog

Bitmaps

Geospatial Indexes