7.1 Redis 简介

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以结合数据库、消息中间件来使用。它支持多种类型的数据结构,如字符串(strings),散列(hashes),列表(lists),集合(sets),有序集合(sorted sets)与范围查询,bitmaps,hyperloglogs 和地理空间(geospatial)索引半径查询。 Redis 内置了复制(replication),LUA脚本(Lua scripting),LRU驱动事件(LRU eviction),事务(transactions)和不同级别的磁盘持久化(persistence),并通过 Redis 哨兵(Sentinel)和自动分区(Cluster)提供高可用性(high availability)。正是因为上述特性,在企业环境中,Redis 被大量使用。

Redis 是以源码方式发布的,在 Linux、Mac 下都需要本地编译后使用。官方并不支持 Windows 版本,而且在生产环境下也少有见到在 Windows 下使用 Redis 的。

在学习过程中,为了简化环境起见,我们选择在 Windows 下安装 Redis 预编译版本。

根据 Redis 官方文档所述,其支持8种数据类型,本小节我们介绍经常使用到的前5种数据类型。

  • Binary-safe strings.
  • Lists: collections of string elements sorted according to the order of insertion. They are basically linked lists .
  • Sets: collections of unique, unsorted string elements.
  • 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).
  • 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.
  • 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.
  • 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.
  • 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.

7.1.1 安装

https://github.com/microsoftarchive/redis/releases 这里下载 Windows 预编译版本的 Redis,为了简便起见,我们选择解压包文件。

下载后,将其解压到用户目录中,例如C:\Users\Kevin\Redis-x64-3.2.100

双击redis-server.exe运行 Redis 服务器,稍后可以在控制台看到 Redis 服务在 6379 端口上已经开放了。

7.1.2 测试

双击redis-cli.exe启动 Redis 客户端,确定其已经连接到本地 Redis 服务。

7.1.2.1 字符串的增删改查

输入命令set name 'Kevin'在 Redis 中增加一个 Key-Vaule 记录,并使用get name查询对应的值,如下:

输入命令set name 'Roy',将 name 更新为 Roy,然后再查询其值,如下:

输入命令del name,删除 name 的值,然后使用查询get name,返回 nil。

我们用exists name命令检查 name 这个 key 是否在 Redis 中存在,返回 0 表示不存在,也就是说上面的删除命令是正确执行了的。

7.1.2.2 List 集合的增删改查

Redis 列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。

一个列表最多可以包含 \( 2^{32} - 1 \) 个元素(40多亿)。

输入命令lpush userList 'Kevin' 'Roy',Redis 返回2,说明其存入了两个值。

输入命令lrange userList 0 -1,可以查询到 userList 这个 List 中的所有元素。

输入命令rpush userList 'GoodMan',往 List 的尾部增加一个元素。

输入命令lpush userList 'Wonderful',往 List 的头部增加一个元素。

然后查询所有的 List 元素,确认 Wonderful 增加在头部,GoodMan 追加在尾部。

输入命令lset userList 1 'Roy Zhang',将 List 中的第 2 个元素(Roy)更新为“Roy Zhang”,查询 List 确认是否正确修改。

输入命令lrem userList 0 'Wonderful',删除 List 中的第1个元素,并查询确认。

7.1.2.3 Set 集合的增删改查

Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。集合中最大的成员数为 \( 2^{32} - 1 \)(40多亿)。

输入命令sadd userSet 'Kevin' 'Roy' 'GoodMan',往 userSet 中添加 3 个元素。然后执行命令smembers userSet命令,查询 userSet 这个Set集合中的元素。

输入命令srem userSet 'GoodMan',删除 userSet 中的“GoodMan”元素。

输入命令sadd userSet 'Wonderful',往 userSet 这个 Set 集合中添加“Wonderful”元素。查询后发现新加入的元素添加到了第 2 位,当然,只是显示问题,Set 中的元素本来就没有顺序。

输入命令sadd userSet 'Kevin',Redis 返回 0,是因为 userSet 中本来就有“Kevin”这个元素,所有无法再次添加。

7.1.2.4 Hash 集合的增删改查

Redis Hash 是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象。Redis 中每个 hash 可以存储 \( 2^{32} - 1 \) 键值对(40多亿)。

输入命令hset userHset 'user1' 'Kevin'创建 Key 为 userHset 的 hash 集合,往其中添加一条记录:字段为 user1,值为 Kevin,Redis 返回添加成功的记录数为 1。

输入命令hset userHset 'user2' 'Roy',往 userHset 中添加第2条记录。

使用命令hlen userHset查询 Hash 集合的记录数,此处应该返回 2 条。

输入命令hkeys userHset查询 Hash 集合中所有的字段(Key)。

image-20191128093027446

输入命令hvals userHset查询 Hash 集合中所有的值(Value)。

image-20191128093218806

输入命令hgetall userHset查询 userHset 这个哈希集合中的所有字段和值(Key Value)。

输入命令hget userHset 'user1'查询 userHset 这个哈希集合中 Key 为 user1 所对应的值(Value)。

输入命令hset userHset 'user1' 'Kevin Zhang'更新 userHset 这个哈希集合中 Key 为 user1 所对应的值(Value),注意 Redis 返回 0,说明删除操作并不会往 Redis 中增加记录。

然后使用命令hget userHset 'user1'查询 user1,检查值是否被更改。

输入命令hdel userHset user1在 userHset 这个哈希集合中删除 user1 和对应的值(当前为Kevin Zhang),Redis 返回1,说明删除了一个 Key-Value 值对。

然后使用命令hgetall userHset检查是否删除成功。

7.1.2.5 Sorted Set 集合的增删改查

Redis 有序集合和集合一样也是 string 类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个 double 类型的分数。Redis 正是通过分数来为集合中的成员进行从小到大的排序。

有序集合的成员是唯一的,但分数(score)却可以重复。集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。 集合中最大的成员数为 \( 2^{32} - 1 \)(40多亿)。

输入命令zadd userZset 1 'Kevin',为 userZset 添加成员 Kevin,分数为 1。输入命令zadd userZset 2 'Roy',为 userZset 添加成员 Roy,分数为 2。

输入命令zrange userZset 0 -1按照分数从小到大查询集合中的元素。

输入命令zrevrange userZset 0 -1按照分数从大到小查询集合中的元素。

输入命令zscore userZset 'Kevin'查询 Kevin 这个元素的分数。

输入命令zrem userZset 'Kevin'删除集合中的 Kevin 这个元素。

下一节:Spring Boot 为集成 Redis 提供了方便的启动器(Starter),在 Spring Boot 的自动配置中我们可以看到是通过 RedisCacheConfiguration 来进行配置的。