Redis 攻略(Redis Object)

文章目录

Redis 是什么

我们来看官方定义:

Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Redis 是一个开源(BSD 许可)的内存数据结构存储系统,可用作数据库、缓存、消息代理和流式处理引擎。Redis 提供多种数据结构,例如字符串、哈希表、列表、集合、支持范围查询的有序集合、位图、HyperLogLog、地理空间索引和流。Redis 内置了复制、Lua 脚本、LRU 淘汰机制、事务以及不同级别的磁盘持久化 ,并通过 Redis Sentinel 提供高可用性,并通过 Redis Cluster 实现自动分区。

核心:

Redis 是一个内存存储。Redis 是一个 KV 存储。

解决什么问题:

used as a database, cache, message broker, and streaming engine

可用作数据库、缓存、消息代理和流式引擎

常用于缓存、消息中转、数据流引擎。

支持什么数据结构

提供了 strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams 一系列数据结构。

有什么特性

Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Redis 内置了复制、Lua 脚本、LRU 淘汰、事务和不同级别的磁盘持久化功能,并通过 Redis Sentinel 提供高可用性,并通过 Redis Cluster 提供自动分区功能。

Redis版本选择

推荐是5.0.5,在6.0之前比较新的版本,可以避免多线程额外的复杂度,同时,大厂很多部门都是用的是5.0版本。

当然我这边就先用8.4.0玩玩指令啥的

go 复制代码
root@GoLang:~/proj/goforjob# redis-server --version
Redis server v=8.4.0 sha=00000000:1 malloc=jemalloc-5.3.0 bits=64 build=711c294ad663ee6e

当然为了看源码,我还是下了5.0.5的

go 复制代码
root@GoLang:~/proj/goforjob# git clone https://github.com/redis/redis.git
Cloning into 'redis'...
remote: Enumerating objects: 101144, done.
remote: Counting objects: 100% (185/185), done.
root@GoLang:~/proj/goforjob/redis# ls
00-RELEASENOTES  CODE_OF_CONDUCT.md  INSTALL      MANIFESTO  redis.conf              runtest            runtest-sentinel  src     utils
BUGS             CONTRIBUTING.md     LICENSE.txt  modules    REDISCONTRIBUTIONS.txt  runtest-cluster    SECURITY.md       tests
codecov.yml      deps                Makefile     README.md  redis-full.conf         runtest-moduleapi  sentinel.conf     TLS.md
root@GoLang:~/proj/goforjob/redis# git checkout 5.0.5
Note: switching to '5.0.5'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at c696aebdb Redis 5.0.5

Base理论

可以说 BASE 理论 是 CAP 中一致性的妥协。和传统事务的 ACID 想法不同,BASE 不追求一致性,而是允许数据在一定时间内不一致,但最终达到一致状态,从而获得更高的可用性和性能。

Object是什么?

Redis 是 key-value 存储,key 和 value 在 Redis 中都被抽象为对象,key 只能是字符串,而 value 支持丰富的对象类型,包括 String、List、Set、Hash、Sorted Set 等。

go 复制代码
root@GoLang:~/proj/goforjob/redis# grep -r "typedef struct redisObject" .
./src/server.h:typedef struct redisObject {
./README.md:    typedef struct redisObject {
grep: ./.git/objects/pack/pack-b2b4a245be35794bc1bfbae37e79a483dc778c21.pack: binary file matches
root@GoLang:~/proj/goforjob/redis# 

Object 在内存中是什么样子?

redisObject 定义如下:

/root/proj/goforjob/redis/src/server.h

go 复制代码
// from Redis 5.0.5
#define LRU_BITS 24
typedef struct redisObject {
    unsigned type:4;      // 对象类型
    unsigned encoding:4;  // 编码方式
    unsigned lru:LRU_BITS; /* LRU 时间或 LFU 数据 */
    int refcount;          // 引用计数
    void *ptr;             // 内容指针,指向实际内容
} robj;

LFU 是 Least Frequently Used 的缩写,翻译为"最不常使用"

在结构体 redisObject 中,type 和 encoding 字段使用了 4 个 bit 位 来表示,这意味着每个字段只能表示 16 个不同的值。

unsigned 实际上是 unsigned int 的简写,表示无符号整数类型,专门用于表示没有负数的值。type 和 encoding 字段使用 4 位无符号整数来表示对象的类型和编码方式。

各字段解释:

type:是哪种 Redis 对象类型。

encoding:表示对象底层编码,使用 OBJECT ENCODING [key] 可以查看对应的编码方式。

lru:记录对象访问信息,用于内存淘汰,可以先忽略

refcount:引用计数,用来描述有多少个指针,指向该对象。

ptr:内容指针,指向实际内容。

对象与数据结构

实际操作的主要有 6 个 Redis 对象,它们底层依赖一些数据结构,包括字符串、跳表、哈希表、压缩列表、双端链表等,不同对象可能有依赖相同数据结构,有些数据会先通过这些底层结构

实践优于理论,对象在底层结构中,直接与数据通信,是对外暴露的对象,先熟悉对象概念,再自然探索对象底层结构,这样关联性学习的效果会更好。

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
七夜zippoe1 分钟前
Python错误追踪终极指南:Sentry集成与深度定制实战
数据库·python·sentry·告警策略·错误追踪
lihao lihao8 分钟前
MySQl复合查询
数据库·sql·mysql
aq553560010 分钟前
MySQL-触发器(TRIGGER)
android·数据库·mysql
xcLeigh11 分钟前
从 Oracle RAC 到金仓高可用集群:平滑切换的架构对比与落地指南
数据库·oracle·架构·集群·数据清洗·kingbasees
blurblurblun20 分钟前
Redis底层专题(1)------ sds字符串
数据库·redis·缓存
深蓝轨迹24 分钟前
SQL优化及实战分享
java·数据库·sql
数据知道26 分钟前
MongoDB安全加固最佳实践:CIS基准符合性检查与实施步骤
数据库·安全·mongodb
毕业设计-小慧29 分钟前
计算机毕业设计springboot电影选座与订票系统 基于SpringBoot的影院在线票务管理平台 基于SpringBoot的智能影厅座位预约系统
spring boot·后端·课程设计
light blue bird31 分钟前
MES/ERP报表大致化元素组排类查询
jvm·数据库·.net·ai大数据
xiaoye370841 分钟前
动态代理的使用场景与适用时机
java·数据库·sql