Redis cli basic operation

catalogue

1, Connect redis cli

2, Switch database (SELECT)

3, String string (can be integer, floating point and string, collectively referred to as element)

Common commands of type string:

1. Insert / read a single message

2. Insert / read multiple messages

4, Hash hash value (the key of hash must be unique)

Common commands supported by hash data types:

1. Insert / read a single message

2. Insert / read multiple messages

3. Delete

5, List (implement queue, elements are not unique, first in first out principle)

Common commands supported by list type:

1. Left insert / left delete

2. Right insert / right delete

3. Traversal

6, Set set (different elements)

Common commands supported by set type:

7, Sorted Set   Ordered set

sort set is similar to hash. It is also a mapped storage:

8, General deletion

9, Clean up the current library

10, Clean up all libraries

11, Failure time

  1. Query expiration time

2. Set the expiration time

1, Connect redis cli

Use the Redis cli client to connect to the Redis service.

[root@master redis]#  bin/redis-cli -h 192.168.182.100 -p 6379 -a 123456

  • -H: IP address
  • -p: Port
  • -a: password

2, Switch database (SELECT)

SELECT 0~15            # The default is in Library 0

3, String string (can be integer, floating point and string, collectively referred to as element)

Common commands of type string:

  • Self adding: incr
  • Self subtraction: decr
  • Add: incrby
  • Less: decrby

1. Insert / read a single message

SET key value [expiration EX seconds|PX milliseconds] [NX|XX]    # insert

GET ke   # read

192.168.182.100:6379> set username zhangsan
OK
192.168.182.100:6379> get username
"zhangsan"

2. Insert / read multiple messages

MSET key value [key value ...]     # Insert multiple

MGET key [key ...]   # Read multiple

192.168.182.100:6379> mset age 20 address sh 
OK
192.168.182.100:6379> mget age address
1) "20"
2) "sh"

4, Hash hash value (the key of hash must be unique)

Common commands supported by hash data types:

  • hset: adding hash data

  • hget: get hash data

  • hmget: get multiple hash data

1. Insert / read a single message

HSET key field value   # insert

HGET key fiel   # read

192.168.182.100:6379> hset user username zhangsan 
(integer) 1
192.168.182.100:6379> hget user username
"zhangsan"

2. Insert / read multiple messages

HMSET key field value [field value ...]   # Insert multiple

HMGET key field [field ...]    # Read multiple

192.168.182.100:6379> hmset user age 18 address sh
OK
192.168.182.100:6379> hmget user age address
1) "18"
2) "sh"

3. Delete

HDEL key field [field ...]   # delete

192.168.182.100:6379> hdel user address
(integer) 1

5, List (implement queue, elements are not unique, first in first out principle)

Common commands supported by list type:

  • lpush: push from left

  • lpop: pop from left

  • rpush: change from right to push

  • rpop: pop from right

  • Len: view the length of a list data type
     

1. Left insert / left delete

LPUSH key value [value ...]    # Left insert, LPUSH(Left Push)

LPOP key   # Left delete, LPOP(Left Pop)

192.168.182.100:6379> lpush stu zhangsan lisi wangwu
(integer) 3
192.168.182.100:6379> lpop stu 
"wangwu"
192.168.182.100:6379> lpop stu
"lisi"
192.168.182.100:6379> lpop stu
"zhangsan"

2. Right insert / right delete

RPUSH key value [value ...]   # Right insert, RPUSH(Right Push)

RPOP key   # Right delete, RPOP(Right Pop)

192.168.182.100:6379> rpush stu zhangsan lisi wangwu
(integer) 3
192.168.182.100:6379> rpop stu
"wangwu"
192.168.182.100:6379> rpop stu
"lisi"
192.168.182.100:6379> rpop stu
"zhangsan"

3. Traversal

LRANGE key start stop   #  Left closed right closed

192.168.182.100:6379> lrange stu 0 2
1) "wangwu"
2) "lisi"
3) "zhangsan"

6, Set set (different elements)

Common commands supported by set type:

  • sadd: add data

  • scard: view the number of elements in the set data

  • sismember: judge whether an element exists in the set data

  • srem: delete elements in a set data

#Insert the collection (1 is returned if the element is added successfully, 0 is returned if the element exists)

SADD key member [member ...] 

#Judge whether the value is in the Set represented by the key (1 if the element exists, 0 if the element or key does not exist)

SMEMBERS key

#   Judge whether an element exists in the set data (1 is returned if an element is deleted successfully, and 0 is returned if deletion fails)

SREM key member [member ...]

192.168.182.100:6379[3]> SADD letters a b c d e
(integer) 5
192.168.182.100:6379[3]> SMEMBERS letters
1) "b"
2) "d"
3) "c"
4) "a"
5) "e"
192.168.182.100:6379[3]> SREM letters a
(integer) 1

7, Sorted Set   Ordered set

sort set is similar to hash. It is also a mapped storage:

  • zadd: add

  • zcard: query

  • zrange: data sorting

192.168.182.100:6379[4]> ZADD stu 98 zhangsan 95 lisi 90 wangwu 100 zhaoliu 93 tianqi
(integer) 5
192.168.182.100:6379[4]> ZRANGE stu 0 4
1) "wangwu"
2) "tianqi"
3) "lisi"
4) "zhangsan"
5) "zhaoliu"
192.168.182.100:6379[4]> ZREVRANGE stu 0 4
1) "zhaoliu"
2) "zhangsan"
3) "lisi"
4) "tianqi"
5) "wangwu"
192.168.182.100:6379[4]> ZREM stu zhangsan
(integer) 1

8, General deletion

DEL key [key ...]    # delete

192.168.182.100:6379> del username
(integer) 1

9, Clean up the current library

FLUSHDB

10, Clean up all libraries

FLUSHALL

11, Failure time

  1. Query expiration time

PTTL key, in milliseconds

TTL key, in seconds

192.168.182.100:6379> TTL lock
(integer) -1
  • -1: Never fail
  • -2: Invalid
  • Normal time > 0 number: remaining failure time

2. Set the expiration time

PEXPIRE key milliseconds   # millisecond

EXPIRE key seconds   # second

Method 1:

192.168.182.100:6379> PEXPIRE lock 10000   # Set the lock expiration time to 10000 milliseconds
(integer) 1

  Method 2:

192.168.182.100:6379> set code test EX 100     # Set the expiration time to 100 seconds
OK
192.168.182.100:6379> ttl code
(integer) 167
192.168.182.100:6379> ttl code
(integer) 165

...

Tags: Database Redis

Posted on Mon, 04 Oct 2021 19:30:11 -0400 by Candrew