Redisを使ってみる

やりたいことなのですが、大量の単語データからの検索が必要なものです。

基本的に今回は検索のみなので、以前携わっていたお仕事の中で使っていたRedisでまずは試してみたい。
実際自分の担当の所ではあまり関係なかったのでRedisと言う言葉とぼやっとしたイメージしかなかったので
今回は復習もかねてきちんと中身を把握しようと思います。

インストール

aptにもありましたが、今回は手抜きせず自分でインストールしてみます。

最新の安定版を http://redis.io/ からダウンロードしてきます。

#ダウンロードしたファイルを解凍します。
$ tar zxf redis-2.6.5.tar.gz
$ ls redis-2.6.5
00-RELEASENOTES  CTRIBUTING  INSTALL    Makefile  deps        runtest        src    utils
BUGS             COPYING       MANIFESTO  README    redis.conf  sentinel.conf  tests
$ cd redis-2.6.5
# ビルド
$ make
....................
....................
Hint: To run 'make test' is a good idea ;)
# makeが終わるとテストしてみなって言われるので、言われるままやってみます。

$ make test
....................
....................
#ずらずらっとテストが始まり最後にはこの様な表示になります。

                   The End

Execution time of different units:
  0 seconds - unit/printver
  0 seconds - unit/quit
  1 seconds - unit/auth
  2 seconds - unit/multi
  2 seconds - unit/protocol
  3 seconds - integration/aof
  1 seconds - integration/rdb
  2 seconds - integration/convert-zipmap-hash-on-load
  8 seconds - unit/expire
  1 seconds - unit/pubsub
  9 seconds - unit/type/set
  1 seconds - unit/slowlog
  8 seconds - integration/replication-4
  1 seconds - unit/introspection
  2 seconds - unit/limits
  3 seconds - unit/scripting
  15 seconds - unit/type/list
  17 seconds - unit/type/list-2
  8 seconds - unit/dump
  21 seconds - unit/type/hash
  21 seconds - unit/aofrw
  23 seconds - unit/other
  23 seconds - integration/replication-3
  25 seconds - unit/type/zset
  13 seconds - unit/bitops
  26 seconds - unit/sort
  28 seconds - integration/replication-2
  20 seconds - unit/maxmemory
  30 seconds - unit/basic
  31 seconds - unit/type/list-3
  22 seconds - unit/obuf-limits
  32 seconds - integration/replication

\o/ All tests passed without errors!

redisサーバー動作確認

srcの中にビルドしたモジュールが入っています。

$ cd src
$ ./redis-server
[8097] 28 Nov 23:06:14.152 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
[8097] 28 Nov 23:06:14.154 # Unable to set the max number of files limit to 10032 (Operation not permitted), setting the max clients configuration to 3984.
[8097] 28 Nov 23:06:14.154 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.6.5 (00000000/0) 32 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 8097
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

[8097] 28 Nov 23:06:14.156 # Server started, Redis version 2.6.5
[8097] 28 Nov 23:06:14.156 # 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.
[8097] 28 Nov 23:06:14.156 * The server is now ready to accept connections on port 6379

ポート番号 6379 で起動したようです。

redisクライアントで動作確認

同じくsrcフォルダにクライアントモジュールがあります。

$ ./redis-cli set test hoge
OK
$ ./redis-cli get test 
"hoge"

ちゃんとデータが入ったようです。

システムにインストール

動作確認できたのでシステムに入れます。

$ sudo make install

#utilの下にインストールスクリプトがありますのでこれを実行します。
$ cd utils
$ ls
build-static-symbols.tcl  mkrelease.sh   redis_init_script      whatisdoing.sh
generate-command-help.rb  redis-copy.rb  redis_init_script.tpl
install_server.sh         redis-sha1.rb  speed-regression.tcl

$ sudo ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server


Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
s#^port [0-9]{4}$#port 6379#;s#^logfile .+$#logfile /var/log/redis_6379.log#;s#^dir .+$#dir /var/lib/redis/6379#;s#^pidfile .+$#pidfile /var/run/redis_6379.pid#;s#^daemonize no$#daemonize yes#;
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
update-rc.d: warning: /etc/init.d/redis_6379 missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
 Adding system startup for /etc/init.d/redis_6379 ...
   /etc/rc0.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc1.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc6.d/K20redis_6379 -> ../init.d/redis_6379
   /etc/rc2.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc3.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc4.d/S20redis_6379 -> ../init.d/redis_6379
   /etc/rc5.d/S20redis_6379 -> ../init.d/redis_6379
Success!
Starting Redis server...
Installation successful!