【文档熟肉】redis数据类型

Redis data types

redis数据类型

Overview of the many data types supported by Redis
概述redis支持的几种数据类型。

Strings

字符串

Strings are the most basic kind of Redis value. Redis Strings are binary safe, this means that a Redis string can contain any kind of data, for instance a JPEG image or a serialized Ruby object.
字符串是最基本的 Redis 值类型。 Redis 字符串是二进制安全的,这意味着 Redis 字符串可以包含任何类型的数据,例如 JPEG 图像或序列化的 Ruby 对象。

A String value can be at max 512 Megabytes in length.
字符串值的最大长度为 512 兆字节。

You can do a number of interesting things using strings in Redis, for instance you can:
你可以在 Redis 中使用字符串做一些有趣的事情,例如你可以:

Use Strings as atomic counters using commands in the INCR family: INCR, DECR, INCRBY.
使用 INCR 系列中的命令将字符串用作原子计数器:INCR、DECR、INCRBY。

Append to strings with the APPEND command.
使用 APPEND 命令附加到字符串。

Use Strings as a random access vectors with GETRANGE and SETRANGE.
使用字符串作为带有 GETRANGE 和 SETRANGE 的随机访问向量。

Encode a lot of data in little space, or create a Redis backed Bloom Filter using GETBIT and SETBIT.
使用字符串作为带有 GETRANGE 和 SETRANGE 的随机访问向量。

Check all the available string commands for more information, or read the introduction to Redis data types.
查看所有可用的字符串命令以获取更多信息,或阅读 Redis 数据类型介绍。

Lists

Redis Lists are simply lists of strings, sorted by insertion order. It is possible to add elements to a Redis List pushing new elements on the head (on the left) or on the tail (on the right) of the list.
Redis 列表是简单的字符串列表,按插入顺序排序。 可以将元素添加到 Redis 列表中,将新元素推送到列表的头部(左侧)或尾部(右侧)。

The LPUSH command inserts a new element on the head, while RPUSH inserts a new element on the tail. A new list is created when one of this operations is performed against an empty key. Similarly the key is removed from the key space if a list operation will empty the list. These are very handy semantics since all the list commands will behave exactly like they were called with an empty list if called with a non-existing key as argument.
LPUSH 命令在头部插入一个新元素,而 RPUSH 在尾部插入一个新元素。 当对空键执行此操作之一时,将创建一个新列表。 同样,如果列表操作将清空列表,则从键空间中删除键。 这些是非常方便的语义,因为如果使用不存在的键作为参数调用所有列表命令,它们的行为将与使用空列表调用时完全一样。

Some example of list operations and resulting lists:
列表操作和结果列表的一些示例:

LPUSH mylist a   # now the list is "a"
LPUSH mylist b   # now the list is "b","a"
RPUSH mylist c   # now the list is "b","a","c" (RPUSH was used this time)

The max length of a list is 2^32 - 1 elements (4294967295, more than 4 billion of elements per list).
列表的最大长度为 2^32 - 1 个元素(4294967295,每个列表超过 40 亿个元素)。

The main features of Redis Lists from the point of view of time complexity are the support for constant time insertion and deletion of elements near the head and tail, even with many millions of inserted items. Accessing elements is very fast near the extremes of the list but is slow if you try accessing the middle of a very big list, as it is an O(N) operation.
从时间复杂度的角度来看,Redis Lists 的主要特点是支持恒定时间插入和删除靠近头部和尾部的元素,即使插入了数百万个元素。 访问列表的极端附近的元素非常快,但如果您尝试访问一个非常大的列表的中间,则速度很慢,因为这是一个 O(N) 操作。

You can do many interesting things with Redis Lists, for instance you can:
你可以用 Redis 列表做很多有趣的事情,例如你可以:

Model a timeline in a social network, using LPUSH in order to add new elements in the user time line, and using LRANGE in order to retrieve a few of recently inserted items.
在社交网络中为时间线建模,使用 LPUSH 以在用户时间线中添加新元素,并使用 LRANGE 以检索一些最近插入的项目。

You can use LPUSH together with LTRIM to create a list that never exceeds a given number of elements, but just remembers the latest N elements.
您可以将 LPUSH 与 LTRIM 一起使用来创建一个永远不会超过给定元素数量的列表,而只记住最新的 N 个元素。

Lists can be used as a message passing primitive, See for instance the well known Resque Ruby library for creating background jobs.
列表可以用作消息传递原语,例如,参见著名的 Resque Ruby 库,用于创建后台作业。

