当前位置:网站首页>Redis多线程与ACL
Redis多线程与ACL
2022-06-26 05:51:00 【少年鹿】
Redis是单线程吗
Redis 的单线程主要是指 Redis 的网络 IO 和键值对读写是由一个线程来完成的,这也是 Redis 对外提供键值存储服务的主要流程。但 Redis 的其他功能,比如持久化、异步删除、集群数据同步等,其实是由额外的线程执行的。
Redis 单线程如何处理那么多的并发客户端连接?
这就是Redis底层IO通信的结构 IO多路复用

# 查看redis支持的最大连接数,在redis.conf文件中可修改, # maxclients 10000 127.0.0.1:6379> CONFIG GET maxclients ##1) "maxclients" ##2) "10000"
Redis提供多线程支持
1. redis 6.0 提供了多线程的支持,redis 6 以前的版本,严格来说也是多线程,只不过执行用户命令的请求时单线程模型,还有一些线程用来执行后台任务,
比如 unlink 删除 大key,rdb持久化等。
redis 6.0 提供了多线程的读写IO, 但是最终执行用户命令的线程依然是单线程的,这样,就没有多线程数据的竞争关系,依然很高效。
redis 6.0 以前线程执行模式,如下操作在一个线程中执行完成

Redis作为单线程为什么效率还那么高
1:因为它所有的数据都在内存中,所有的运算都是内存级别的运算,而且单线程避免了多线程的切换性能损耗问题。正因为 Redis 是单线程,所以要小心使用 Redis 指令,对于那些耗时的指令(比如keys),一定要谨慎使用,一不小心就可能会导致 Redis 卡顿。(内存响应时间是100纳秒)
2: 非阻塞的I/o操作,Redis的IO多路复用:redis利用epoll来实现IO多路复用,将连接信息和事件放到队列中,依次放到文件事件分派器,事件分派器将事件分发给事件处理器。
3:采用单线程避免不必要的上下文切换和竞争条件,也不存在多进程或者多线程导致的切换而消耗 CPU,
4,:数据结构简单,对数据操作也简单,Redis 中的数据结构是专门进行设计的;
redis 6.0 线程执行模式:
可以通过如下参数配置多线程模型:
如:
io-threads 4 // 这里说 有三个IO 线程,还有一个线程是main线程,main线程负责IO读写和命令执行操作
默认情况下,如上配置,有三个IO线程, 这三个IO线程只会执行 IO中的write 操作,也就是说,read 和 命令执行 都由main线程执行。最后多线程将数据写回到客户端。

开启了如下参数:
io-threads-do-reads yes // 将支持IO线程执行 读写任务。

2. client side caching
客户端缓存:redis 6 提供了服务端追踪key的变化,客户端缓存数据的特性,这需要客户端实现

执行流程为, 当客户端访问某个key时,服务端将记录key 和 client ,客户端拿到数据后,进行客户端缓存,这时,当key再次被访问时,key将被直接返回,避免了与redis 服务器的再次交互,节省服务端资源,当数据被其他请求修改时,服务端将主动通知客户端失效的key,客户端进行本地失效,下次请求时,重新获取最新数据。
目前只有lettuce对其进行了支持:
<dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>6.0.0.RELEASE</version> </dependency>
public static void main(String[] args) throws InterruptedException {
RedisClient redisClient = RedisClient.create("redis://192.168.109.200");
Map<String, String> clientCache = new ConcurrentHashMap<>();
StatefulRedisConnection<String, String> myself = redisClient.connect();
CacheFrontend<String, String> frontend =
ClientSideCaching.enable(CacheAccessor.forMap(clientCache),
myself,
TrackingArgs.Builder.enabled().noloop());
String key="csk";
int count = 0;
while (true){
System.out.println(frontend.get(key));
TimeUnit.SECONDS.sleep(3);
if (count++ == Integer.MAX_VALUE){
myself.close();
redisClient.shutdown();
}
}
}ACL
ACL 是对于命令的访问和执行权限的控制,默认情况下,可以有执行任意的指令,兼容以前版本
ACL设置有两种方式
1:命令方式
ACL SETUSER + 具体的权限规则, 通过 ACL SAVE 进行持久化
2 对 ACL 配置文件进行编写,并且执行 ACL LOAD 进行加载
ACL存储有两种方式,但是两种方式不能同时配置,否则直接报错退出进程
1.redis 配置文件: redis.conf
2.ACL配置文件, 在redis.conf 中通过 aclfile /path 配置acl文件的路径
命令方式:
ACL SETUSER alice // 创建一个 用户名为 alice的用户
用如上的命令创建的用户语义为:
1. 处于 off 状态, 它是被禁用的,不能用auth进行认证
2. 不能访问任何命令
3. 不能访问任意的key
4. 没有密码
如上用户alice 没有任何意义。
创建一个对 cached: 前缀具有get命令执行权限的用户,并且设置密码:
acl setuser alice on >pass123 ~cached:* +get auth alice pass123 set a a (error) NOPERM this user has no permissions to run the 'set' command or its subcommand get a a (error) NOPERM this user has no permissions to access one of the keys used as arguments get cached:name vvv
如上,如果访问没有被授权的命令,或者key, 将报错,set 命令没有被授权, key a 没有被授权,
cached:name 可以通过验证。
更符合阅读习惯的格式
ACL GETUSER alice
添加多个访问模式,空格分隔, 注意,切换其他用户进行登录,alice没有admin权限
ACL SETUSER alice ~objects:* ~items:* ~public:*
针对类型命令的约束
ACL SETUSER alice on [email protected] [email protected] >密码 ~*
这里[email protected]: 包含所有得命令 然后用[email protected] 去除在redis command table 中定义的 dangerous 命令

可以通过如下命令进行查看具体有哪些命令属于某个类别
acl cat // 查看所有类别 acl cat dangerous // 查看所有的 dangerous 命令
开放子命令
ACL SETUSER myuser -client +client|setname +client|getname
禁用client 命令,但是开放 client 命令中的子命令 setname 和 getname ,只能是先禁用,后追加子命令,因为后续可能会有新的命令增加。
边栏推荐
- DOM document
- numpy.tile()
- 【C語言】深度剖析數據在內存中的存儲
- Life is so fragile
- Bingc (inheritance)
- How Navicat reuses the current connection information to another computer
- Combined mode, transparent mode and secure mode
- 5 minutes to learn regular expressions
- 421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
- Posting - don't get lost in the ocean of Technology
猜你喜欢

The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government

Combined mode, transparent mode and secure mode

Easy to understand from the IDE, and then talk about the applet IDE

Factory method pattern, abstract factory pattern

Record how to modify the control across threads

MySQL database-01 database overview

Gram 矩阵

类和对象的学习

Kolla ansible deploy openstack Yoga version

组合模式、透明方式和安全方式
随机推荐
Consul服务注册与发现
Kolla ansible deploy openstack Yoga version
从新东方直播来探究下小程序音视频通话及互动直播
ZigBee learning in simple terms lesson 3 external interruption
机器学习 05:非线性支持向量机
Daily production training report (16)
numpy.frombuffer()
A new explanation of tcp/ip five layer protocol model
Interface oriented programming
REUSE_ ALV_ GRID_ Display event implementation (data_changed)
Sofa weekly | open source person - Yu Yu, QA this week, contributor this week
About XXX management system (version C)
RIA想法
卷妹带你学jdbc---2天冲刺Day2
Factory method pattern, abstract factory pattern
How to associate wechat applet QR code to realize two code aggregation
RIA ideas
A love that never leaves
家庭记账程序(第一版)
Pytorch (network model training)