当前位置:网站首页>Redis getting started complete tutorial: Key Management

Redis getting started complete tutorial: Key Management

2022-07-04 23:00:00 Gu Ge academic

2.7.1  Single key management
Commands for a single key , Some of them have been introduced in the previous sections , for example type、del、
object、exists、expire etc. , Here are the remaining important commands .
1. The key to rename
rename key newkey
For example, there is an existing key value pair , The key is python, The value is jedis:
127.0.0.1:6379> get python
"jedis"
The following operation will change the key python Rename it to java:
127.0.0.1:6379> set python jedis
OK
127.0.0.1:6379> rename python java
OK
127.0.0.1:6379> get python
(nil)
127.0.0.1:6379> get java
"jedis"
If in rename Before , key java Already exist , Then its value will also be overridden , As follows
in :
127.0.0.1:6379> set a b
OK
127.0.0.1:6379> set c d
OK
127.0.0.1:6379> rename a c
OK
127.0.0.1:6379> get a
(nil)
127.0.0.1:6379> get c
"b"

To prevent being forced rename,Redis Provides renamenx command , Make sure that only newKey
Only when it doesn't exist , For example, the following operation renamenx when ,newkey=python Already exist ,
The return is 0 Delegate did not complete the rename , So the key java and python The value of the unchanged :
127.0.0.1:6379> set java jedis
OK
127.0.0.1:6379> set python redis-py
OK
127.0.0.1:6379> renamenx java python
(integer) 0
127.0.0.1:6379> get java
"jedis"
127.0.0.1:6379> get python
"redis-py"
When using the rename command , There are two points to note :
· Because during the rename key del Command delete old key , If the value corresponding to the key is compared
Big , There will be blocking Redis The possibility of , Don't ignore that .
· If rename and renamenx Medium key and newkey If it's the same , stay Redis3.2 And
The previous version returned slightly different results .
Redis3.2 Will return OK:
127.0.0.1:6379> rename key key
OK
Redis3.2 Previous versions have errors :
127.0.0.1:6379> rename key key
(error) ERR source and destination objects are the same
2. Returns a key at random

randomkey
In the following example , The current database has 1000 Key value pairs ,randomkey Commands will be randomly selected
Pick a key :
127.0.0.1:6379> dbsize
1000
127.0.0.1:6379> randomkey
"hello"
127.0.0.1:6379> randomkey
"jedis"
3. Key expired
2.1 Section briefly introduces the key expiration function , It can automatically delete keys with expiration time , stay
Many application scenarios are very helpful . except expire、ttl Beyond the command ,Redis It also provides
expireat、pexpire、pexpireat、pttl、persist Wait for a series of orders , Let's talk about it separately
bright :
·expire key seconds: The key in seconds Seconds after expired .
·expireat key timestamp: The key is timestamped in seconds timestamp After expired .
Here are the keys hello Set up 10 Seconds past due , And then through ttl Observe its expiration remaining
between ( Company : second ), as time goes on ,ttl Gradually smaller , It turns out to be -2:
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> expire hello 10
(integer) 1
# And then there were 7 second
127.0.0.1:6379> ttl hello
(integer) 7
...
# And then there were 0 second
127.0.0.1:6379> ttl hello
(integer) 0
# The return result is -2 , Description Key hello It has been deleted
127.0.0.1:6379> ttl hello
(integer) -2

ttl Command and pttl You can query the remaining expiration time of the key , however pttl Higher accuracy can achieve
Millisecond level , Yes 3 A return value :
· Greater than or equal to 0 The integer of : Key remaining expiration time (ttl Is the second ,pttl Is ms ).
·-1: The key does not set an expiration time .
·-2: The key doesn't exist .
expireat The command sets the key's second expiration timestamp , For example, if you need to key hello stay
2016-08-0100:00:00( The second timestamp is zero 1469980800) Be overdue , You can perform the following operations
do :
127.0.0.1:6379> expireat hello 1469980800
(integer) 1
besides ,Redis2.6 A millisecond expiration scheme is provided after the release :
·pexpire key milliseconds: The key in milliseconds Expired in milliseconds .
·pexpireat key milliseconds-timestamp The key is timestamped in milliseconds timestamp after
period .
But either using an expiration time or a timestamp , Second or millisecond , stay Redis The most
The end use is pexpireat.
In the use of Redis When related expired commands , The following points need to be noted .
1) If expire key Is not there , The return result is 0:

