一、详细介Redis 简介
"Redis is 绍种数据an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker." —— Redis是一个开放源代码(BSD许可)的内存中数据结构存储,用作数据库,基本结构缓存和消息代理。详细介(摘自官网)
Redis 是绍种数据一个开源,高级的基本结构键值存储和一个适用的解决方案,用于构建高性能,详细介可扩展的绍种数据 Web 应用程序。Redis 也被作者戏称为 数据结构服务器 ,基本结构这意味着使用者可以通过一些命令,详细介基于带有 TCP 套接字的绍种数据简单 服务器-客户端 协议来访问一组 可变数据结构 。(在 Redis 中都采用键值对的基本结构方式,只不过对应的详细介数据结构不一样罢了)
Redis 的优点
以下是 Redis 的一些优点:
异常快- Redis 非常快,每秒可执行大约 110000 次的绍种数据设置(SET)操作,每秒大约可执行 81000 次的基本结构读取/获取(GET)操作。 支持丰富的数据类型 - Redis 支持开发人员常用的大多数数据类型,例如列表,集合,免费源码下载排序集和散列等等。这使得 Redis 很容易被用来解决各种问题,因为我们知道哪些问题可以更好使用地哪些数据类型来处理解决。 操作具有原子性- 所有 Redis 操作都是原子操作,这确保如果两个客户端并发访问,Redis 服务器能接收更新的值。多实用工具- Redis 是一个多实用工具,可用于多种用例,如:缓存,消息队列(Redis 本地支持发布/订阅),应用程序中的任何短期数据,例如,web应用程序中的会话,网页命中计数等。Redis 的安装
这一步比较简单,你可以在网上搜到许多满意的教程,这里就不再赘述。
给一个菜鸟教程的安装教程用作参考:https://www.runoob.com/redis/redis-install.html
测试本地 Redis 性能
当你安装完成之后,你可以先执行 redis-server 让 Redis 启动起来,然后运行命令 redis-benchmark -n 100000 -q 来检测本地同时执行 10 万个请求时的性能:

当然不同电脑之间由于各方面的原因会存在性能差距,这个测试您可以权当是一种 「乐趣」 就好。
二、亿华云Redis 五种基本数据结构
Redis 有 5 种基础数据结构,它们分别是:string(字符串)、list(列表)、hash(字典)、set(集合) 和 zset(有序集合)。这 5 种是 Redis 相关知识中最基础、最重要的部分,下面我们结合源码以及一些实践来给大家分别讲解一下。
1)字符串 string
Redis 中的字符串是一种 动态字符串,这意味着使用者可以修改,它的底层实现有点类似于 Java 中的 ArrayList,有一个字符数组,从源码的 sds.h/sdshdr 文件 中可以看到 Redis 底层对于字符串的定义 SDS,即 Simple Dynamic String 结构:
/* Note: sdshdr5 is never used, we just access the flags byte directly. * However is here to document the layout of type 5 SDS strings. */ struct __attribute__ ((__packed__)) sdshdr5 { unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ char buf[]; }; struct __attribute__ ((__packed__)) sdshdr8 { uint8_t len; /* used */ uint8_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; struct __attribute__ ((__packed__)) sdshdr16 { uint16_t len; /* used */ uint16_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; struct __attribute__ ((__packed__)) sdshdr32 { uint32_t len; /* used */ uint32_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; struct __attribute__ ((__packed__)) sdshdr64 { uint64_t len; /* used */ uint64_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; };你会发现同样一组结构 Redis 使用泛型定义了好多次,为什么不直接使用 int 类型呢?
因为当字符串比较短的时候,len 和 alloc 可以使用 byte 和 short 来表示,Redis 为了对内存做极致的优化,不同长度的字符串使用不同的结构体来表示。
SDS 与 C 字符串的区别
为什么不考虑直接使用 C 语言的云南idc服务商字符串呢?因为 C 语言这种简单的字符串表示方式 不符合 Redis 对字符串在安全性、效率以及功能方面的要求。我们知道,C 语言使用了一个长度为 N+1 的字符数组来表示长度为 N 的字符串,并且字符数组最后一个元素总是 \0。(下图就展示了 C 语言中值为 "Redis" 的一个字符数组)

这样简单的数据结构可能会造成以下一些问题:
获取字符串长度为 O(N) 级别的操作 → 因为 C 不保存数组的长度,每次都需要遍历一遍整个数组; 不能很好的杜绝 缓冲区溢出/内存泄漏的问题 → 跟上述问题原因一样,如果执行拼接 or 缩短字符串的操作,如果操作不当就很容易造成上述问题; C 字符串 只能保存文本数据→ 因为 C 语言中的字符串必须符合某种编码(比如 ASCII),例如中间出现的 \0 可能会被判定为提前结束的字符串而识别不了;我们以追加字符串的操作举例,Redis 源码如下:
/* Append the specified binary-safe string pointed by t of len bytes to the * end of the specified sds string s. * * After the call, the passed sds string is no longer valid and all the * references must be substituted with the new pointer returned by the call. */ sds sdscatlen(sds s, const void *t, size_t len) { // 获取原字符串的长度 size_t curlen = sdslen(s); // 按需调整空间,如果容量不够容纳追加的内容,就会重新分配字节数组并复制原字符串的内容到新数组中 s = sdsMakeRoomFor(s,len); if (s == NULL) return NULL; // 内存不足 memcpy(s+curlen, t, len); // 追加目标字符串到字节数组中 sdssetlen(s, curlen+len); // 设置追加后的长度 s[curlen+len] =