“This is the third day of my participation in the August More Text Challenge.
1, Redis Key basic command
The Redis Key command is used to manage Redis keys.
grammar
The basic syntax of the Redis key command is as follows:
Redis 127.0.0.1:6379 > COMMAND KEY_NAMECopy the code
Redis Key use cases:
127.0.0.1:6379 >setThe name wanli OK 127.0.0.1:6379 >set pwd 123
OK
127.0.0.1:6379> keys *
1) "name"
2) "pwd"
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to judge whether there is a key -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 > exists the nameIf there is a key,
(integer1)# existence is 1,127.0.0.1:6379 > exists name1 (integer) 0 # does not exist is 0
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the specified key moves to specify the database -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 > move the name 1Select * from database 1 where key = 'name'
(integer) 1
127.0.0.1:6379> keys *
1) "pwd"127.0.0.1:6379 > select 1Switch to database 1OK 127.0.0.1:6379 [1] > keys *# Key successfully moved
1) "name"127.0.0.1:6379 [1] > flushdbClear database 1
OK
127.0.0.1:6379[1]> keys *
(empty array)
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- of the specified key expiration time -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 > expirepwd 15 Key PWD expires after 15s
(integer127.0.0.1) 1:6379 > TTLpwd Check how much time is left to expire
(integer127.0.0.1), 11:6379 > TTLpwd
(integer127.0.0.1) 2:6379 > TTLpwd
(integer2) -#key has expired127.0.0.1:6379 > getpwd
(nil)
127.0.0.1:6379> keys *
(empty array)
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- check the key type -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 >type pwd
string
Copy the code
For more commonly used Redis Key commands, see the following table: Redis. IO /commands
The serial number | Commands and Description |
---|---|
1 | The DEL key command is used to delete a key when it exists. |
2 | The DUMP key serializes the given key and returns the serialized value. |
3 | EXISTS Key Checks whether a given key EXISTS. |
4 | EXPIRE Key seconds Sets the expiration time, in seconds, for a given key. |
5 | EXPIREAT Key timestamp Similar to EXPIRE, EXPIREAT is used to set the expiration time for the key. The difference is that the EXPIREAT command accepts a UNIX timestamp as the time parameter. |
6 | PEXPIRE key milliseconds Sets the expiration time of the key in milliseconds. |
7 | PEXPIREAT key snaps-timestamp Sets the key expiration time timestamp (Unix timestamp) in milliseconds |
8 | KEYS Pattern Looks for all KEYS that match the given pattern. |
9 | MOVE Key DB Moves the key of the current database to the specified database db. |
10 | PERSIST Key Removes the expiration time of a key, and the key persists. |
11 | PTTL key Returns the remaining expiration time of the key in milliseconds. |
12 | TTL key Returns the TTL (time to live) of a given key in seconds. |
13 | RANDOMKEY returns a RANDOMKEY from the current database. |
14 | RENAME Key newKey Changes the name of a key |
15 | RENAMENX key NewKey Rename the key to newkey only when newkey does not exist. |
16 | SCAN CURSOR MATCH Pattern] [COUNT COUNT Iterates database keys in the database. |
17 | TYPE Key Returns the TYPE of the value stored in the key. |
Refer to the novice tutorial: www.runoob.com/redis/redis…
2. Redis is a String
Redis string datatype commands are used to manage Redis string values. The basic syntax is as follows:
Grammar:
Redis 127.0.0.1:6379 > COMMAND KEY_NAMECopy the code
Example:
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - key - value access -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 >setKey1 wanli OK 127.0.0.1:6379> get key1"wanli"
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to the specified value is appended to the key value (value) at the end of the original -- -- -- -- -- -- -- -- -- -- -- -- --
127.0.0.1:6379> append key1 hello
(integer) 10
127.0.0.1:6379> get key1
"wanlihello"127.0.0.1:6379 > append key1"hi"
(integer) 12
127.0.0.1:6379> get key1
"wanlihellohi"127.0.0.1:6379 > append key3 taoxianIf the specified key does not exist, the specified key is created and assigned to value
(integer) 7
127.0.0.1:6379> get key3
"taoxian"
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- determine the key whether there is -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 >setKey2 shenming OK 127.0.0.1:6379> exists key1 key2 (integer2)# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for the specified key length -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 > strlen key1 (integer12)# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the number stored in the key operation -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 >setView 10 OK 127.0.0.1:6379> get view"10"127.0.0.1:6379 > incr view# reduce the number stored in the key by one
(integer) 11
127.0.0.1:6379> incr view
(integer) 12
127.0.0.1:6379> get view
"12"127.0.0.1:6379 > decr view# reduce the number stored in the key by one
(integer) 11
127.0.0.1:6379> decr view
(integer) 10
127.0.0.1:6379> get view
"10"
127.0.0.1:6379>
127.0.0.1:6379> incrby view 5 Add the value stored in key to the given increment value
(integer) 15
127.0.0.1:6379> decrby view 5 Subtract the given decrement value from the value stored in key
(integer6379 > 10 127.0.0.1) :# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- check the string range -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 >setKey1 wanliguyichegn OK 127.0.0.1:6379> GETRANGE key1 0 4# see [0, 4]
"wanli"127.0.0.1:6379> GETRANGE key1 0-1Get key ()
"wanliguyichegn"
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the replacement string content -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 >setKey2 abcde OK 127.0.0.1:6379> getrange key2 0-1"abcde"
127.0.0.1:6379> setrange key2 2 ** # Replaces the content after the second character
(integer) 5
127.0.0.1:6379> get key2
"ab**e"127.0.0.1:6379> setrange key2 2 $$$$$$$$(integer) 10
127.0.0.1:6379> get key2
"ab$$$$$$$$"
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- setex (set with) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 > keys * 1)"key1"
2) "key2"
127.0.0.1:6379> setex key3 10 hello # associate the value value with the key and set the expiration time of the key to 10s
OK
127.0.0.1:6379> ttl key3
(integer) 5
127.0.0.1:6379> keys *
1) "key1"
2) "key2"
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- setnx (set if not exist) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 > keys *All keys in the current database
1) "key1"
2) "key2"127.0.0.1:6379 > setnx key3 hiSet the value of the key only if the key does not exist.
(integer1)If # is 1, the setting is successful127.0.0.1:6379 > keys * 1)"key3"
2) "key1"
3) "key2"
127.0.0.1:6379> setnx key3 hi
(integer) 0 If # is 0, the setting fails
127.0.0.1:6379> setnx key3 hello
(integer) 0
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - batch access key - value -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 # Batch setup
OK
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
127.0.0.1:6379> mget k1 k2 k3 # Batch take out
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx k1 v1 k4 v4 # create it when it doesn't exist
(integer) 0 # failed because msetnx is an atomic operation, so either they all succeed or they all fail127.0.0.1:6379 > keys * 1)"k3"
2) "k2"
3) "k1"127.0.0.1:6379 ># -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- combined command getset -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --127.0.0.1:6379 > getset k4 helloGet set = nil(nil) 127.0.0.1:6379 > get k4# set the value of success
"hello"127.0.0.1:6379 > getset k4 hiGet the original value and replace it with the new value
"hello"127.0.0.1:6379 > get k4# replace succeeded
"hi"
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- object in the form of a key command -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
127.0.0.1:6379> mset user:1:name wanli user:1:age 3
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "wanli"
2) "3"
Copy the code
For more commands, see redis. IO /commands