You can do a lot more with lists, this data type supports a number of commands, including blocking commands like BLPOP.
你可以用列表做更多的事情,这种数据类型支持许多命令,包括像 BLPOP 这样的阻塞命令。

Please check all the available commands operating on lists for more information, or read the introduction to Redis data types.
请查看所有在列表上操作的可用命令以获取更多信息,或阅读 Redis 数据类型介绍。

Sets

Redis Sets are an unordered collection of Strings. It is possible to add, remove, and test for existence of members in O(1) (constant time regardless of the number of elements contained inside the Set).
Redis Set 是字符串的无序集合。 可以在 O(1) 中添加、删除和测试成员的存在(无论集合中包含的元素数量如何,都是恒定的时间)。

Redis Sets have the desirable property of not allowing repeated members. Adding the same element multiple times will result in a set having a single copy of this element. Practically speaking this means that adding a member does not require a check if exists then add operation.
Redis Set 具有不允许重复成员的理想属性。 多次添加相同的元素将导致集合具有该元素的单个副本。 实际上,这意味着添加成员不需要检查是否存在然后添加操作。

A very interesting thing about Redis Sets is that they support a number of server side commands to compute sets starting from existing sets, so you can do unions, intersections, differences of sets in very short time.
Redis Sets 的一个非常有趣的地方是,它们支持许多服务器端命令来从现有集合开始计算集合,因此您可以在很短的时间内进行集合的并集、交集、差集。

The max number of members in a set is 2^32 - 1 (4294967295, more than 4 billion of members per set).
集合中的最大成员数为 2^32 - 1(4294967295,每组超过 40 亿个成员)。

You can do many interesting things using Redis Sets, for instance you can:
你可以使用 Redis Sets 做很多有趣的事情,例如你可以:

You can track unique things using Redis Sets. Want to know all the unique IP addresses visiting a given blog post? Simply use SADD every time you process a page view. You are sure repeated IPs will not be inserted.
您可以使用 Redis Sets 跟踪独特的事物。 想知道访问给定博客文章的所有唯一 IP 地址吗? 每次处理页面视图时只需使用 SADD。 您确定不会插入重复的 IP。

Redis Sets are good to represent relations. You can create a tagging system with Redis using a Set to represent every tag. Then you can add all the IDs of all the objects having a given tag into a Set representing this particular tag, using the SADD command. Do you want all the IDs of all the Objects having three different tags at the same time? Just use SINTER.
You can use Sets to extract elements at random using the SPOP or SRANDMEMBER commands.
Redis Set 可以很好地表示关系。 您可以使用 Redis 创建一个标记系统,使用 Set 来表示每个标记。 然后,您可以使用 SADD 命令将具有给定标签的所有对象的所有 ID 添加到表示该特定标签的集合中。 您是否希望所有对象的所有 ID 同时具有三个不同的标签? 只需使用 SINTER。
您可以使用 Sets 使用 SPOP 或 SRANDMEMBER 命令随机提取元素。

As usual, check the full list of Set commands for more information, or read the introduction to Redis data types.
像往常一样,请查看 Set 命令的完整列表以获取更多信息,或阅读 Redis 数据类型的介绍。

Hashes

Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (e.g. A User with a number of fields like name, surname, age, and so forth):
Redis 哈希是字符串字段和字符串值之间的映射,因此它们是表示对象的完美数据类型(例如,具有姓名、姓氏、年龄等多个字段的用户):

HMSET user:1000 username antirez password P1pp0 age 34
HGETALL user:1000
HSET user:1000 password 12345
HGETALL user:1000

A hash with a few fields (where few means up to one hundred or so) is stored in a way that takes very little space, so you can store millions of objects in a small Redis instance.
具有几个字段(其中很少意味着最多一百个左右)的哈希以占用非常少的空间的方式存储,因此您可以在一个小型 Redis 实例中存储数百万个对象。

While Hashes are used mainly to represent objects, they are capable of storing many elements, so you can use Hashes for many other tasks as well.
虽然哈希主要用于表示对象,但它们能够存储许多元素,因此您也可以将哈希用于许多其他任务。

Every hash can store up to 2^32 - 1 field-value pairs (more than 4 billion).
每个散列最多可以存储 2^32 - 1 个字段值对(超过 40 亿个)。

Check the full list of Hash commands for more information, or read the introduction to Redis data types.
查看完整的 Hash 命令列表以获取更多信息,或阅读 Redis 数据类型介绍。

