用(客户端)javascript直接连接到Redis?

时间:2011-06-10 20:54:56

标签: php javascript mysql linux redis

有没有办法使用客户端(不是Node.js)javascript直接连接到Redis?

我已经成功为一些项目使用Node.js + PHP + Redis + Socket.io(对于客户端)。但是,我真的认为这可以进一步简化为PHP + Redis + Browser javascript - 取出Node.js服务器,这是另一台服务器,如果没有必要,我宁愿不使用。对于简单的事情,我认为最好使用Javascript直接连接到Redis。

根据我的理解,Redis只是通过端口提供请求,因此任何可以向该端口发出请求的语言都可以正常工作。理论上,你不能只使用客户端javascript命中redis服务器的端口吗?

我最感兴趣的是发布/订阅功能,这可能是也可能是不可能的。

我不确定您是否可以使用AJAX访问非端口80端口,但从技术上讲,您应该能够使用Nginx反向代理将Redis的端口转发到端口80。

有什么想法吗?只是一个想法。我对目前的解决方案感到非常满意,但想知道我们是否可以做得更好或更有效率并不会有什么坏处。

4 个答案:

答案 0 :(得分:13)

您只能使用客户端JavaScript以及在某些浏览器中使用websockets进行HTTP请求。但是,您应该查看Webdis。它为Redis添加了一个简单的HTTP / JSON层,并且应该完全按照你想要的那样。

编辑:链接已修复。

答案 1 :(得分:7)

真正的障碍是克服浏览器中ajax请求的非端口80/443限制;即使使用Webdis解决方案,因为它通过默认运行端口7379,并且如果从端口80运行,将与您的Apache或Nginx进程冲突。

我的建议是使用nginx proxy_pass指向webdis进程。您可以将流量重定向到端口80并执行ajax请求,而不会出现恼人的安全问题。

下面是一个示例NGINX配置,似乎对我有用。

upstream WebdisServerPool 
{
    server 127.0.0.1:7379; #webdis server1
    server 192.168.1.1:7379; #webdis server 2
}


server {

    listen   80; #
    root /path/to/my/php/code/;
    index index.php;
    server_name yourServerName.com;

    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
            expires max;
            log_not_found off;
    }

    location / {
            # Check if a file exists, or route it to index.php.
            try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /path/to/my/php/code/$fastcgi_script_name;

    }

    location /redis {

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            rewrite /(.*)/(.*)/(.*)$ /$2/$3 break; #ignore the /redis
             proxy_redirect off;
            proxy_pass http://webdisServerPool;
    }
}

在前端,这是获取所有键的示例。例如,所有redis请求都将通过/ redis:

$.ajax({ 
        url: "/redis/KEYS/*", 
        method: 'GET', 
        dataType: 'json', 
        success:function(data)
        {
            $each(data.KEYS,function(key,value){            
                $('body').append(key+"=>"+value+" <br> ");
            });
        }
});

OR

您可以使用:

http://wiki.nginx.org/HttpRedis并自行解析回复。

答案 2 :(得分:3)

我发现直接的Redis http接口与pub / sub不兼容,或者很难设置(在编写本文时)。

以下是基于predis示例的pub / sub的“解决方法”。

http://bradleygoldsmith.tumblr.com/post/35601539836/quick-and-dirty-redis-subscribe-publish-notifications

答案 3 :(得分:1)

我在php中有一堆预定义的redis访问器,我使用'router'样式函数通过带有jQuery的$ .post请求从客户端使用它们。路由器只是一个很大的转变:

 public function router() {
$response = array();
switch ($_POST['method']) {
case 'get_whole_list': //is a convenience function with arg $list_key
  if ($_POST['list_key']) {//which will be provided by the POST request data
$response = $this->get_whole_list($_POST['list_key']);
break;
  } else {
$response = (array('error' => 'must be passed with post key "list_key"'));
break;
  } //and so on, until

//it's time to send the response: 
return json_encode(array('response' => $response));
}

然后你只是echo $myClass->router()

我使用jQuery访问它,如:

redgets.get_whole_list = function(key, callback) {
    $.post(redgets.router, //points to my php file
       {method: 'get_whole_list', //tells it what to do
       list_key: key}, //provides the required args
       function(data) {
           callback($.parseJSON(data).response); //parses the response
       });

这一切都很好;也许它不理想,但它确实使node.js服务器变得多余。 我很惊讶没有人用这种风格制作过通用的redis界面。

相关问题