Redisコマンド

前提
OS : Rocky Linux 9.1
Redis version : 6.2.7

redis-cli

Sring型

set key01 val01
get key01

unlink key01
get key01

mset key11 val11 key12 val12
mget key11 key12


set counter01 0
incr counter01
get counter01
decr counter01
get counter01


List型

lpush key21 val21
lpush key21 val22
rpush key21 val23
rpush key21 val24

lrange key21 0 -1

lpop key21
lrange key21 0 -1
rpop key21
lrange key21 0 -1

unlink key21
lrange key21 0 -1


Hash型

hset hash01 key31 val31 key32 val32
hset hash01 key33 val33

hgetall hash01
hget hash01 key31

hkeys hash01
hvals hash01

hdel hash01 key31
hgetall hash01

unlink hash01
hgetall hash01


Set型

sadd key41 val41 val42
sadd key41 val42
sadd key41 val43

smembers key41

srem key41 val41
smembers key41

unlink key41
smembers key41


Sorted Set型

zadd key51 10 val51
zadd key51 20 val52
zadd key51 30 val53

zrange key51 0 -1
zrange key51 0 -1 withscores

zrange key51 -inf +inf byscore withscores
zrange key51 +inf -inf byscore rev withscores


zrem key51 val51
zrange key51 0 -1

unlink key51
zrange key51 0 -1

 

DB内の全キー削除
keys *
flushdb async
keys *

 

サーバー情報取得
info


スナップショット取得

redis-cli dbsize
redis-cli bgsave
redis-cli lastsave

date --date "@$(redis-cli lastsave)" "+%Y/%m/%d %H:%M:%S"

ls -l /var/lib/redis/dump.rdb
cp -a /var/lib/redis/dump.rdb /tmp/dump.rdb.bk20220103
ls -l /tmp/dump.rdb.bk20220103

redis-check-rdb /tmp/dump.rdb.bk20220103


リストア

systemctl stop redis

ls -l /var/lib/redis/dump.rdb
cp -a /tmp/dump.rdb.bk20220103 /var/lib/redis/dump.rdb
ls -l /var/lib/redis/dump.rdb

systemctl start redis

 

スローログ

https://tsunokawa.hatenablog.com/entry/2016/08/18/191231

config get slowlog-log-slower-than
config get slowlog-max-len

config set slowlog-log-slower-than 0
config set slowlog-max-len 1000

slowlog get 3

自動採番されたID
コマンド実行時のUnixタイムスタンプ
コマンド実行にかかった時間、マイクロ秒単位
コマンドとそのパラメータ


slowlog len
slowlog reset


トランザクション

set counter01 0
get counter01

multi
set counter01 10
incr counter01
incr counter01
get counter01
exec
get counter01


set counter02 0
get counter02

multi
set counter02 10
incr counter02
incr counter02
get counter02
discard
get counter02