当前位置:网站首页>Redis学习笔记—单个键管理
Redis学习笔记—单个键管理
2022-06-23 09:00:00 【爱锅巴】
键重命名
rename key newkey
重命名键名“python”为“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"
如果原来的键存在,rename会覆盖原来的键的值
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"
为了防止强行覆盖Redis提供了renamenx命令
renamenx key newkey
举例说明
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
随机返回一个键
randomkey
随机返回键
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"
键过期
1.设置键过期
秒级:
expire key seconds
毫秒级:
pexpire key milliseconds
2.查看键的剩余过期时间
秒级别
ttl
毫秒级
pttl
- 大于等于0的整数:键剩余的过期时间(ttl是秒,pttl是毫秒)
- -1:键没有设置过期时间
- -2:键不存在。
设置键为“java”过期时间100秒
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.键在时间戳timestamp后过期
秒级时间戳:
<pre>expireat key timestamp</pre>
毫秒级时间戳:
pexpireat key milliseconds-timestamp
设置键“hello”在2019-03-11 15:35:42过期(秒时间戳是1552289742,毫秒为1552289742000)
expireat hello 1552289742
4.清除键的过期时间
- persist 命令
persist key
- 字符串类型的key,直接set会清除key的过期时间
5.设置键值的同时添加过期时间
setex key seconds value
setex 不但是原子执行,同时减少了一次网络通讯的时间
迁移键
迁移键功能非常重要,因为有时候我们只想把部分数据由一个Redis迁移到另一个Redis(例如从生产环境迁移到测试环境),Redis发展历程中提供了move、dump+restore、migrate三组迁移键的方法,它们的实现方式以及使用的场景不太相同
- move
move key db
move命令用于在Redis内部进行数据迁移,Redis内部可以有多个数据库,彼此数据是相互隔离的,move就是从一个数据库移动到另外一个数据库
- dump+restore
dump key
restore key ttl value
dump+restore实现的是在不同Redis实例间进行数据迁移:
- 在源Redis实例上,dump命令会将键值序列化,格式采用的是RDB格式
- 在目标Redis上,restore命令将上面序列化的值进行复原,其中ttl为过期时间,如果ttl设置为0则代表没有过期时间
在主机1中对key的数据进行序列化,返回RDB格式的序列化value
127.0.0.1:6379> dump java
"\x00\x05jedis\t\x00b\xfc\b/\xf1'<)"
复制这个序列化的value,在主机2中restore迁移数据
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:目标Redis的IP地址
- port:目标Redis的端口
- key:如果单个键就写键名,如果转移多个键此处就写双引号""空字符串(Redis3.0.6版本之后支持迁移多个键)
- destination-db:目标数据库的索引,如果是0号数据库就写0
- timeout:迁移的超时时间(毫秒)
- [copy]:如果添加此选项,迁移后并不删除源键
- [replace]:如果添加此选项,migrate不管目标Redis是否存在该键都会正常迁移进行数据覆盖
- [KEYS key]:迁移多个键,例如要迁移key1、key2、key3,此处填写“keys key1 key2 key3
mingrate实现的是在不同Redis实例间进行数据迁移,实际上migrate命令就是将dump、restore、del三个命令进行组合,从而简化了操作流程。
这个命令是一个原子操作,它在执行的时候会阻塞进行迁移的两个实例,直到以下任意结果发生:迁移成功,迁移失败,等到超时。
开另一个端口为6666的Redis实例
./redis-server --port 6666 &
redis-cli -h 127.0.0.1 -p 6666
127.0.0.1:6666> keys *
(empty list or set)
6379端口实例进行migrate的copy操作
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端口实例
127.0.0.1:6666> keys *
1) "java"
6379端口实例进行migrate的默认操作(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端口实例
127.0.0.1:6666> keys *
1) "java"
2) "hello"
从端口6379端口实例批量迁移到6666端口实例
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端口实例
127.0.0.1:6666> keys *
1) "Tom"
2) "Jerry"
3) "java"
4) "hello"
边栏推荐
- [qnx hypervisor 2.2 user manual]6.2 network
- General paging (1)
- 4sum of leetcode topic analysis
- Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“
- 3、 System analysis and design
- 173. Binary Search Tree Iterator
- '教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
- Longest substring without repeated characters (C language)
- Leetcode topic analysis 3sum
- 自定义标签——jsp标签增强
猜你喜欢

自定义标签——jsp标签增强

三层架构与SSM之间的对应关系

The fourth online workshop review

“教练,我想打篮球“ —— 给做系统的同学们准备的 AI 学习系列小册

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册

多线程初学

986. Interval List Intersections

The sliding window of the force button "step by step" (209. sub array with the smallest length, 904. fruit basket)

Click Add drop-down box

瞄准海外宠物市场,「Grasphand 」做了一款独立于手机的智能追踪产品 | 早期项目
随机推荐
Leetcode topic analysis 3sum
Summary of Arthas vmtool command
Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent
523. Continuous Subarray Sum
Summary ranges of leetcode topic resolution
How thingjs enables low threshold 3D visualization development
Detailed explanation of srl16e in xilinxffpga
Fraction to recursing decimal
The sliding window of the force button "step by step" (209. sub array with the smallest length, 904. fruit basket)
Best time to buy and sell stock II
1、 Software architecture evaluation
Quartz Crystal Drive Level Calculation
[QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
Mqtt+flink to subscribe and publish real-time messages
Use of type dependent names must be prefixed with 'typename'
Testing -- automated testing selenium (about API)
Intelligent operation and maintenance exploration | anomaly detection method in cloud system
Combination sum III of leetcode topic analysis
【NanoPi2试用体验】裸机第一步
4sum of leetcode topic analysis