Common commands

keys

Returns all keys that satisfy the given pattern

Redis 127.0.0.1:6379> keys myList * 1)"mylist"
2) "mylist5"
3) "mylist6"
4) "mylist7"
5) "mylist8"

Copy the code

exists

Check whether a key exists example: HongWan does not exist in the database, but age does exist

Redis 127.0.0.1:6379> exists HongWan (integer) 0
redis 127.0.0.1:6379> exists age
(integer1) redis 127.0.0.1:6379 >Copy the code

del

Delete a key

Redis 127.0.0.1:6379> del age (integer) 1
redis 127.0.0.1:6379> exists age
(integer) 0

Copy the code

rename

Rename key example: age was successfully renamed to age_new

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] >Copy the code

type

Example of type of return value: This method makes it very easy to determine the type of value

Redis 127.0.0.1:6379 >typeAddr string redis 127.0.0.1:6379 >typeMyzset2 zset redis 127.0.0.1:6379 >typeMylist list redis 127.0.0.1:6379 >Copy the code

Set the lifetime of the key

In actual use, Redis is more used as a cache. However, the cached data generally needs to set the survival time, that is, the data will be destroyed after expiration.

EXPIRE Key seconds Sets the lifetime of the key (unit: TTL indicates the TTL after the key is automatically deleted. Check the TTL of the key. PERSIST Key clearance duration PEXPIRE key millisecondsCopy the code

Example:

192.168.101.3:7002 >set test1 settest192.168.101.3:7002> gettestTo obtaintestThe value of the"1"192.168.101.3:7002 > EXPIREtest5 settestSurvival time of 5 seconds (integer) 1 192.168.101.3:7002 > TTLtestTo viewtestThe generation time of the birth is 1 second. Delete (integer) 1 192.168.101.3:7002 > TTLtest
(integer) - 2 192.168.101.3:7002 > gettestTo obtaintestThe value of, has been deleted (nil)Copy the code

The next article covers the Redis persistence scheme