redis 常用命令總結
阿新 • • 發佈:2018-12-04
keys
返回滿足給定條件的所有key
127.0.0.1:6379> keys *
1) "testlist2"
2) "testset4"
3) "testset2"
4) "mytest"
5) "num2"
6) "testset1"
7) "testset3"
8) "testset"
127.0.0.1:6379> keys testset*
1) "testset4"
2) "testset2"
3) "testset1"
4) "testset3"
5) "testset"
exists
確認一個key 是否存在 127.0.0.1:6379> exists test (integer) 0 127.0.0.1:6379> exists testset (integer) 1
dbsize
返回當前資料庫中key 的數目。
127.0.0.1:6379> dbsize
(integer) 10
127.0.0.1:6379> keys *
1) "testlist2"
2) "testset4"
3) "testset2"
4) "mytest"
5) "name_new"
6) "num2"
7) "testset1"
8) "num3"
9) "age1"
10) "num1"
del
刪除一個key 127.0.0.1:6379> keys testset* 1) "testset4" 2) "testset2" 3) "testset1" 4) "testset3" 5) "testset" 127.0.0.1:6379> del testset (integer) 1 127.0.0.1:6379> keys testset* 1) "testset4" 2) "testset2" 3) "testset1" 4) "testset3"
flushdb
刪除當前選擇資料庫中的所有key。
127.0.0.1:6379[1]> keys *
1) "age"
127.0.0.1:6379[1]> flushdb
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> dbsize
(integer) 10
flushall
刪除所有資料庫中的所有key。 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> set age 15 OK 127.0.0.1:6379[1]> keys * 1) "age" 127.0.0.1:6379[1]> flushall OK 127.0.0.1:6379[1]> dbsize (integer) 0 127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> dbsize (integer) 0
expire
設定一個key 的過期時間(單位:秒),ttl命令檢測有效時長
127.0.0.1:6379> keys testset*
1) "testset4"
2) "testset2"
3) "testset1"
4) "testset3"
127.0.0.1:6379> expire testset3 10
(integer) 1
127.0.0.1:6379> ttl testset3
(integer) 1
127.0.0.1:6379> ttl testset3
(integer) -2
127.0.0.1:6379> keys testset*
1) "testset4"
2) "testset2"
3) "testset1"
persist
移除給定key 的過期時間,取消過期
127.0.0.1:6379> expire age 100
(integer) 1
127.0.0.1:6379> ttl age
(integer) 94
127.0.0.1:6379> ttl age
(integer) 90
127.0.0.1:6379> persist age
(integer) 1
127.0.0.1:6379> ttl age
(integer) -1
127.0.0.1:6379> get age
"15"
select
選擇資料庫(redis預設16個數據庫 0~15)
# vim redis.conf
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16
127.0.0.1:6379> select 0
OK
move
將當前資料庫中的key 轉移到其它資料庫中
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> set age 15
OK
127.0.0.1:6379> get age
"15"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get age
(nil)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> move age 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get age
"15"
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get age
(nil)
randomkey
隨機返回一個key
127.0.0.1:6379> keys *
1) "testlist2"
2) "testset4"
3) "testset2"
4) "mytest"
5) "num2"
6) "testset1"
7) "num3"
8) "age1"
9) "num1"
10) "name"
127.0.0.1:6379> randomkey
"num3"
rename
重新命名key
127.0.0.1:6379> keys *
1) "testlist2"
2) "testset4"
3) "testset2"
4) "mytest"
5) "num2"
6) "testset1"
7) "num3"
8) "age1"
9) "num1"
10) "name"
127.0.0.1:6379> rename name name_new
OK
127.0.0.1:6379> keys name*
1) "name_new"
type
返回值的型別
127.0.0.1:6379> keys testset*
1) "testset4"
2) "testset2"
3) "testset1"
127.0.0.1:6379> type testset1
set
127.0.0.1:6379> keys name*
1) "name_new"
127.0.0.1:6379> type name
none
127.0.0.1:6379> keys num*
1) "num2"
2) "num3"
3) "num1"
127.0.0.1:6379> type num1
string
ping
測試連線是否存活
127.0.0.1:6379> ping
PONG
echo
在命令列輸出內容
127.0.0.1:6379> echo "Hello world!"
"Hello world!"
info
獲取伺服器的資訊
127.0.0.1:6379> info
monitor
實時轉儲收到的請求
127.0.0.1:6379> monitor
OK
1543934476.796187 [0 127.0.0.1:52841] "info"
1543934614.406197 [0 127.0.0.1:52841] "config" "get"
1543934632.306195 [0 127.0.0.1:52841] "config" "get" "dir"
1543934662.761337 [0 127.0.0.1:52841] "config" "get" "*"
config get
獲取伺服器配置資訊。
129.127.0.0.1:6379> config get *
quit / shutdown
退出連線。