架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 793|回复: 0

[资料] .NET/C# 使用 StackExchange.Redis 性能优化

[复制链接]
发表于 2024-12-23 21:37:10 | 显示全部楼层 |阅读模式
需求:一般 .NET 开发人员会使用 StackExchange.Redis 库对 Redis 进行读写操作,今天查看网络链接发现 StackExchange.Redis 库使用单例模式,竟然和 Redis 服务器建立了 4 个 TCP 长链接。

网上搜索,找到网友回答“默认情况下,StackExchange.Redis 中的每个 ConnectionMultiplexer 实例都会为每个缓存创建至少 2 个连接。一个用于交互命令,一个用于发布/订阅消息。

如果不用 pub/sub 发布订阅功能的话,其实可以关闭来优化连接占用。

在 Redis 服务器中使用 CLIENT LIST 命令,可以查看所有客户端连接,优化前如下图

QQ截图20241223212246.png

客户端 flags 组合含义如下:

A: connection to be closed ASAP
b: the client is waiting in a blocking operation
c: connection to be closed after writing entire reply
d: a watched keys has been modified - EXEC will fail
e: the client is excluded from the client eviction mechanism
i: the client is waiting for a VM I/O (deprecated)
M: the client is a master
N: no specific flag set
O: the client is a client in MONITOR mode
P: the client is a Pub/Sub subscriber
r: the client is in readonly mode against a cluster node
S: the client is a replica node connection to this instance
u: the client is unblocked
U: the client is connected via a Unix domain socket
x: the client is in a MULTI/EXEC context
t: the client enabled keys tracking in order to perform client side caching
T: the client will not touch the LRU/LFU of the keys it accesses
R: the client tracking target client is invalid
B: the client enabled broadcast tracking mode
如何关闭 pub/sub 发布订阅连接呢?

方案一

修改 .NET 代码如下:

方案二(推荐)

直接修改连接字符串,如下:

修改完成后,重新运行程序,可以看到已经关闭了发布订阅连接,优化后,如下图:

QQ截图20241223212502.jpg

优化前:建立了 4 个 TCP 连接,优化后:建立了 2 个 TCP 连接。

参考:

超链接登录可见。
超链接登录可见。
超链接登录可见。





上一篇:【转】Angular 代码混淆加密变得简单
下一篇:.NET/C# 测试代码多个 Main 方法解决方案
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-6-15 19:53

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表