通过 Node.js 上的套接字进行 Redis 连接 [英] Redis Connection via socket on Node.js

查看:63
本文介绍了通过 Node.js 上的套接字进行 Redis 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于共享托管,我在目标主机上的 redis 服务器不在端口上运行,而是在一个非常特定的套接字上运行,该套接字可以通过套接字文件连接,只有我的用户才能访问.

但是,我还没有找到如何通过 node_redisconnect-redis 包中的套接字指定连接,我想使用这些包.

有人知道怎么做吗?

解决方案

更新:我在下面的回答并不完全正确.事实证明,我在下面提到的问题中的解决方案实际上仍然有效.IMO,这更像是巧合,但您可以执行以下操作,并且应该可以:

var redis = require('redis'),client = redis.createClient('/tmp/redis.sock');

正如您从下面的代码片段中看到的,这将传递给 net.createConnection,后者将连接到 unix 套接字 /tmp/redis.sock.>

旧答案:

有一个关于此 node_redis/issues/204 的封闭问题.看来,底层的 node.js net.createConnection API 已经发生了变化.看起来它在 node_redis 的 exports.createClient 函数中是一个很小的修复:

exports.createClient = function (port_arg, host_arg, options) {var port = port_arg ||默认端口,主机 = 主机参数 ||默认主机,redis_client,net_client;net_client = net.createConnection(端口,主机);redis_client = new RedisClient(net_client, options);redis_client.port = 端口;redis_client.host = 主机;返回 redis_client;};

似乎 net.createConnection 会尝试连接到一个 unix 套接字,如果它是用一个参数调用的,看起来像一个路径.我建议您实施修复并发送拉取请求,因为这似乎值得支持.

Because of shared hosting, my redis server on the target host does not run on a port, but on a very specific socket, which can be connected to via the socket file, only accessible to my user.

However, I have not found how I can specify connection via a socket in the node_redis and connect-redis packages, the ones I want to use.

Anyone know how to do it?

解决方案

Update: My answer below is not really correct. It turns out that the solution in the issue I mention below actually still works. It's more of a coincidence, IMO, but you can do something like this, and it should work:

var redis = require('redis'),
    client = redis.createClient('/tmp/redis.sock');

As you see from the code snippet below, this will get passed to net.createConnection which will connect to the unix socket /tmp/redis.sock.

Old answer:

There is a closed issue about this node_redis/issues/204. It seems, thought, that the underlying node.js net.createConnection API has since changed. It looks as though it would be a quite small fix in node_redis' exports.createClient function:

exports.createClient = function (port_arg, host_arg, options) {
    var port = port_arg || default_port,
        host = host_arg || default_host,
        redis_client, net_client;

    net_client = net.createConnection(port, host);

    redis_client = new RedisClient(net_client, options);

    redis_client.port = port;
    redis_client.host = host;

    return redis_client;
};

It seems as though net.createConnection will attempt to connect to a unix socket if it's called with one argument, that looks like a path. I suggest you implement a fix and send a pull request, since this seems like something worth supporting.

这篇关于通过 Node.js 上的套接字进行 Redis 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