Sorted Sets

Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with a score, that is used to keep the Sorted Set in order, from the smallest to the greatest score. While members are unique, scores may be repeated.
Redis Sorted Sets 与 Redis Sets 类似,是字符串的非重复集合。 不同之处在于,Sorted Set 的每个成员都与一个分数相关联,该分数用于保持 Sorted Set 从最小到最大分数的顺序。 虽然成员是唯一的,但分数可能会重复。

With Sorted Sets you can add, remove, or update elements in a very fast way (in a time proportional to the logarithm of the number of elements). Since elements are stored in order and not ordered afterwards, you can also get ranges by score or by rank (position) in a very fast way. Accessing the middle of a Sorted Set is also very fast, so you can use Sorted Sets as a smart list of non repeating elements where you can quickly access everything you need: elements in order, fast existence test, fast access to elements in the middle!
使用排序集,您可以以非常快速的方式添加、删除或更新元素(时间与元素数量的对数成正比)。 由于元素是按顺序存储的,而不是事后排序的,因此您还可以通过分数或排名(位置)以非常快速的方式获取范围。 访问 Sorted Set 的中间也非常快,因此您可以将 Sorted Sets 用作非重复元素的智能列表,您可以在其中快速访问所需的一切:元素按顺序、快速存在测试、快速访问中间元素 !

In short with Sorted Sets you can do a lot of tasks with great performance that are really hard to model in other kind of databases.
简而言之,使用 Sorted Sets,您可以执行许多性能出色的任务,而这些任务在其他类型的数据库中很难建模。

With Sorted Sets you can:
使用排序集,您可以:

Build a leaderboard in a massive online game, where every time a new score is submitted you update it using ZADD. You can easily retrieve the top users using ZRANGE, you can also, given a user name, return its rank in the listing using ZRANK. Using ZRANK and ZRANGE together you can show users with a score similar to a given user. All very quickly.
在大型在线游戏中建立排行榜,每次提交新分数时,您都使用 ZADD 更新它。 您可以使用 ZRANGE 轻松检索排名靠前的用户,还可以给定用户名,使用 ZRANK 返回其在列表中的排名。 将 ZRANK 和 ZRANGE 一起使用,您可以向用户显示与给定用户相似的分数。 一切都很快。

Sorted Sets are often used in order to index data that is stored inside Redis. For instance if you have many hashes representing users, you can use a Sorted Set with members having the age of the user as the score and the ID of the user as the value. So using ZRANGEBYSCORE it will be trivial and fast to retrieve all the users with a given age range.
排序集通常用于索引存储在 Redis 中的数据。 例如,如果您有许多代表用户的散列,您可以使用一个排序集,其中成员以用户的年龄作为分数,用户的 ID 作为值。 因此,使用 ZRANGEBYSCORE 检索具有给定年龄范围的所有用户将是简单而快速的。

Sorted Sets are one of the more advanced Redis data types, so take some time to check the full list of Sorted Set commands to discover what you can do with Redis! Also you may want to read the Introduction to Redis Data Types.
排序集是更高级的 Redis 数据类型之一,因此请花一些时间查看排序集命令的完整列表,以了解您可以使用 Redis 做什么! 此外,您可能还想阅读 Redis 数据类型简介。

Bitmaps and HyperLogLogs

Redis also supports Bitmaps and HyperLogLogs which are actually data types based on the String base type, but having their own semantics.
Redis 还支持 Bitmaps 和 HyperLogLogs,它们实际上是基于 String 基本类型的数据类型,但有自己的语义。

Please refer to the data types tutorial for information about those types.
有关这些类型的信息,请参阅数据类型教程。

Streams

A Redis stream is a data structure that acts like an append-only log. Streams are useful for recording events in the order they occur. See the Redis streams docs for details and usage.
Redis 流是一种数据结构,其作用类似于仅附加日志。 流对于按事件发生的顺序记录事件很有用。 有关详细信息和用法,请参阅 Redis 流文档。

Geospatial indexes

Redis provides geospatial indexes, which are useful for finding locations within a given geographic radius. You can add locations to a geospatial index using the GEOADD command. You then search for locations within a given radius using the GEORADIUS command.
Redis 提供地理空间索引,这对于查找给定地理半径内的位置很有用。 您可以使用 GEOADD 命令将位置添加到地理空间索引。 然后使用 GEORADIUS 命令搜索给定半径内的位置。

See the complete geospatial index command reference for all of the details.
有关所有详细信息,请参阅完整的地理空间索引命令参考。

Data types tutorial
Learning the basic Redis data types and how to use them
学习基本的 Redis 数据类型以及如何使用它们

Redis streams
An introduction to the Redis stream data type
Redis流数据类型介绍

参考文献:
[1],redis官方文档Redis data types

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值