127.0.0.1:6379> expire not_exist_key 30
(integer) 0
2) If the expiration time is negative , The key is immediately deleted , Like to use del command :
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> expire hello -2
(integer) 1
127.0.0.1:6379> get hello
(nil)
3)persist The command clears the key's expiration time :
127.0.0.1:6379> hset key f1 v1
(integer) 1
127.0.0.1:6379> expire key 50
(integer) 1
127.0.0.1:6379> ttl key
(integer) 46
127.0.0.1:6379> persist key
(integer) 1
127.0.0.1:6379> ttl key
(integer) -1
4) For string type keys , perform set The command will remove the expiration time , It's an easy question
Neglected in development .
The following is Redis Source code ,set The function of the command setKey, You can see the final execution
removeExpire(db,key) Function removes the expiration time :
void setKey(redisDb *db, robj *key, robj *val) {
if (lookupKeyWrite(db,key) == NULL) {
dbAdd(db,key,val);
} else {
dbOverwrite(db,key,val);
}
incrRefCount(val);
//   Remove the expiration time
removeExpire(db,key);
signalModifiedKey(db,key);
}

The following example confirms set Will cause the expiration time to lapse , because ttl Turn into -1:
127.0.0.1:6379> expire hello 50
(integer) 1
127.0.0.1:6379> ttl hello
(integer) 46
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> ttl hello
(integer) -1
5)Redis Secondary data structures are not supported ( Such as a hash 、 list ) Overdue work of internal elements
can , For example, you cannot set the expiration time on an element of a list type .
6)setex Command as set+expire The combination of , Not just atoms , At the same time, it reduces one
Network communication time .
of Redis Detailed principle of key expiration ,8.2 This section will analyze in depth .
4. The migration of key
The migration key function is very important , Because sometimes we just want to get part of the data from one Redis Move
Move to another Redis( For example, moving from a production environment to a test environment ),Redis In the course of development
For move、dump+restore、migrate Three ways to transfer bonds , How they are implemented and
The scenarios used are different , Here are the introduction .
(1)move
move key db
Pictured 2-26 Shown ,move The command is used in the Redis Internal data migration ,Redis The interior can
To have multiple databases , Due to multiple database functions, we will introduce them later , All we need to know here is
Redis There can be multiple databases inside , They're isolated from each other in terms of data ,move key db Just

Is to move the specified key from the source database to the target database , However, the author believes that the multi database function is not
Recommended for use in a production environment , So the reader can know this command .

(2)dump+restore
dump key
restore key ttl value
dump+restore Can be realized in different Redis The function of data migration between instances , whole
The migration process is divided into two steps :
1) In the source Redis On ,dump The command serializes the key values , The format is RDB Format .
2) In the target Redis On ,restore The restore command restores the values serialized above , among ttl ginseng
Number represents expiration time , If ttl=0 Represents no expiration time .
The whole process is as shown in the figure 2-27 Shown .

 

of dump+restore There are two points to note : First of all , The whole migration process is not atomic
Of , It's done step by step through the client . second , The migration process is to open two client connections
Pick up , therefore dump The result is not in the source Redis And the target Redis Transmission between , Now let's use one
Examples demonstrate the whole process .
1) In the source Redis On the implementation dump:
redis-source> set hello world
OK
redis-source> dump hello
"\x00\x05world\x06\x00\x8f<T\x04%\xfcNQ"
2) In the target Redis On the implementation restore:

