Learn redis
Install
mac(osx) install
brew install redis
$redis-server --version
Redis server v=2.8.8 sha=00000000:0 malloc=libc bits=64 build=9b70aa15a1458a30
Install後 file
start(サーバ起動)
redis-server
/usr/local/bin/redis-server
start(サーバ起動)
redis-cli
/usr/local/bin/redis-cli
設定file(config)
/usr/local/etc/redis.conf
db file
dir /usr/local/var/db/redis/
log file
/usr/local/var/log/redis.log
mac auto start
https://gist.github.com/tonypujals/9631143
Set up launchctl to auto start redis
$ ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
/usr/local/opt/redis/ is a symlink to /usr/local/Cellar/redis/x.y.z (e.g., 2.8.7)
You can use launchctl to start and stop redis
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
You can also more conveniently use brew to start, stop, and verify service status
$ brew services list | grep redis
$ brew services start redis
$ brew services stop redis
Datatype
http://redis.io/topics/data-types-intro
- Binary-safe strings
- Lists :
- Sets : collection of unique
- Sorted sets
- Hashes
- Bit arrays
- HyperLogLogs
Redis keys
- 長いkeyはよくない
cli
redis-cli
cli commands
aws 接続例
redis-cli -h mycachecluster.eaogs8.0001.usw2.cache.amazonaws.com -p 6379
簡単テスト
127.0.0.1:6379> set key val
127.0.0.1:6379> get key #valが返却
get all keys
keys *
exmaple
127.0.0.1:6379> keys *
1) "resque:queues"
2) "resque:stat:processed"
3) "resque:stat:failed"
4) "resque:failed"
TYPE key
keyのtypeを確認する
valueを取得するためにGET
を使ってもtypeがsetのkeyは取得できない。
この場合にはGETSET
が必要なので、
FLUSHALL
FLUSHALL
in redis-cli will flush all namespaces in all databases and is generally like dropping a nuclear weapon on the problem
LIST
# 要素の追加
> lpush KEY VALUE # 左から
> rpush KEY VALUE # 右から
# 要素の取り出し
> lpop KEY VALUE
> rpop KEY VALUE
# 要素の表示
# 表示したい区間の index (0, 1, 2...) を指定する
> lrange KEY START END
# N番目の要素を表示
> lindex KEY N
# N番目の要素を更新
> lset KEY VALUE
# Listの長さを取得
> llen KEY
# 任意の区間を取得
> ltrim KEY START END
# Example
> rpush list 1
(integer) 1
> rpush list 2
(integer) 2
> lpush list 0
(integer) 3
> lrange list 0 -1
1) "0"
2) "1"
3) "2"
> lset list 1 New
ok
> lrange list 0 -1
1) "0"
2) "New"
3) "2"
値を確認 - lindex
list indexかな
http://redis.io/commands/LINDEX
LINDEX resque:queue:default 0
GUI
medis
app storeが有料
寄付したいなら買ってもいい ただで使いならbuildして使う
build
git clone https://github.com/luin/medis.git
npm install
npm run deploy
./Medis.app