目錄
- 1.Key操作命令
- 1.1 `keys *`
- 1.2 `exists <key]>`
- 1.3 `type <key>`
- 1.4 `del <key>`
- 1.5 `unlink <key>`
- 1.6 `ttl <key>`
- 1.7 `expire <key> <秒數>`
- 1.8 `move <key> <index>`
- 2.庫操作命令
- 2.1 `select <index>`
- 2.2 `dbsize`
- 2.3 `flushdb`
- 2.4 `flushall`
- 3.其他命令
- 3.1 `help @<type>`
1.Key操作命令
Redis是Key-Value數據庫,Key都是字符串且區分大小寫,關于Redis的key操作,主要有常見的以下幾個
Redis的命令是不區分大小寫的
1.1 keys *
查看當前庫所有的Key,類似于數據庫的select * from tb_xxx
127.0.0.1:6379> keys *
1) "k1"
2) "k2"
1.2 exists <key]>
Key是否存在,返回bool,1代表true,0代表false
127.0.0.1:6379> exists k1
(integer) 1
127.0.0.1:6379> exists k2
(integer) 1
127.0.0.1:6379> exists k3
(integer) 0
Redis的底層使用C語言實現,很多命令返回bool時,多用0和1表示
1.3 type <key>
key對應的value是什么類型
127.0.0.1:6379> type k1
string
1.4 del <key>
刪除數據,返回bool
127.0.0.1:6379> del k2
(integer) 1
1.5 unlink <key>
非阻塞刪除,僅僅將key從keyspace元數據中刪除,真正的數據刪除將在后續異步進行,返回bool
127.0.0.1:6379> unlink k1
(integer) 1
1.6 ttl <key>
查看key還有多少秒過期,-1代表永不過期,-2代表已過期,通常和expire
命令搭配使用
127.0.0.1:6379> ttl k1
(integer) -1
1.7 expire <key> <秒數>
為指定的key設置過期時間
127.0.0.1:6379> expire k1 100
(integer) 1
127.0.0.1:6379> ttl k1
(integer) 90
127.0.0.1:6379> ttl k1
(integer) 86
1.8 move <key> <index>
將當前key移動到指定的數據庫中,返回bool
127.0.0.1:6379> move k1 2
(integer) 1
2.庫操作命令
2.1 select <index>
選中幾號倉庫。redis.conf配置文件默認Redis共16個數據庫(0-15),默認選中0號庫
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> select 3
OK
127.0.0.1:6379[3]>
2.2 dbsize
查看當前庫有多少key
127.0.0.1:6379[3]> dbsize
(integer) 0
127.0.0.1:6379[3]> set k1 v1
OK
127.0.0.1:6379[3]> dbsize
(integer) 1
127.0.0.1:6379[3]>
2.3 flushdb
清空當前庫中的所有key
127.0.0.1:6379> flushdb
OK
2.4 flushall
清空整個Redis中的所有key
127.0.0.1:6379> flushall
OK
3.其他命令
3.1 help @<type>
命令行下輸入help @<type>
命令,redis服務器會返回該數據類型的所有用法
127.0.0.1:6379> help @stringAPPEND key value
summary: Appends a string to the value of a key. Creates the key if it doesn't exist.
since: 2.0.0DECR key
summary: Decrements the integer value of a key by one. Uses 0 as initial value if the key doesn't exist.
since: 1.0.0DECRBY key decrement
summary: Decrements a number from the integer value of a key. Uses 0 as initial value if the key doesn't exist.
since: 1.0.0GET key
summary: Returns the string value of a key.
since: 1.0.0GETDEL key
summary: Returns the string value of a key after deleting the key.
since: 6.2.0GETEX key [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|PERSIST]
summary: Returns the string value of a key after setting its expiration time.
since: 6.2.0GETRANGE key start end
summary: Returns a substring of the string stored at a key.
since: 2.4.0GETSET key value
summary: Returns the previous string value of a key after setting it to a new value.
since: 1.0.0INCR key
summary: Increments the integer value of a key by one. Uses 0 as initial value if the key doesn't exist.
since: 1.0.0INCRBY key increment
summary: Increments the integer value of a key by a number. Uses 0 as initial value if the key doesn't exist.
since: 1.0.0INCRBYFLOAT key increment
summary: Increment the floating point value of a key by a number. Uses 0 as initial value if the key doesn't exist.
since: 2.6.0LCS key1 key2 [LEN] [IDX] [MINMATCHLEN min-match-len] [WITHMATCHLEN]
summary: Finds the longest common substring.
since: 7.0.0MGET key [key ...]
summary: Atomically returns the string values of one or more keys.
since: 1.0.0MSET key value [key value ...]
summary: Atomically creates or modifies the string values of one or more keys.
since: 1.0.1MSETNX key value [key value ...]
summary: Atomically modifies the string values of one or more keys only when all keys don't exist.
since: 1.0.1PSETEX key milliseconds value
summary: Sets both string value and expiration time in milliseconds of a key. The key is created if it doesn't exist.
since: 2.6.0SET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL]
summary: Sets the string value of a key, ignoring its type. The key is created if it doesn't exist.
since: 1.0.0SETEX key seconds value
summary: Sets the string value and expiration time of a key. Creates the key if it doesn't exist.
since: 2.0.0SETNX key value
summary: Set the string value of a key only when the key doesn't exist.
since: 1.0.0SETRANGE key offset value
summary: Overwrites a part of a string value with another by an offset. Creates the key if it doesn't exist.
since: 2.2.0STRLEN key
summary: Returns the length of a string value.
since: 2.2.0SUBSTR key start end
summary: Returns a substring from a string value.
since: 1.0.0127.0.0.1:6379>