redis-target> get hello
(nil)
redis-target> restore hello 0 "\x00\x05world\x06\x00\x8f<T\x04%\xfcNQ"
OK
redis-target> get hello
"world"
above 2 The pseudo code corresponding to step is as follows :
Redis sourceRedis = new Redis("sourceMachine", 6379);
Redis targetRedis = new Redis("targetMachine", 6379);
targetRedis.restore("hello", 0, sourceRedis.dump(key));
(3)migrate
migrate host port key|"" destination-db timeout [copy] [replace] [keys key [key ...]]
migrate Commands are also used in Redis Data migration between instances , actually migrate life
Order is to dump、restore、del Three commands combined , Thus, the operation process is simplified .
migrate Commands are atomic , And from Redis3.0.6 Migration of multiple keys has been supported since version
function , Effectively improve the migration efficiency ,migrate stay 10.4 Section plays an important role in horizontal expansion .
The whole process is as shown in the figure 2-28 Shown , The implementation process and dump+restore similar , But there are 3 spot
Not quite the same : First of all , The whole thing is done by atoms , No need in more than one Redis Instance
Client's , Just in the source Redis On the implementation migrate Command is enough . second ,migrate Ordered
The data is transmitted directly at the source Redis And the target Redis The complete . Third , The goal is Redis complete restore
Will be sent after OK To the source Redis, Source Redis After receiving will be based on migrate The corresponding options to determine whether
In the source Redis Delete the corresponding key on .

 

Following pair migrate The parameters of :
·host: The goal is Redis Of IP Address .
·port: The goal is Redis The port of .
·key|"": stay Redis3.0.6 Before the release ,migrate Only one key is supported for migration , So here is
The key to be migrated , but Redis3.0.6 Multiple key migrations are supported after release , If you currently need to migrate more
Key , Here is an empty string "".
·destination-db: The goal is Redis Database index , For example to migrate to 0 The database , this
157
Just write in the 0.
·timeout: Timeout for migration ( The unit is millisecond ).
·[copy]: If you add this option , The source key is not deleted after the migration .
·[replace]: If you add this option ,migrate Regardless of the target Redis Whether the key exists or not
Data coverage with normal migration .
·[keys key[key...]]: Migrate multiple keys , For example to migrate key1、key2、key3, Here to fill out
Write “keys key1 key2 key3”.
Here's an example migrate command , For the convenience of demonstrating the source Redis Use 6379 port , Objective
mark Redis Use 6380 port , Now let's put the source Redis Key hello Move to target Redis in , Will be divided into
There are several situations as follows :
situation 1: Source Redis There are keys hello, The goal is Redis No, :
127.0.0.1:6379> migrate 127.0.0.1 6380 hello 0 1000
OK
situation 2: Source Redis And the target Redis Have a key hello:
127.0.0.1:6379> get hello
"world"
127.0.0.1:6380> get hello
"redis"
If migrate The order didn't add replace Option will receive an error prompt , If you add replace Meeting
return OK Indicates that the migration was successful :
127.0.0.1:6379> migrate 127.0.0.1 6379 hello 0 1000
(error) ERR Target instance replied with error: BUSYKEY Target key name already exists.

127.0.0.1:6379> migrate 127.0.0.1 6379 hello 0 1000 replace
OK
situation 3: Source Redis There is no key hello. As shown below , This situation will receive nokey To mention
in :
127.0.0.1:6379> migrate 127.0.0.1 6380 hello 0 1000
NOKEY
Let's demonstrate Redis3.0.6 After the release, the function of migrating multiple keys .
· Source Redis Batch add multiple keys :
127.0.0.1:6379> mset key1 value1 key2 value2 key3 value3
OK
· Source Redis Execute the following command to complete the migration of multiple keys :
127.0.0.1:6379> migrate 127.0.0.1 6380 "" 0 5000 keys key1 key2 key3
OK
At this point about Redis The command of data migration is introduced , Last use table 2-9 To sum up
move、dump+restore、migrate The similarities and differences of the three migration ways , The author suggests using migrate
Command key value migration . 

2.7.2  Traversal keys
Redis Provides two commands to traverse all keys , Namely keys and scan, This section will describe them
Introduce and briefly analyze .
1. Full traversal key
keys pattern
At the beginning of this chapter keys Simple use of commands , actually keys The command is to support pattern matching
Of , For example, to an empty Redis Insert 4 A key value pair of string type .
127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379> mset hello world redis best jedis best hill high
OK
If you want to get all the keys , have access to keys pattern command :
127.0.0.1:6379> keys *
1) "hill"
2) "jedis"
3) "redis"
4) "hello"
Above to traverse all the keys ,pattern Use the asterisk directly , This is because pattern The use of
yes glob Style wildcard :
·* Represents matching any character .
· To match a character .

