当前位置:网站首页>Redis learning notes - single key management

Redis learning notes - single key management

2022-06-23 09:10:00 Love Guoba

The key to rename

rename key newkey

Rename key name “python” by “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 the original key exists ,rename Will overwrite the value of the original key

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"

In order to prevent forced covering Redis Provides renamenx command

renamenx key newkey

Illustrate with examples

127.0.0.1:6379> set a b
OK
127.0.0.1:6379> set c d
OK
127.0.0.1:6379> renamenx a c
(integer) 0
127.0.0.1:6379> get a
"b"
127.0.0.1:6379> get c
"d"
127.0.0.1:6379> renamenx a e
(integer) 1

Returns a key at random

randomkey

Random return key

127.0.0.1:6379> dbsize
(integer) 21
127.0.0.1:6379> randomkey
"user:ranking:2"
127.0.0.1:6379> randomkey
"java"
127.0.0.1:6379> randomkey
"user:2"

Key expired

1. Set key expired

Second level :

expire key seconds

millisecond :

pexpire key milliseconds
2. View the remaining expiration time of the key

Second level

ttl

millisecond

pttl
  • 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 .

Set the key to “java” Expiration time 100 second

127.0.0.1:6379> expire java 100
(integer) 1
127.0.0.1:6379> pttl java
(integer) 95390
127.0.0.1:6379> ttl java
(integer) 89
3. Key in timestamp timestamp After expired

Second timestamps :

<pre>expireat key timestamp</pre>

Millisecond timestamps :

pexpireat key milliseconds-timestamp

Set the key “hello” stay 2019-03-11 15:35:42 Be overdue ( The second timestamp is 1552289742, The millisecond is 1552289742000)

expireat hello 1552289742
4. The expiration time of the clear key
  1. persist command
persist key
  1. String type key, direct set Will clear key The expiration time of
5. Add expiration time while setting key value
setex key seconds value

setex Not just atoms , At the same time, it reduces the time of a network communication

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 to another one Redis( For example, moving from a production environment to a test environment ),Redis In the course of development, it provides move、dump+restore、migrate Three ways to transfer bonds , Their implementations and scenarios are different

  1. move
move key db

move The command is used in the Redis Internal data migration ,Redis There can be multiple databases inside , Data is isolated from each other ,move Is to move from one database to another

  1. dump+restore
dump key
restore key ttl value

dump+restore The implementation is different Redis Data migration between instances :

  • In the source Redis For instance ,dump The command serializes the key values , The format is RDB Format
  • In the target Redis On ,restore The restore command restores the values serialized above , among ttl For expiration time , If ttl Set to 0 There is no expiration time

Host computer 1 Chinese vs key Serialize the data , return RDB Serialization of formats value

127.0.0.1:6379> dump java
"\x00\x05jedis\t\x00b\xfc\b/\xf1'<)"

Copy this serialized value, Host computer 2 in restore Migrating data

127.0.0.1:6379> get java
(nil)
127.0.0.1:6379> restore java 0 "\x00\x05jedis\t\x00b\xfc\b/\xf1'<)"
OK
127.0.0.1:6379> get java
"jedis"
  1. migrate
migrate host port key|"" destination-db timeout [COPY] [REPLACE] [KEYS key]
  • host: The goal is Redis Of IP Address
  • port: The goal is Redis The port of
  • key: If you write the key name for a single key , If you transfer multiple keys, write double quotes here "" An empty string (Redis3.0.6 Multiple key migrations are supported after release )
  • destination-db: Index of the target database , If it is 0 No. database will write 0
  • timeout: Timeout for migration ( 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 will be migrated normally for data coverage
  • [KEYS key]: Migrate multiple keys , For example to migrate key1、key2、key3, Fill in here “keys key1 key2 key3

mingrate The implementation is different Redis Data migration between instances , actually migrate An order is to make dump、restore、del Three commands combined , Thus, the operation process is simplified .
This command is an atomic operation , It blocks two instances of the migration when it is executed , Until any of the following results occur : Migration successful , Migration failed , Wait until the timeout .

Open another port as 6666 Of Redis example

./redis-server --port 6666 &
redis-cli -h 127.0.0.1 -p 6666
127.0.0.1:6666> keys *
(empty list or set)

6379 Port instance migrate Of copy operation

127.0.0.1:6379> migrate 127.0.0.1 6666 java 0 1000 copy
OK
127.0.0.1:6379> get java
"jedis"

6666 Port instance

127.0.0.1:6666> keys *
1) "java"

6379 Port instance migrate Default action for (dump——restore——del)

127.0.0.1:6379> set hello world 
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> migrate 127.0.0.1 6666 hello 0 1000 
OK
127.0.0.1:6379> get hello
(nil)

6666 Port instance

127.0.0.1:6666> keys *
1) "java"
2) "hello"

From port 6379 Port instances are migrated to in batches 6666 Port instance

127.0.0.1:6379> mset Tom "It's a cat" Jerry "It's a mouse"
OK
127.0.0.1:6379> mget Tom Jerry
1) "It's a cat"
2) "It's a mouse"
127.0.0.1:6379> migrate 127.0.0.1 6666 "" 0 1000 keys Tom Jerry
OK
127.0.0.1:6379> mget Tom Jerry
1) (nil)
2) (nil)

6666 Port instance

127.0.0.1:6666> keys *
1) "Tom"
2) "Jerry"
3) "java"
4) "hello"
原网站

版权声明
本文为[Love Guoba]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230900185405.html