知识杂货铺 知识杂货铺
首页
后端(1本书)
  • 主题初衷与诞生
  • 介绍
  • 快速上手
  • 目录结构
  • 核心配置和约定
  • 自动生成front matter
  • Markdown 容器
  • Markdown 中使用组件
  • 相关文章

    • 使目录栏支持h2~h6标题
    • 如何让你的笔记更有表现力
    • 批量操作front matter工具
    • 部署
    • 关于写文章和H1标题
    • 关于博客搭建与管理
    • 在线编辑和新增文章的方法
  • 主题配置
  • 首页配置
  • front matter配置
  • 目录页配置
  • 添加摘要
  • 修改主题颜色和样式
  • 评论栏
  • 快速开始
  • 代码集成_TODO
  • 框架初探
  • 在GitHub上贡献代码
  • 使用K8s部署系统
  • Seata分布式事务
GitHub (opens new window)

Kevin Zhang

爱凑热闹的高龄程序猿
首页
后端(1本书)
  • 主题初衷与诞生
  • 介绍
  • 快速上手
  • 目录结构
  • 核心配置和约定
  • 自动生成front matter
  • Markdown 容器
  • Markdown 中使用组件
  • 相关文章

    • 使目录栏支持h2~h6标题
    • 如何让你的笔记更有表现力
    • 批量操作front matter工具
    • 部署
    • 关于写文章和H1标题
    • 关于博客搭建与管理
    • 在线编辑和新增文章的方法
  • 主题配置
  • 首页配置
  • front matter配置
  • 目录页配置
  • 添加摘要
  • 修改主题颜色和样式
  • 评论栏
  • 快速开始
  • 代码集成_TODO
  • 框架初探
  • 在GitHub上贡献代码
  • 使用K8s部署系统
  • Seata分布式事务
GitHub (opens new window)
  • Spring Boot 培训教程
  • Spring Boot介绍

  • 开发环境配置

  • 原理剖析

  • Web开发

  • 数据访问

  • 事务

  • 集成Redis

    • Redis缓存
    • Redis介绍
      • 7.1 Redis 简介
        • 7.1.1 安装
        • 7.1.2 测试
        • 7.1.2.1 字符串的增删改查
        • 7.1.2.2 List 集合的增删改查
        • 7.1.2.3 Set 集合的增删改查
        • 7.1.2.4 Hash 集合的增删改查
        • 7.1.2.5 Sorted Set 集合的增删改查
    • 集成Redis
    • Session共享
    • 部署Redis集群
    • 课后作业
  • 集成MongoDB

  • 异步消息

  • 异常处理

  • 单元测试与热部署

  • 安全控制

  • 应用监控

  • 企业级开发

  • 多环境配置与部署

  • 综合示例

  • 前后端分离的vue急速入门

  • Spring Boot配置大全

  • 在Docker中部署Spring Boot应用

  • 开发前后端分离应用

  • 前进到Spring Cloud

  • 规则引擎

  • 流程引擎

  • 后记
  • 后端
  • 集成Redis
Kevin Zhang
2024-10-30
目录

Redis介绍

# 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种数据类型 (opens new window),本小节我们介绍经常使用到的前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 (opens new window).

# 7.1.1 安装

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

image-20191127202044621

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

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

image-20191127202555635

# 7.1.2 测试

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

image-20191127202842175

# 7.1.2.1 字符串的增删改查

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

image-20191127203200439

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

image-20191127203434648

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

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

image-20191127203555401

# 7.1.2.2 List 集合的增删改查

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

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

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

image-20191127210438989

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

image-20191127210600575

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

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

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

image-20191127211032593

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

image-20191127211327545

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

image-20191127211541725

# 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集合中的元素。

image-20191127212511955

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

image-20191127212656923

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

image-20191127212911806

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

image-20191127213110248

# 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。

image-20191128092337714

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

image-20191128092804308

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

image-20191128092922075

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

image-20191128093027446

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

image-20191128093218806

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

image-20191128093400840

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

image-20191128093543626

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

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

image-20191128093940300

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

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

image-20191128094416055

# 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。

image-20191128100707538

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

image-20191128100841268

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

image-20191128101038358

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

image-20191128101233995

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

image-20191128101437770

编辑 (opens new window)
上次更新: 2024/11/17, 16:29:23
Redis缓存
集成Redis

← Redis缓存 集成Redis→

最近更新
01
PNG图片处理C++
02-07
02
PNG图片处理
01-24
03
离线安装Docker
12-24
更多文章>
Theme by Vdoing | Copyright © 2008-2025 Kevin Zhang | MIT License | 蜀ICP备20013663号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式