·[] Represents matching partial characters , for example [1,3] On behalf of the match 1,3,[1-10] On behalf of the match 1 To 10
Any number of .
·\x Used to escape , For example, to match the asterisk 、 The question mark needs to be escaped .
The following operations match to j,r start , Following the edis All the keys of the string :
127.0.0.1:6379> keys [j,r]edis
1) "jedis"
2) "redis"
For example, the following operation will match to hello and hill These two keys :
127.0.0.1:6379> keys hll*
1) "hill"
2) "hello"
When you need to traverse all the keys ( For example, detecting expired or idle time 、 Looking for big objects, etc ),
keys It's a very helpful order , For example, if you want to delete all of them to video The key at the beginning of the string , Sure
Do the following :
redis-cli keys video* | xargs redis-cli del
But if you think about Redis The single thread architecture is not so wonderful , If Redis Contains
Lots of keys , perform keys Orders are likely to result Redis Blocking , Therefore, it is generally recommended not to live
Used in production environment keys command . But sometimes there is a need to traverse the key , You can use
The following three cases are used :
· In a company that does not provide external services Redis Execute... From node , This will not block the client
Request , But it will affect master-slave replication , About master-slave replication, we will see 6 Chapter gives a detailed introduction
Shao .

· If you confirm that the total number of key values is really small , The command can be executed .
· Use the following scan The command traverses all keys incrementally , It can effectively prevent obstruction
stopper .
2. Progressive traversal
Redis from 2.8 After version , Provides a new command scan, It can effectively solve keys life
Make the existing problems . and keys When the command is executed, it traverses all the different keys ,scan Use progressive traversal
To solve the problem keys The possible blocking problem of command , Every time scan The time complexity of the command is
O(1), But to really achieve keys The function of , It needs to be done many times scan.Redis Store key value pairs
The actual use is hashtable Data structure of , The simplified model is shown in the figure 2-29 Shown .

 

  So every time you execute scan, Think of it as scanning only a few keys in a dictionary , Until will
All keys in the dictionary are traversed .scan Is used as follows :
scan cursor [match pattern] [count number]

·cursor Is the required parameter , actually cursor It's a cursor , The first traversal from 0 Start , Every time
Time scan Returns the value of the current cursor after the traversal , Until the value of the bid is 0, End of traversal .
·match pattern Is an optional parameter , What it does is it matches patterns , This and keys Of
Pattern matching is very much like .
·count number Is an optional parameter , What it does is it says the number of keys to traverse at a time , Default
The value is 10, This parameter can be appropriately increased .
There is one Redis Yes 26 Key ( english 26 Letters ), Now I want to traverse all the keys , send
use scan The operation of command effect is as follows . First execution scan0, The return result is divided into two parts : The first
A part 6 Next time scan Needed cursor, The second part is 10 Key :
127.0.0.1:6379> scan 0
1) "6"
2) 1) "w"
2) "i"
3) "e"
4) "x"
5) "j"
6) "q"
7) "y"
8) "u"
9) "b"
10) "o"
Use the new cursor="6", perform scan6:
127.0.0.1:6379> scan 6
1) "11"
2) 1) "h"
2) "n"
3) "m"
4) "t"
5) "c"
6) "d"
7) "g"
8) "p"
9) "z"
10) "a"

This time I got cursor="11", Carry on scan11 Get the results cursor Turn into 0, Description
Some keys have been traversed :
127.0.0.1:6379> scan 11
1) "0"
2) 1) "s"
2) "f"
3) "r"
4) "v"
5) "k"
6) "l"
except scan outside ,Redis Provides hash oriented types 、 Collection types 、 Sweep of ordered sets
Trace traversal command , Solve problems such as hgetall、smembers、zrange Possible blocking problems , Yes
The commands to be answered are hscan、sscan、zscan, Their usage and scan similar , Let's say
sscan Explain for example , There are two types of elements in the current collection , For example, use old:user
and new:user start , First, you need to put old:user Delete all the elements at the beginning , You can refer to the following
Pseudo code :
String key = "myset";
//   Definition pattern
String pattern = "old:user*";
//   The cursor starts from each time 0 Start
String cursor = "0";
while (true) {
//   Get scan results
ScanResult scanResult = redis.sscan(key, cursor, pattern);
List elements = scanResult.getResult();
if (elements != null && elements.size() > 0) {
//   Batch deletion
redis.srem(key, elements);
}
//   Get new cursor
cursor = scanResult.getStringCursor();
//   If the cursor is 0 End of traversal
if ("0".equals(cursor)) {
break;
}
}
Progressive traversal can effectively solve keys The possible blocking problem of command , however scan and
Not perfect , If in scan If there's a bond change in the process ( increase 、 Delete 、 modify ),

