试着安装node-redis的备忘录

因為前幾天製作的mongo資料庫的複寫集 (http://qiita.com/items/b5e99bf4923b84b2bb40) 似乎穩定地工作著,所以下一步我就想著要貪心一點,試試看在另一個資料庫上「以鍵值對的形式,全部存儲在內存中,極速的」redis集群。

被灵感触动了,决定在台湾迎接好日子(注1)。我们选择在台湾特制的船上,将牛久大佛的铜铸造品运送过来。总之,无论如何,立马就试试看吧。

所以,在Node.js中,可以使用许多Redis库,但最受欢迎的应该要数node-redis了。
https://github.com/mranney/node_redis

思い立ったが台湾吉日

2012年,台湾观光局为日本市场推出的旅游口号是「一念之间,台湾吉日」。

安装

npm install redis

结束了。但是由于这是一个客户端,所以需要一个服务器。

安装redis-server

关于获取/安装 Redis 的方法有很多种……

wget http://redis.googlecode.com/files/redis-バージョン.tar.gz
git clone git://github.com/antirez/redis.git

虽然有其他选择,但我使用Ubuntu,所以就用它吧。

sudo apt-get install redis-server

就这样。

Redis服务器安装完成。

当使用apt-get进行安装时,会在/etc/redis/redis.conf创建redis.conf文件,其中daemonize参数默认为yes,timeout参数为300,logfile参数为/var/log/redis/redis-server.log。端口号默认为6379,绑定地址固定为’127.0.0.1’。

可以通过以下方式进行启动、停止和重启。

sudo /etc/init.d/redis-server start | stop | restart

尝试使用node-redis

好的,现在Redis服务器已经启动了,我们来用node-redis进行确认吧。
嗯,试一下在git上找到的示例代码。

    var redis = require("redis"),
        client = redis.createClient();

    client.on("error", function (err) {
        console.log("Error " + err);
    });

    client.set("string key", "string val", redis.print);
    client.hset("hash key", "hashtest 1", "some value", redis.print);
    client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
    client.hkeys("hash key", function (err, replies) {
        console.log(replies.length + " replies:");
        replies.forEach(function (reply, i) {
            console.log("    " + i + ": " + reply);
        });
        client.quit();
    });

    client.set("test", "うひょー");

    client.get("test", function(err, data){
      console.log(data);

    });

结果 (jié guǒ)

由于键/值对,非常简单。

node test.js
Reply: OK
Reply: 0
Reply: 0
2 replies:
    0: hashtest 1
    1: hashtest 2
うひょー

今天我也尝试从另一台机器上进行复制,所以稍后也会记下来,不要忘记(在 redis 上设置复制 http://qiita.com/items/7f97870ef06df9257fd4)。这也非常简单^^ 这真是令人高兴的时代。

请提供以下的中文参考答案。

Redis文档的日文翻译
http://redis.shibu.jp/index.html
Redis指令参考手册
http://redis.io/commands

广告
将在 10 秒后关闭
bannerAds