当前位置:网站首页>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
- persist command
persist key
- 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
- 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
- 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"
- 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"
边栏推荐
- 披萨订购设计----简单工厂模式
- Intelligent operation and maintenance exploration | anomaly detection method in cloud system
- 3. caller service call - dapr
- How to use matrix analysis to build your thinking scaffold in flowus, notation and other note taking software
- 自定义标签——jsp标签增强
- Redis学习笔记—Pipeline
- Combination sum of leetcode topic analysis
- 16.系统启动流程
- 位绑定
- Leetcode topic analysis contains duplicate II
猜你喜欢

Redis学习笔记—持久化机制之RDB

社区文章|MOSN 构建 Subset 优化思路分享

Testing -- automated testing selenium (about API)

Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“

简易学生管理

S5P4418裸机编程的实现(替换2ndboot)

636. Exclusive Time of Functions

一个采用直接映射方式的32KB缓存......存储器课后习题

General paging (1)
Redis学习笔记—慢查询分析
随机推荐
How to use matrix analysis to build your thinking scaffold in flowus, notation and other note taking software
一个采用直接映射方式的32KB缓存......存储器课后习题
进入小公司的初级程序员要如何自我提高?
Unity grid programming 06
Quartz Crystal Drive Level Calculation
Unity grid programming 08
嵌入式系统概述(学习笔记)
How to use "tomato working method" in flowus, notation and other note taking software?
Redis学习笔记—redis-cli详解
【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
Redis学习笔记—客户端通讯协议RESP
Balls and cows of leetcode topic analysis
Servlet-02 生命周期
670. Maximum Swap
Redis学习笔记—发布订阅
MySQL故障案例 | mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME
Structure binary tree from inorder and postorder traversal for leetcode topic analysis
[QNX Hypervisor 2.2用户手册]6.2 网络
位绑定
528. Random Pick with Weight