Then traversal effect may encounter the following problems : The new key may not traverse to , Traversal out of heavy
Complex bond, etc , in other words scan It's not guaranteed to traverse all the keys completely , These are
What we need to consider when developing .

2.7.3  Database management
Redis Several aspects are provided Redis Operation of database , They are dbsize、select、
flushdb/flushall command , This section will introduce these commands through specific use scenarios .
1. Switch database
select dbIndex
Many relational databases , for example MySQL Support the existence of multiple databases under one instance
Of , However, it is different from relational database in distinguishing different database names by characters ,Redis Just count
Word as the implementation of multiple databases .Redis There are... In the default configuration 16 A database :
databases 16
hypothesis databases=16,select0 The operation will switch to the first database ,select15 Choose the most
The latter database , however 0 Database and 15 There is no association between the data in the database , what
To the same key can exist :
127.0.0.1:6379> set hello world # Default in 0 The database
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> select 15 # Switch to 15 The database
OK
127.0.0.1:6379[15]> get hello # because 15 Database and 0 Database number is isolated , therefore get hello It's empty
(nil)
chart 2-30 More vividly show the above operation process . And you can see that , When using redis-
cli-h{ip}-p{port} Connect Redis when , The default is 0 The database , When selecting other data
library , There will be [index] Prefix ID of , among index Is the index subscript of the database .

So can it be like using test database and formal database , Put the official data in 0
The database , The test database is placed in 1 The database , Then the two will not suffer from each other in terms of data
Affected . Is it really that good ?
Redis3.0 This function has been gradually weakened in the Internet , for example Redis Distributed implementation of Redis
Cluster Only use 0 The database , Just for downward compatibility with the database functions of the old version ,
This function is not completely abandoned , Let's analyze why we should discard this “ good ” The function of
Well ? In summary, there are three points :
·Redis It's single threaded . If multiple databases are used , So these databases are still using
One CPU, Each other will still be affected .

· How to use multiple databases , It will make it difficult to debug and operate different business databases ,
If there is a slow query , It will still affect other databases , This will make other business parties decide
The bit problem is very difficult .
· part Redis Our clients don't support this at all . Even if you support , In development
Switch back and forth the database in digital form , It's easy to mess up .
The author suggests that if you want to use multiple database functions , It is possible to deploy multiple
Redis example , Ports are used to distinguish each other , Because modern computers or servers usually have many
individual CPU Of . In this way, the business will not be affected , And use it reasonably CPU information
Source .
2.flushdb/flushall
flushdb/flushall The command is used to clean up the database , The difference between the two is flushdb Only clear when
Pre database ,flushall It will clean up all the databases .
For example 0 Database number has four key value pairs 、1 Database number has three key value pairs :
127.0.0.1:6379> dbsize
(integer) 4
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> dbsize
(integer) 3
If in 0 Database execution flushdb,1 The data from database number one is still there :
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> dbsize
(integer) 3

Execute... On any database flushall Will clean up all databases :
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> dbsize
(integer) 0
flushdb/flushall Command can be very convenient to clean up data , But it also brings two problems :
·flushdb/flushall The command will clear all data , Once misoperation, the consequences are unimaginable , The first
12 Chapter will introduce rename-command Configuration circumvents this problem , And how to recover quickly after misoperation
Complex data .
· If the number of key values in the current database is large ,flushdb/flushall Blocked Redis The possibility of
sex .
So it's using flushdb/flushall Be careful . 

原网站

版权声明
本文为[Gu Ge academic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042229263884.html