lxyuma BLOG

開発関係のメモ

redisインストール

redis

 

KVS。 

 

要するにmemcachedみたいなものだが、

 

memcachedより少し複雑なデータ構造をサポートしてるらしい。

 

例えば、リストとか、ハッシュとか開発者になじみのあるような形で使える。

 

 

 

参考

はじめてのRedis

http://kotaroito.hatenablog.com/entry/20120612/1339460946

 

ニコニコ生放送に見るredis活用ノウハウ

http://gihyo.jp/dev/feature/01/redis/0001

 

 

速度

 

少し古い本だけど、nosqlファーストガイドのパフォーマンス検証を見ると、

 

比較している(memcached含め)nosqlの中で一番早く

 

InnoDBより10倍早く挿入、5倍で検索できる事が書かれてる。

 

 

 

2012/10月の以下、ブログによると、

 

redisはinnodbより、2、3倍早く、検索、挿入できるらしい。

 

 

パフォーマンス比較 Cassandra、Mongodb、SQLite、H2、MySQL、Postgres

http://d.hatena.ne.jp/cypher256/20121013/p1

 ※とても面白い記事でした。衝撃的な事も書かれてるし

 

 

 

まぁ、条件によって、全然違うのだろうけど。

 

とにかく、早いらしい。

 

 

普及

 

なんかイマイチ、パッとしない。

 

githubとかニコ生で使われているらしいが。

 

 

使いどころが微妙なのか?

 

今回、railsのa/b testのgemを入れたく、

 

そのgemがredis必要なので、インストールする。

 

install

 

AWSのec2(amazon linux ami)に入れます。

$ cd /tmp/

$ wget http://redis.googlecode.com/files/redis-2.6.7.tar.gz

$ tar zxvf redis-2.6.7.tar.gz 

$ cd redis-2.6.7

$ make

$ sudo make install

 

 

あれ、めっちゃ簡単。

 

しかも、何もはまらなかった。

 

良かった、、というか、これはこれで寂しいような、、、

 

 

 

サーバー起動

$ redis-server

[3092] 23 Dec 01:19:29.423 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
[3092] 23 Dec 01:19:29.424 # Unable to set the max number of files limit to 10032 (Operation not permitted), setting the max clients configuration to 3984.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.6.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 3092
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

[3092] 23 Dec 01:19:29.425 # Server started, Redis version 2.6.7
[3092] 23 Dec 01:19:29.425 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[3092] 23 Dec 01:19:29.425 * The server is now ready to accept connections on port 6379


 

あっさり起動。ほお。

 

クライアント

 

別のターミナル画面を起動して

 

クライアントを起動してみる。

 

以下のコマンド叩いてみる。

 

ニコニコ生放送に見る Redis 活用ノウハウ

第2回 Redisの導入と基本機能

http://gihyo.jp/dev/feature/01/redis/0002

 

 

 

$ redis-cli

redis 127.0.0.1:6379> 

 

これが、コンソール

 

 

redis 127.0.0.1:6379> set mykey hello
OK
redis 127.0.0.1:6379> get mykey
"hello"
redis 127.0.0.1:6379> 

 

ほうほう。

 

インクリメント

 

redis 127.0.0.1:6379> incr mynum
(integer) 1
redis 127.0.0.1:6379> incr mynum
(integer) 2
redis 127.0.0.1:6379> decr mynum
(integer) 1
redis 127.0.0.1:6379> 

dbのきりかえ

 

データベースの切り替えを数値(index)で行うらしい。

 

初めはデフォルト0になってる

 

redis 127.0.0.1:6379> set mykey abcdefg
OK
redis 127.0.0.1:6379> select 2
OK
redis 127.0.0.1:6379[2]> set mykey 1234567
OK
redis 127.0.0.1:6379[2]> select 0
OK
redis 127.0.0.1:6379> get mykey
"abcdefg"

 

アトミックな操作

 

アトミック実行(要するにrdbトランザクションみたいなやつ)

 

まとめて実行しちゃう。

 

redis 127.0.0.1:6379> multi
OK
redis 127.0.0.1:6379> get counter
QUEUED
redis 127.0.0.1:6379> set counter 10000
QUEUED
redis 127.0.0.1:6379> exec
1) "10000"
2) OK

 

キー検索

 

次に、キーの検索やってみる

 

redis 127.0.0.1:6379> set user:123:aa 77
OK
redis 127.0.0.1:6379> set user:123:bb 99
OK
redis 127.0.0.1:6379> keys user:123:*
1) "user:123:aa"
2) "user:123:bb"

 

 

 

あと、冗長化について、調べておきたいので

 

今度書く。多分。