数据类型
八种数据类型
Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogs, Bitmaps.
Redis 内部使用 Redis Object 来实现所有的数据类型。
As you can see in the client structure above, arguments in a command are described as
robj
structures. The following is the fullrobj
structure, which defines a Redis object:Basically this structure can represent all the basic Redis data types like strings, lists, sets, sorted sets and so forth.
1 | typedef struct redisObject { |
Binary-safe strings
介绍
Redis 的 key 是 二进制安全的,这意味着你可以使用任何二进制序列当作 Redis 的 key,即使是一张图片。空字符串也是合法的 key。
key 所允许的最大大小为 512 MB。
INCR 命令将 String 类型解析成 Integer 类型,并自增 1,这个操作是原子的。
命令
1 | > set mykey somevalue |
场景
- 计数。
- 存储数据行字段。
Lists
Lists: collections of string elements sorted according to the order of insertion. They are basically linked lists.
介绍
Redis Lists 是用链表实现的,和数组相比保证了插入的速度,但是访问的速度不是那么快(和被访问元素的索引位置有关)。
命令
lpush 命令将一个元素添加到链表头,rpush命令将一个元素添加到链表尾。lpop,rpop 类似。
1 | > rpush mylist A |
场景
- 记住用户发布到社交网络上的最新更新。
- 生产者消费者,类似消息队列。
Sets
Sets: collections of unique, unsorted string elements.
介绍
无序去重的集合, 还可以对集合进行许多其他操作,例如测试给定元素是否已存在,执行多个集合之间的交集,并集或差等。
使用
1 | > sadd myset 1 2 3 |
场景
- 给文章打标签。
- 发牌。
- 共同关注。
Sorted sets
Sorted sets, similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements (for example you may ask: give me the top 10, or the bottom 10).
Sorted sets 类似于 set 和 hash 之间的混合。像 set 一样,sorted set 由唯一的,非重复的字符串元素组成,虽然集合内的元素没有排序,但是 sorted set 中的每个元素都与一个称为 score 的浮点值相关联(这就是为什么该类型也类似于哈希的原因,因为每个元素都映射到一个值)。
它们根据以下规则排序:
如果 A 和 B 是两个具有不同分数的元素,那么如果 A.score 是 > B.score,则A > B。
如果 A 和 B 的分数完全相同,那么如果 A 字符串在字典上大于 B 字符串,则A > B。
A 和 B 字符串不能相等,因为排序集仅具有唯一元素。
使用
1 | > zadd hackers 1940 "Alan Kay" |
场景
- 排行榜 O(log(N)) 时间复杂度。
Hashes
Hashes, which are maps composed of fields associated with values. Both the field and the value are strings. This is very similar to Ruby or Python hashes.
介绍
查找快速
使用
1 | > hmset user:1000 username antirez birthyear 1977 verified 1 |
场景
- 存储对象。
Bit arrays (or simply bitmaps)
Bit arrays (or simply bitmaps): it is possible, using special commands, to handle String values like an array of bits: you can set and clear individual bits, count all the bits set to 1, find the first set or unset bit, and so forth.
位图不是实际的数据类型,而是在 String 类型上定义的一组面向位的操作。由于字符串是二进制安全Blob,并且最大长度为512 MB,因此可设置多达 2^32 个不同的位。
位操作分为两类:常量时间的单个位操作(例如将一个位设置为1或0或获取其值),以及对位组的操作,例如计算给定位范围内 1 的数量。
位图的最大优点之一是它们在存储信息时可以节省大量空间。
例如,在以增量用户ID表示不同用户的系统中,仅使用512 MB内存就可以记住40亿用户的一位信息(例如,判断用户是否要接收新闻通讯)。
使用
1 | > setbit key 10 1 |
场景
- 存储与对象 ID 相关联的布尔信息。(用户在线状态,用户签到等)
- 各种数据量大的统计。
HyperLogLogs
HyperLogLogs: this is a probabilistic data structure which is used in order to estimate the cardinality of a set. Don’t be scared, it is simpler than it seems… See later in the HyperLogLog section of this tutorial.
介绍
HyperLogLog是一种概率数据结构,用于对唯一事物进行计数,结果有一定的错误率,能节约大量内存。
使用
1 | > pfadd hll a b c d |
场景
- 统计,不需要详细的数据。
Streams
Streams: append-only collections of map-like entries that provide an abstract log data type. They are covered in depth in the Introduction to Redis Streams.
介绍
Redis Streams是一种在内存中的抽象数据类型 , 像日志文件一样,通常实现为仅追加模式打开的文件 。