Getting started with redis 1. Basic operations of five data structures (2)

Getting started with redis 1. Basic operations of five data structures [2]


Database connection command:
[Default connection to library No. 6379 0]: redis-cli
[Specify the redis service to connect to port 6380]: redis-cli -p 6380
[Specify the redis service to connect to library No. 8 on port 6380]: redis-cli -p 6380-n 8

After logging in,
enter help @generic(it will automatically complete after @) commonly used:
del ,exists,expire,keys ,move,object,persist,pexpire,type
eg:
check what keys are in the library: keys *
clear all keys in the library (operate with caution):flushdb / flushall

1. String

1)、set

Assign a value to a string: set k1 hello
Get a value:get k1

Query the usage of set command: help set
Insert image description here
According to the usage, you can know the following picture

Function of nx: The
assignment can only be successful when k1 is newly created and has no value. If k1 already has a value, k1 cannot be assigned a value. [Applicable scenario: distributed lock, many people, whoever can assign the value successfully will seize the lock]
Insert image description here
The role of xx: The update
can only be successful when k1 has a value.
Insert image description here

2)、getset

Send a get and then a set. Using this command is equivalent to sending only one package and reducing IO requests:getset k1 mm
Insert image description here

3), msetnx assigns values ​​to multiple elements, atomic operations

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty list or set)

Assign values ​​to k1 and k2

127.0.0.1:6379> msetnx k1 a k2 b
(integer) 1
127.0.0.1:6379> mget k1 k2
1) "a"
2) "b"

Update the value of k2 and assign a value to k3. nx can only successfully assign values ​​to elements without values. The following operation fails. msetnx is an atomic operation, so the assignment of k3 fails.

127.0.0.1:6379> msetnx k2 c k3 d
(integer) 0
127.0.0.1:6379> mget k1 k2 k3
1) "a"
2) "b"
3) (nil)

4), append string & get partial number

Guess you like

Origin blog.csdn.net/qq_17033579/article/details/129588249