LoginSignup
3
4

More than 5 years have passed since last update.

MacでRedisを使ってみる

Posted at

Redisのインストール

Homebrewからインストールできます。

$ brew install redis
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/redis-2.8.13.mavericks.bottle.tar.gz
######################################################################## 100.0%
==> Pouring redis-2.8.13.mavericks.bottle.tar.gz
==> Caveats
To have launchd start redis at login:
    ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
Then to load redis now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
Or, if you don't want/need launchctl, you can just run:
    redis-server /usr/local/etc/redis.conf
==> Summary
?  /usr/local/Cellar/redis/2.8.13: 10 files, 1.3M

redisサーバーの起動

次のコマンドでRedisを起動できます。

$ redis-server
[86861] 19 Jul 00:04:43.047 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
[86861] 19 Jul 00:04:43.049 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 2.8.13 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 86861
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

[86861] 19 Jul 00:04:43.055 # Server started, Redis version 2.8.13
[86861] 19 Jul 00:04:43.055 * The server is now ready to accept connections on port 6379

redisに接続

起動したRedisに接続するには、次のコマンドを使います。

$ redis-cli
127.0.0.1:6379>

Redisへの値の設定と取得

setコマンド

Redisに値を設定するにはsetコマンドを使用します。

set [key] [value]

127.0.0.1:6379> set test-key test
OK

getコマンド

Redisに設定した値を取得するにはgetコマンドを使用します。

get [key]

127.0.0.1:6379> get test-key
"test"

delコマンド

Redisに設定したキーを削除するには、delコマンドを使用します。

127.0.0.1:6379> del test-key
(integer)1

その他

Redisに登録してあるすべてのキーを削除する

$ redis-cli --raw -n 0 KEYS "*" | xargs redis-cli -n 0 DEL
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