forget for get

覚えるために忘れる

redis-cliコマンドまとめ

redis-cliコマンドの自分用のまとめです。
 
参考記事はこちら。

qiita.com

基本

set name lightwill
get name
setnx name lightwill2
setnxだと上書きできない
 
有効期限を指定 5秒
setex name 5 lightwill
psetex name 5000 lightwill
有効期限を確認
ttl name
pttl name
 
取得してから更新
getset name light
 追記
set str abc
append str def //abcdef
バイト数取得(日本語だと文字数にならない)
strlen name
日本語が文字化けしないようにする
redis-cli --raw
 
複数
mset name lightwill age 35
mget name age
msetnx name lightwill2 age 135 blood a
既存キーがあると新規キーも登録されない
 
加算減算
set age 30
incr age //31
incrby age 2 //33
decr age //32
decrby age 2 //30
incrbyfloat age 0.5 //30.5
incrbyfloat age -0.5 //30
 

ハッシュ

hset lightwill hp 100
hset lightwill pow 20
hget lightwill hp
hsetnx lightwill hp 200
hgetall lightwill
hdel lightwill pow
hlen lightwill
hexists lightwill hp
hkeys lightwill
hvals lightwill
hmset lightwill def 30 spd 50
hmget lightwill hp spd
hincrby lightwill hp -1
hincrbyfloat lightwill hp -0.5

 

セット(順番のない、重複のないメンバーの集合)

sadd larc hyde
sadd larc ken tetsu sakura
smembers larc
scard larc //件数取得
sismember larc hyde
srem larc sakura
srandmember larc
srandmember larc 2
srandmember larc -5 //重複ありで5件
spop larc //ランダムに取得して削除
spop larc 2 //件数指定はバージョン2.6、2.8、3.0では不可
smove larc vamps hyde

sdiff saiueo sai su //eo
sdiffstore seo saiueo sai su
sinter saiueo sai //ai
sinterstore sai2 saiueo sai
sunion sai su //aiu
sunionstore saiu sai su
 

ソート済みセット(スコアで順位付け、メンバーの重複なし、同じスコアはあり)

zadd ranking 100 melon
zadd ranking 80 mango 80 momo 70 pine
zadd ranking 120 melon //スコア更新
zscore ranking melon
zcard ranking //件数取得
zincrby ranking 10 melon
zincrby ranking -10 pine
zincrby ranking 50 apple //追加される
zrem ranking pine
zrem ranking pine apple
zrange ranking 0 2
zrange ranking 0 2 withscores //スコアも取得
zrange ranking 0 -1 //すべて
zrevrange ranking 0 -1 //降順
zcount ranking 60 80
zcount ranking (60 (80 //60 <、< 80
zcount ranking -inf +inf //システムの最小値と最大値
zrangebyscore ranking 60 80
zrevrangebyscore ranking 80 60 withscores
zrevrangebyscore ranking inf -inf limit 0 2
zremrangebyscore ranking -inf 10 //10以下は削除
zrank ranking melon
zrevrank ranking melon
zremrangebyrank ranking 80 100 //81位から101まで削除

zinterstore year 2 month1 month2 //積集合の合計
zinterstore year 3 month1 month2 month3 weights 1 2 3 //各スコアを倍にして重みづけ
zinterstore year 2 month1 month2 aggregate max
zinterstore year 2 month1 month2 aggregate min
zunionstore year 3 month1 month2 month3 //和集合 weights、aggregateも同じ
 

リスト(順番保持、重複あり)

lpush list a //先頭(left)に追加
lpush list b c d
lpushx none a //キーがなければ何もしない
lrange list 0 -1
lpop list //d
rpush list d //末尾(right)に追加
rpushx none a
rpop list //d
llen list
lrem abababa 1 a //bababa
lrem abababa -1 a //babab
lrem abababa 0 a //bbb
lpush aiueo a i u e o //oeuia
linsert aiueo after u k //oeukia
linsert aiueo before u s //oesukia
lindex abcde 2 //c
lindex abcde -2 //b
lset abcde 0 z //zdcba
ltrim abcde 1 3 //dcb
rpoplpush abc def //cb afed
 
blpop list1 list2 0 //どれかから値を取得するまでブロック
brpop list1 list2 0
brpop list3 3 //3秒待つ
brpoplpush list1 list2 0 //list1から2へ移動させるまでブロック
 

その他

exists list1
exists list1 list2 list3 //個数が返ってくる
type list1
rename bakarudi summers //変更先のキーがあったら上書き
renamenx bakarudi summers
del list

expire list1 5 //期限を設定
expireat list2 1592382942 //unixtime指定
pexpire list1 5000
pexpireat list 1592383942999
persist list //期限を削除
randomkey //ランダムにキーを返す
keys list* //パターンに合うキーを取得
 
部分操作
set str abcdefg
getrange str 0 2 //abc
getrange str 0 -1 //abcdefg
getrange str -3 -1 //efg
setrange str 2 CD //abCDefg
setrange str 7 hi //abCDefghi
setrange str 11 lm //abCDefghi\x00\x00lm 間にzero-bytesが入る
 
ビット操作
setbit mybit 0 1
getbit mybit 0
bitcount mybit
bitop and result bit1100 bit1010 //result=1000
bitop or result bit1100 bit1010
bitop xor result bit1100 bit1010
bitop not result bit1100
 
dump larc //シリアライズしたデータ
restore larc2 0 "\x02\x02\x04yuki\x05tetsu\b\x00#\xfcO\xc2n\x9f*\x8b"
move larc 1 //別のDBへ移動(デフォは0)
select 1