LoginSignup
3
4

More than 5 years have passed since last update.

Redisメモ / まずはターミナルで確認したい!

Last updated at Posted at 2015-09-14

開発で使っているので覚え書き。最低限、データが入ったかどうかを確認するため。

起動 / アクセス

Macにbrew install で入れてるのが前提。

  • redis-server で起動。 #redis-server start とすると怒られる^^;
  • yumでinstallの場合はサービス化できるようですが、ひとまずMacなので都度起動
  • デフォルトでポート6379, -h でオプションを確認
% redis-server 
57635:C 14 Sep 13:09:14.230 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
57635:M 14 Sep 13:09:14.235 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 57635
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

57635:M 14 Sep 13:09:14.241 # Server started, Redis version 3.0.2
57635:M 14 Sep 13:09:14.242 * The server is now ready to accept connections on port 6379

アクセス

redis-cli を使う。

% redis-cli
127.0.0.1:6379> 
127.0.0.1:6379> select 0  # 0番目のDBにアクセス
OK
127.0.0.1:6379> keys *    # 登録されてるキーを表示
(empty list or set)       # 最初なんで何も無い

127.0.0.1:6379> set name namae
OK
127.0.0.1:6379> keys *
1) "name"                # キーが出来た
127.0.0.1:6379> get name # 値を取ってみる
"namae"

127.0.0.1:6379> del name  # キーを消してみる
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)

incr /decrby

127.0.0.1:6379> get test_id    # 1をセット
"1"
127.0.0.1:6379> incr test_id   # インクリメント
(integer) 2
127.0.0.1:6379> incr test_id
(integer) 3
127.0.0.1:6379> incr test_id
(integer) 4
127.0.0.1:6379> get test_id    # 増えている
"4"

127.0.0.1:6379> decrby test_id 4 # 4減らす
(integer) 0
127.0.0.1:6379> get test_id
"0"

キーの確認

127.0.0.1:6379> keys *id*  # キー名にid を含むものを抽出
1) "test_id"
2) "int_id"
127.0.0.1:6379> exists test_id # existsで存在確認
(integer) 1
127.0.0.1:6379> exists test_id_2
(integer) 0

127.0.0.1:6379> get hoge  # 存在しないキーを呼び出したら
(nil)                     # nilが返る

データの書き出し

基本はメモリ上にしかデータが載っていないので、Redisのプロセスを落としたら、データは消えてしまう。

  • bgsaveで、デフォルトでRedisを起動したディレクトリ上にデータ書き出し。
  • 同じディレクトリでRedisを立ち上げた場合、ダンプしたデータをメモリ上に読み込んでくれる。
# cliからの操作
127.0.0.1:6379> bgsave
Background saving started

# Redisのプロセス側での出力
83889:M 14 Sep 23:15:53.046 * Background saving started by pid 83910
83910:C 14 Sep 23:15:53.047 * DB saved on disk
83889:M 14 Sep 23:15:53.110 * Background saving terminated with success

ダンプデータを覗いてみる

まずこんな感じでデータを用意。({"test": "data"})

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set test data
OK
127.0.0.1:6379> keys *
1) "test"
127.0.0.1:6379> bgsave
Background saving started

dump.db を出力してみる。

% hexdump dump.rdb 
0000000 52 45 44 49 53 30 30 30 36 fe 00 00 04 74 65 73
0000010 74 04 64 61 74 61 ff 97 36 c8 c8 ec af 54 43   
000001f

こちらのドキュメントによると、最初はRedisのマジックナンバーが指定されているとのこと。

  • 52 45 44 49 53 -> "REDIS"
  • 30 30 30 36 -> DBのバージョン(この場合0006)

-c オプションを付けてみると、なんとなく値見えるような...

% hexdump -c dump.rdb 
0000000   R   E   D   I   S   0   0   0   6   ?  \0  \0 004   t   e   s
0000010   t 004   d   a   t   a   ? 227   6   ?   ?   ?   ?   T   C    
000001f

Ref. Redis RDB Dump File Format

日本語を入れてみる

redis-cli だけだと化けてしまう...。

  • redis-cli --raw でコマンドラインツールを起動すると大丈夫とのこと。
    • (Use raw formatting for replies (default when STDOUT is not a tty).)
127.0.0.1:6379> set data2 日本語いれてみる
OK
127.0.0.1:6379> keys *
1) "data2"
2) "test"
127.0.0.1:6379> get data2
"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e\xe3\x81\x84\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\xbf\xe3\x82\x8b"    # 化けてしまう....
127.0.0.1:6379> save

127.0.0.1:6379> exit
% redis-cli --raw
127.0.0.1:6379> keys *
data2
test
127.0.0.1:6379> get data2
日本語いれてみる  # 大丈夫

雑感

まだまだちゃんと使っているわけではありませんが、とても簡単にアクセスできたので驚きました。
botのデータを格納するとかには向いている、というのは良くわかりました。

参考

以下のサイトを参考にさせていただきました。
ありがとうございました!

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4