Start the Redis service in Linux.

Database operations

Switching databases

In Redis, there are 16 databases by default. You can view the field DATABASE 16 in the Redis. Conf file.

127.0.0.1> select [database ID]Copy the code

Deletes all key-value pairs of the current library

127.0.0.1 > FLUSHDBCopy the code

Delete all key-value pairs from all libraries

127.0.0.1 > FLUSHALLCopy the code

The key operation

Viewing the TTL of the key (Time to Live)

127.0.0.1 > TTL/keynameCopy the code

If the command output is -1, the key never expires. If the result is -2, the key has expired. Other results indicate the remaining lifetime of the key. Set the expiration time of the key

127.0.0.1> EXPIRE [keyname] [EXPIRE time /s]Copy the code

Query all keys of the current library

127.0.0.1 > keys *Copy the code

View the value data type of the specified key

127.0.0.1 > type [keyname]Copy the code

Operation of different data types

String

# add/update 127.0.0.1> set [keyname] [value] # delete 127.0.0.1> del [keyname] # add/update 127.0.0.1> set [keyname] [value] # delete 127.0.0.1> del [keyname Set k1 123 OK 127.0.0.1> append k1 456 OK 127.0.0.1> get k1 "123456" # query string length 127.0.0.1> strlen k1 (Integer) 1 OK 127.0.0.1> INCR k2 (Integer) 2 127.0.0.1> INCR k2 (Integer) 3 127.0.0.1> INCR 127.0.0.1> set k3 5 OK 127.0.0.1> DECR k3 (Integer) 4 127.0.0.1> DECR k3 (Integer) 3 127.0.0.1> DECR k3 (Integer) 2 # Add 127.0.0.1> INCRBY k3 3 (Integer) 5 # subtract 127.0.0.1> DECRBY k3 3 (Integer) 2 # Range query substring 127.0.0.1> SET K4 ABCD123 127.0.0.1> GETRANGE K4 0 5 "ABCD1" # 127.0.0.1> SETRANGE K4 0 XXX "XXXD123" 127.0.0.1> SETEX K4 10 V4 OK SETNX(SET IF NO EXISTS) 127.0.0.1> SETNX k1 ABC (Integer) 0 # 0 indicates that update fails 127.0.0.1> SETNX k6 ABC (Integer) 1 MGET k10 k11 k12 1) "v10" 2) "v11" 3) "v12" 127.0.0.1> MSETNX k13 v13 k14 v14 k15 v15 OK # If there is an existing key, then this operation failsCopy the code

List

127.0.0.1> RPUSH list2 12 3 4 5 # first in, first out 127.0.0.1> LRANGE list1 0-1 1) "5" 2) "4" 3) "3" 4) "2" 5) "1" 1" 127.0.0.1> LRANGE List2 0-1 1) "1" 2) "2" 127.0.0.1> LPOP list1 "5" 127.0.0.1> LPOP list2 "1" 127.0.0.1> RPOP list1 "1" 127.0.0.1> RPOP list2 # access 127.0.0.1> LINDEX list1 2 "3" 127.0.0.1> LINDEX list1 4 "1" 127.0.0.1> LINDEX list2 2 "3" 127.0.0.1> LINDEX list2 4 "5" # Query string length 127.0.0.1> LLEN list2 "5" # delete N M 127.0.0.1> LREM list1 N MCopy the code

hash

set

sorted set