RedisãRubyãã触ã£ã¦ã¿ã
ååãå¤æ©è½é«éãªãã¼ããªã¥ã¼ã¹ãã¢Redisãã¤ã³ã¹ãã¼ã«ãã¦ãæ¬ä½ã½ã¼ã¹ã«ã¤ãã¦ããã¯ã©ã¤ã¢ã³ãã§å¤ãä¿åãããåå¾ããããã¦ã¿ã¾ããã
ä»åã¯åãäºãRubyãããã£ã¦ã¿ããã¨æãã¾ãã
ãã¨ãæå¾ã®æ¹ã§Ruby+Redisã§ãã¤ãã¿ã¼ã®ã¿ã¤ã ã©ã¤ã³çãªãã®ã表ç¾ãã¦ã¿ããã
ã¤ã³ã¹ãã¼ã«
ã©ã¤ãã©ãªã¯gemã§æä¾ããã¦ãã¾ãã
$ gem install redis
使ãæ¹
Redis.newã§ãªãã¸ã§ã¯ããçæãã¦ããã¨ã¯Redisã®ã³ãã³ã(http://redis.shibu.jp/commandreference/index.html)ã«ãªãã£ã¦ã¡ã½ãããå®è£
ãã¦ããããã§ãã
ãã¼ã«ã«ãã¹ãã®æ¨æºãã¼ãã§Redisãåãã¦ããå ´å
redis = Redis.new
ãã¹ãã¨ãã¼ããæå®ããå ´å
redis = Redis.new(:host => "10.0.1.1", :port => 6380)
Unixãã¡ã¤ã³ã½ã±ããã使ç¨ããå ´å
redis = Redis.new(:path => "/tmp/redis.sock")
å¤ã®ä¿åã¨åå¾
Rubyã©ã¤ãã©ãªå´ã§å¤ãã»ãããã¦åå¾ãã¦ã¿ã
redis.set "foo", "bar" => "OK" redis.get "foo" => "bar"
redisæ¬ä½ã«ä»å±ãã¦ããæ¨æºã¯ã©ã¤ã¢ã³ãå´ã§å¤ãåå¾
> get "foo" "bar"
æ¨æºã¯ã©ã¤ã¢ã³ãå´ã§å¤ãæ´æ°ãã
> set "foo" "baz" OK
Rubyå´ã§åå¾
redis.get "foo" => "baz"
ããä¸ã¤ãã¼ã¨å¤ã®ãã¢ã追å
redis.set "hoge", "tara" => "OK"
è¤æ°ãã¼ãæå®ãã¦ãã£ãºãã«å¤ã®é åãåå¾
redis.mget "hoge", "foo" => ["tara", "baz"]
ãªã¹ãæä½
ãªã¹ãã®æ«å°¾ã«å¤ã追å
redis.rpush "tweets", "this is my first tweet." => 1
ãªã¹ãã®0çªç®ãã100çªç®ã¾ã§ã®å¤ãåå¾(å®éã«ã¯åå¨ããæ°ã ãåå¾ãã)
redis.lrange "tweets", 0, 100 => ["this is my first tweet."]
ããã²ã¨ã¤å¤ãæ«å°¾ã«è¿½å
redis.rpush "tweets", "hello world!" => 2
0çªç®ãã100çªç®ã¾ã§ã®å¤ãåå¾(å®éã«ã¯åå¨ããæ°ã ãåå¾ãã)
redis.lrange "tweets", 0, 100 => ["this is my first tweet.", "hello world!"]
redisæ¬ä½ã«ä»å±ãã¦ããæ¨æºã¯ã©ã¤ã¢ã³ããããªã¹ããåå¾ãã¦ã¿ã
> lrange "tweets" 0 100 1) "this is my first tweet." 2) "hello world!"
ãªã¹ãã®æ«å°¾ã¸ã®è¿½å ãï¼ï¼åç¹°ãè¿ãã¦ã¿ãã
10.times {|i| redis.rpush("tweets", "tweet #{i}") } => 10
çµæãåå¾ãã¦ã¿ã
redis.lrange "tweets", 0, 10 => ["this is my first tweet.", "hello world!", "tweet 0", "tweet 1", "tweet 2", "tweet 3", "tweet 4", "tweet 5", "tweet 6", "tweet 7", "tweet 8", "tweet 9"]
ã§ã¯ãããã£ãæéãè¨æ¸¬ãã¤ã¤1000åç¹°ãè¿ãã¦ã¿ã
st = Time.now; 1000.times {|i| redis.rpush("tweets", "tweet #{i}") }; et = Time.now; et - st => 0.078255
ã¯ãw ãã³ãã«å ¥ã£ã¦ãããªï¼
redis.lrange "tweets", 0, 2 => ["this is my first tweet.", "hello world!", "tweet 0"]
æ«å°¾ã¯ã©ããªã£ã¦ãã®ããªï¼
redis.lrange "tweets", -3, -1 => ["tweet 997", "tweet 998", "tweet 999"]
.。+:â。q(ãï½¥Ïï½¥ã)pâï¾ã¤ã¤ã¨ã¤ã¤ã¨ã¼
調åã«ä¹ã£ã¦è¿½å ã§1ä¸åå ¥ãã¦ã¿ãã
st = Time.now; 10000.times {|i| redis.rpush("tweets", "tweet #{i}") }; et = Time.now; et - st => 0.755149
ï¼ä¸åç¹°ãè¿ãã¦ãï¼ç§ä»¥å ...
ããã§ãªã¹ãã®è¦ç´ æ°ã確èªãã¦ã¿ã
redis.llen "tweets" => 11012
ãªã¹ãã®æ«å°¾ã確èªãã¦ã¿ã
redis.lrange "tweets", -3, -1 => ["tweet 9997", "tweet 9998", "tweet 9999"]
ã»ã»ã¼
å¤ã®åé¤
ãã¼ãæå®ãã¦å¤ãåé¤
redis.del "tweets" => 1
ç¾å¨ã®ãªã¹ãé·ã確èª
redis.llen "tweets" => 0
æ§é åããããã¼ã¿ãä¿åããã
ããã·ã¥ãJSONã«å¤æãã¦ä¿åãã¦ã¿ããã
ãããªãããã®
tweet = {:id = 1, :user => "shin", :time => Time.now, :body => "hello redis!"}
JSONã§ä¿åãã
require "json"
ãã¼ã¯"msg_(ãã¼çªå·)"ã¨ãã¾ããä»åã¯"msg_1"ã¨ãã§ã
ä¿å
redis.set "msg_1", tweet.to_json => "OK"
åå¾ãã¦JSONããRubyã®ããã·ã¥ã«å¤æ
JSON.parse(redis.get("msg_1")) => {"id"=>1, "user"=>"shin", "time"=>"2011-10-06 16:58:23 +0900", "body"=>"hello redis!"}
ãã°ãããw
ããã¾ã§ã®ä»çµã¿ã使ã£ã¦ãã¤ãã¿ã¼ã®ã¿ã¤ã ã©ã¤ã³ã®ãããªã¢ããå®ç¾ãã¦ã¿ã
å®ãã¼ã¿ã¯"msg_(ã¡ãã»ã¼ã¸çªå·)"ã¨ãããã¼ã§ä¿åããã¦ã¼ã¶ã®ã¿ã¤ã ã©ã¤ã³ã¯"home_(ã¦ã¼ã¶id)"ã®ãããªãã¼ã§ãªã¹ãã«ã¡ãã»ã¼ã¸ã®ãã¼ã追å ãã¦ããã
ä»åã¯é©å½ã«ã¦ã¼ã¶IDã"1"ã ã¨ãã¦ã¿ã¤ã ã©ã¤ã³ã表ç¾ãããªã¹ãã®ãã¼ã¯"home_1"ã§ãã
ä¸ã§ä½ã£ããã¤ã¼ãã®ãã¼ãèªåã®ãã¼ã ã®ãªã¹ãã«è¿½å ãã¾ãã
redis.rpush "home_1", "msg_1" => 1
ããï¼ã¤ã»ã©åãããã«ä¿åãã¾ããã
tweet = {:id => 2, :user => "shin", :time => Time.now, :body => "this is second tweet :-)" } redis.set "msg_2", tweet.to_json => "OK" redis.rpush "home_1", "msg_2"=> 2
tweet = {:id => 3, :user => "shin", :time => Time.now, :body => "this is third tweet :-)" } redis.set "msg_3", tweet.to_json => "OK" redis.rpush "home_1", "msg_3" => 3
redis.lrange("home_1", -100, -1).map{|key| JSON.parse(redis.get key)}
ã®ããã«åªæãå±ããã¨...
=> [{"id"=>1, "user"=>"shin", "time"=>"2011-10-06 16:58:23 +0900", "body"=>"hello redis!"}, {"id"=>2, "user"=>"shin", "time"=>"2011-10-06 17:03:28 +0900", "body"=>"this is second tweet :-)"}, {"id"=>3, "user"=>"shin", "time"=>"2011-10-06 17:05:26 +0900", "body"=>"this is third tweet :-)"}]
ç´è¿100件(ä»åã¯ï¼ä»¶ããç»é²ãã¦ãªããã3件)ã®ããã·ã¥ãã¼ã¿ã®é
åãå¾ããã¾ããã
Webãµã¤ãmçã§è¡¨ç¤ºãããªãæ´ã«.reverseãã¦éé ã«ãã¦ããã°ãã®ã¾ã¾ä½¿ãã¾ããw
試ãã«åå¾ãããã®ãå¤æ°ã«å ¥ãã¦åºåãã¦ã¿ããã
home_timeline = redis.lrange("home_1", -100, -1).map{|key| JSON.parse(redis.get key)}.reverse home_timeline.each do |tweet| puts "#{tweet["user"]} | #{tweet["body"]} (#{tweet["time"]})" end
çµæ
shin | this is third tweet :-) (2011-10-06 17:05:26 +0900) shin | this is second tweet :-) (2011-10-06 17:03:28 +0900) shin | hello redis! (2011-10-06 16:58:23 +0900)
ã¯ã³ããâ(๑◕â◕๑)
...ã¯ã³ããâã¨ãæã£ããã©ãã³ããã®æ¹ãå¹çãããã§ããw
keys = redis.lrange("home_1", -100, -1) redis.mget(*keys).map{|value| JSON.parse(value)}
æåã®ããæ¹ã 㨠[ãã¼ãªã¹ãã®åå¾ + ãã¼ã®æ°]ã ãRedisã¨ã®ããåããå¿ è¦ã§ãããå¾ã®æ¹ã ã¨[ãã¼ãªã¹ãã®åå¾ + å¤ãªã¹ãã®åå¾]ã®ï¼åã§æ¸ã¿ã¾ãã
ææ³
åãã¦RubyããRedisãæä½ãã¾ããããã¡ã½ãããå¼æ°ãRedisã®ã³ãã³ãã«ãã£ã¦å®è£
ããã¦ããã®ã§é常ã«ç´ ç´ãªä½¿ãå¿å°ã§ããã解ããããã§ãã
Redisã«ã¯ä¸è¨ä»¥å¤ã«ããã·ã¥ããã£ã¯ã·ã§ããªãªã©ãããã¾ãã®ã§ãããããå¤åRubyããã¤ã±ãã¨æããã¾ãã
åãªããã¼ããªã¥ã¼ã¹ãã¢ã«æ¯ã¹ã¦Redisã¯ã¢ããªã±ã¼ã·ã§ã³å´ã«ç足ã¤ã£ããã§ãã¦ãå°è±¡ã§ããªã¹ããªã©ã®ãã¼ã¿ã®æ±ããæ ¼æ®µã«ãããããã§ãã
ä¸è¨ã®ãã¤ãã¿ã¼çãªãã®ã®ä»ã«ããä¾ãã°ã¹ãã¼ããã©ã³ããä½ç½®ã移åãããã³ã«ç·¯åº¦çµåº¦ãå ±åããã¦ãããããªã¹ãã«ã¤ã£ããã§ããã°ããªã¹ãä¸ã¤ãä¸ååã®ç§»åçµè·¯ã¨ãã¦æå»ã¨ä¸ç·ã«ä¿åãã¦ãæ è¡ã®ç§»åçµè·¯ãè¨é²ããããã©ã³ãã³ã°ã®ç§»åçµè·¯ãä¿åãã¦å¾ããGoogleMapä¸ã«è¡¨ç¤ºãããåºæ¥ã¾ãã+ï¼0ï¾ã»âã»ï¼ +
ãããã¯å ¨ã¦ãªã¬ã¼ã·ã§ãã«ãã¼ã¿ãã¼ã¹ã§ãåºæ¥ããã¨ã§ãããè² è·çã«Redisã®æ¹ãå¼·ããã§ãããã³ã¼ãã£ã³ã°ããä¸ã§ããã£ã¨ç´æçã§ãã
ããå°ãããããã¨å¼ã£ã¦ã¿ã¦æ©ä¼ããã£ããå®éã®ã¢ããªã±ã¼ã·ã§ã³éçºã«ä½¿ã£ã¦ã¿ããã§ãã :-)
次åã¯Erlangããã®æä½ããã£ã¦ã¿ã¾ãã