Redis 简明教程

Redis - Data Types

Redis 支持 5 种数据类型。

Redis supports 5 types of data types.

Strings

Redis 字符串是字节序列。Redis 中的字符串是二进制安全的,这意味着它们具有已知的长度,不受任何特殊终止符的确定。因此,你可以在一个字符串中存储多达 512 兆字节的内容。

Redis string is a sequence of bytes. Strings in Redis are binary safe, meaning they have a known length not determined by any special terminating characters. Thus, you can store anything up to 512 megabytes in one string.

Example

redis 127.0.0.1:6379> SET name "tutorialspoint"
OK
redis 127.0.0.1:6379> GET name
"tutorialspoint"

在上述示例中, SETGET 是 Redis 命令, name 是在 Redis 中使用的键, tutorialspoint 是存储在 Redis 中的字符串值。

In the above example, SET and GET are Redis commands, name is the key used in Redis and tutorialspoint is the string value that is stored in Redis.

Note − 字符串值的最大长度为 512 兆字节。

Note − A string value can be at max 512 megabytes in length.

Hashes

Redis 哈希是键值对的集合。Redis 哈希是字符串字段和字符串值之间的映射。因此,它们用于表示对象。

A Redis hash is a collection of key value pairs. Redis Hashes are maps between string fields and string values. Hence, they are used to represent objects.

Example

redis 127.0.0.1:6379> HMSET user:1 username tutorialspoint password
tutorialspoint points 200
OK
redis 127.0.0.1:6379> HGETALL user:1
1) "username"
2) "tutorialspoint"
3) "password"
4) "tutorialspoint"
5) "points"
6) "200"

在上述示例中,哈希数据类型用于存储包含用户基本信息的用户的对象。此处 HMSET, HGETALL 是 Redis 的命令, user − 1 是该键。

In the above example, hash data type is used to store the user’s object which contains basic information of the user. Here HMSET, HGETALL are commands for Redis, while user − 1 is the key.

每个哈希最多可以存储 232 - 1 个字段值对(超过 40 亿个)。

Every hash can store up to 232 - 1 field-value pairs (more than 4 billion).

Lists

Redis 列表只是字符串列表,按插入顺序排序。你可以在表头或表尾向 Redis 列表中添加元素。

Redis Lists are simply lists of strings, sorted by insertion order. You can add elements to a Redis List on the head or on the tail.

Example

redis 127.0.0.1:6379> lpush tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> lpush tutoriallist mongodb
(integer) 2
redis 127.0.0.1:6379> lpush tutoriallist rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange tutoriallist 0 10

1) "rabitmq"
2) "mongodb"
3) "redis"

列表的最大长度为 232 - 1 个元素(4294967295,每个列表超过 40 亿个元素)。

The max length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list).

Sets

Redis 集合是无序字符串集合。在 Redis 中,你可以在 O(1) 时间复杂度中添加、删除和测试成员是否存在。

Redis Sets are an unordered collection of strings. In Redis, you can add, remove, and test for the existence of members in O(1) time complexity.

Example

redis 127.0.0.1:6379> sadd tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist mongodb
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers tutoriallist

1) "rabitmq"
2) "mongodb"
3) "redis"

Note − 在上述示例中, rabitmq 被添加了两次,但是由于集合的唯一特性,它只会添加一次。

Note − In the above example, rabitmq is added twice, however due to unique property of the set, it is added only once.

集合中的最大成员数为 232 - 1 (4294967295,每个集合超过 40 亿个成员)。

The max number of members in a set is 232 - 1 (4294967295, more than 4 billion of members per set).

Sorted Sets

Redis 有序集合类似于 Redis 集合,即不重复的字符串集合。但不同的是,有序集合的每个成员都与一个分数相关联,该分数用于按分数从小到大对有序集合进行排序。虽然成员是唯一的,但分数可以重复。

Redis Sorted Sets are similar to Redis Sets, non-repeating collections of Strings. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, the scores may be repeated.

Example

redis 127.0.0.1:6379> zadd tutoriallist 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 1000

1) "redis"
2) "mongodb"
3) "rabitmq"