r - 连接到 R 中的 redis 集群
问题描述
假设redis服务器有几个主机和端口,比如
10.0.1.1:6381
10.0.1.1:6382
10.0.1.2:6381
10.0.1.2:6382
我该如何配置redux::hiredis()
?
我有谷歌,但找不到解决方案。而且我注意到函数db
的参数上有一个注释redis_config
“不要在redis集群上下文中使用。”,所以我想知道这是连接到集群的一种方式。另外,我也尝试过传递redis://10.0.1.1:6381,10.0.1.1:6382,10.0.1.2:6381,10.0.1.2:6382
参数url
,但还是失败了。
有什么建议么?或者有没有其他推荐的套餐?
解决方案
我最初的解决方案是编写一个函数来根据错误消息指向正确的节点。
check_redis <- function(key = "P10000", host = "10.0.1.1", port = 6381) {
r <- redux::hiredis(host = host, port = port)
status <- tryCatch(
{
r$EXISTS(key = key)
},
error = function(e){
address <- str_match(e$message,
"[0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+")
host <- str_split(address, ":", simplify = T)[1]
port <- str_split(address, ":", simplify = T)[2]
return(list(host = host, port = port))
}
)
if (is.list(status)) {
r <- redux::hiredis(host = status$host, port = status$port)
}
return(r)
}
它可以帮助引导到正确的节点,但这种解决方案既不优雅也不高效。所以请指教。
推荐阅读
- php - 为什么我的绑定参数查询不起作用?
- python - 用 Python 填充图像中的透明孔
- code-coverage - 如何更改 Bullseye tmp 目录路径?一个 rhel 机器中的多个用户不能同时使用
- android-jetpack-compose - 如何制作占据整个屏幕宽度的元素的 LazyRow?
- kubernetes - 如何在现有的裸机 kubernetes 集群中编辑 etcd 配置
- visual-studio-code - 有没有办法将 VSCode 配置为始终在新窗口中打开项目?
- python - 如何在python 3中生成随机浮点数
- oracle-apex - 如何通过使用 BI Publisher 在 APEX 中传递参数来打印单个查询
- javascript - highlight.js 的自定义语言定义
- c - 使用 sem_t 时大小为 8 的无效写入