Redis provides a rich command to manipulate databases and various data types, which can be used on Linux endpoints. In programming, such as various language packs, these commands have a corresponding method. The following is a summary of the commands provided by Redis.
Key-Value Related commands
1. Keys
Returns all keys that satisfy the given pattern:
Redis 127.0.0.1:6379> Keys *
1) "Myzset2"
2) "Myzset3"
3) "MyList"
4) "Myset2"
5) "Myset3"
6) "Myset4"
7) "K_zs_1"
8) "Myset5"
9) "Myset6"
"Myset7"
One) "Myhash"
"Myzset"
"Age"
"MySet"
() "Mylist5"
() "Mylist6"
) "Mylist7"
) "Mylist8"
With the expression *, the delegate takes out all keys:
Redis 127.0.0.1:6379> Keys mylist*
1) "MyList"
2) "Mylist5"
3) "Mylist6"
4) "Mylist7"
5) "Mylist8"
Redis 127.0.0.1:6379>
With the expression mylist*, the delegate takes out all keys starting with MyList.
2, exists
Verify that a key exists:
Redis 127.0.0.1:6379> exists Hongwan
(integer) 0
Redis 127.0.0.1:6379> exists age
(integer) 1
Redis 127.0.0.1:6379>
The Hongwan key does not exist in the database from the result, but the age key is present.
3, Del
Delete a key:
Redis 127.0.0.1:6379> del Age
(integer) 1
Redis 127.0.0.1:6379> exists age
(integer) 0
Redis 127.0.0.1:6379>
The Hongwan key does not exist in the database from the result, but the age key is present.
4, expire
Set the expiration time (in seconds) of a key:
Redis 127.0.0.1:6379> expire Addr 10
(integer) 1
Redis 127.0.0.1:6379> TTL addr
(integer) 8
Redis 127.0.0.1:6379> TTL addr
(integer) 1
Redis 127.0.0.1:6379> TTL addr
(integer)-1
Redis 127.0.0.1:6379>
In this example, we set the expiration time for the key addr to 10 seconds, and then we constantly use the TTL to get the valid duration of the key until 1 indicates that the value has expired.
5. Move
Transfer the key from the current database to a different database:
Redis 127.0.0.1:6379> Select 0
Ok
Redis 127.0.0.1:6379> Set Age 30
Ok
Redis 127.0.0.1:6379> Get Age
"30"
Redis 127.0.0.1:6379> Move Age 1
(integer) 1
Redis 127.0.0.1:6379> Get Age
(nil)
Redis 127.0.0.1:6379> Select 1
Ok
Redis 127.0.0.1:6379[1]> Get Age
"30"
Redis 127.0.0.1:6379[1]>
In this example, I first explicitly selected database 0, and then set a key in this library, then we move the key from database 0 to database 1, then we confirm that there is no such key in database 0, but in database 1 there is this key, indicating that we transferred successfully
6, persist
To remove the expiration time for a given key:
Redis 127.0.0.1:6379[1]> expire Age 300
(integer) 1
Redis 127.0.0.1:6379[1]> TTL age
(integer) 294
Redis 127.0.0.1:6379[1]> persist Age
(integer) 1
Redis 127.0.0.1:6379[1]> TTL age
(integer)-1
Redis 127.0.0.1:6379[1]>
In this example, we manually set the key that was not the expiration time to expire successfully.
7, Randomkey
Randomly returns a key in the key space:
Redis 127.0.0.1:6379> Randomkey
"Mylist7"
Redis 127.0.0.1:6379> Randomkey
"Mylist5"
Redis 127.0.0.1:6379>
The result shows that the rule of key is random.
8, rename
Rename key:
Redis 127.0.0.1:6379[1]> Keys *
1) "Age"
Redis 127.0.0.1:6379[1]> Rename Age age_new
Ok
Redis 127.0.0.1:6379[1]> Keys *
1) "Age_new"
Redis 127.0.0.1:6379[1]>
Age succeeded in renaming us to Age_new.
9. Type
Type of return value:
Redis 127.0.0.1:6379> Type Addr
String
Redis 127.0.0.1:6379> Type Myzset2
Zset
Redis 127.0.0.1:6379> Type MyList
List
Redis 127.0.0.1:6379>
This method can be very simple to determine the value of the type.
Redis Common Command Manual: key-Value related commands