【はじめに】
先日のエントリ 今更ながらちゃんとmongoDB でnode.jsとmongoで一個作ろうと思ってたものがあったんだけど、データ構造考えてるうえで「あれ?これなんでKVSでやらなあかんわけ?百歩ゆずって使ってみた的な勉強だから、KVSでなければならないことはないんだけど、じゃあKVSってSQLと比較して何が強いわけ?」という気持ちになったので同僚に聞いてみたところ「今更mongoとかやっても分からないだろうから、redisやれ」と言われたので、半年前にインストールで諦めたredisを再びやる。
いやーハラヘッタなー
【ゴール】
node.jsでredisのsetとgetしてみる
【記録】
1, redisをインストール
参考
% cd % wget http://redis.googlecode.com/files/redis-2.6.7.tar.gz % tar -xzvf redis-2.6.7.tar.gz % cd redis-2.6.7 % make
で、
% ls 00-RELEASENOTES BUGS CONTRIBUTING COPYING INSTALL MANIFESTO Makefile README deps redis.conf runtest sentinel.conf src tests utils % src/redis-server [3755] 07 Dec 19:34:12.287 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf [3755] 07 Dec 19:34:12.288 # Unable to set the max number of files limit to 10032 (Operation not permitted), setting the max clients configuration to 992. [3755] 07 Dec 19:34:12.288 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now. _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.6.7 (00000000/0) 32 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 3755 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'
来てる。
2, npmでredisを扱うモジュールをインストール
参考
むしろ駄菓子屋さんで | redisとnode.js mranney/node_redis · GitHub
% cd ~/node/projects/try_redis % npm install redis
で、はいったっぽい。
3, クライアントアプリケーション立てる
~/node/projects/try_redis/test.js
/** * redisつかってみる! **/ var redis = require('redis'); var client = redis.createClient(); // エラーみる client.on('error', function(err){ console.log(err); }); // hogeというキーで、fugaというバリューを保存 client.set('hoge', 'fuga', redis.print); // hogeというキーのバリューを取得 client.get('hoge',redis.print); // hogeというキーのバリューを取得しコールバックに渡す client.get('hoge',function(err,data){ console.log(data); }); // クライアント倒す client.quit(function(err,res){ console.log('Bye!'); });
で、
% node test
こういうエラー出たり
[Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED]
Node.js+ Slideconcertを動かしてみる コードのおもちゃ箱
Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED - stackoverflow
そりゃね、redis-server起動しないとね
気を取り直して
% node test
結果
Reply: OK Reply: fuga fuga Bye!
おお
【so what的な】
一個つくってみなきゃ分からないよねー
【雑感】
redisのインストールもできなかった半年前 (T^T)