Redis   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Redis系列五:redis键管理和redis数据库管理大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

一、redis键管理

1 键重命名

rename oldKey newkey //格式rename oldKey newKey //若oldKey之前存在则被覆盖set name james ;set name1 mike //数据初始化renamenx name name1 //重命名失败,只有当name1不存在才能改名

2 返回随机键

randomkey //返回随机键

3 键过期

expire name:03 20 //键name:03 在10秒后过期ttl name:03 //查看过期按秒到计时,当返回-2说明已删除PTTL name:03 //查看过期按毫秒到时计set name:05 james //初始化数据pexpire name:05 20000 //20000毫秒(20S)后过期expire name:06 -2 //直接过期,和del一样

设置键在某个时间点过期 使用的是时间戳 expireat name:04 1516971599 //设置在2018/01/27 20:59:59过期时间转时间戳:网址http://tool.chinaz.com/Tools/unixtime.aspx

hset user:01 name james //初始化数据expire user:01 60 //设置60S后过期ttl user:01 //查看过期剩余时间persist user:01 //去掉过期ttl user:1 //返回-1 可以永久查询不失效

注意:对于字符串重设值后,expire无效,set name jamesexpire name 50ttl nameset name james1 //此时expire取消ttl name //返回-1, 长期有效

4. 键的迁移

把部分数据迁移到另一台redis服务器

1,move key db  //rEIDs有16个库, 编号为0-15

 set name james1;  move name 5 //迁移到第6个库

 SELEct 5 ;//数据库切换到第6个库, get name  可以取到james1

 这种模式不建议在生产环境使用,在同一个rEIDs里可以玩

2,dump key; 

 restore key ttl value//实现不同redis实例的键迁移,ttl=0代表没有过期时间

例子:在A服务器上 192.168.1.111

set name james;

dump name; //  得到"\x00\x05james\b\x001\x82;f\"DhJ"

在B服务器上:192.168.1.118

restore name 0 "\x00\x05james\b\x001\x82;f\"DhJ" 

get name  //返回james

3,migrate指令迁移到其它实例redis,在1.111服务器上将test移到118

</td>
<td style="width: 77.15pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top-width: 1pt; border-top-color: windowtext; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="154">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 10.5000pt">192.168.1.118

</td>
<td style="width: 35.35pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top-width: 1pt; border-top-color: windowtext; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="70">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 10.5000pt">6379  

</td>
<td style="width: 43.4pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top-width: 1pt; border-top-color: windowtext; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="86">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 10.5000pt">test

</td>
<td style="width: 33.75pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top-width: 1pt; border-top-color: windowtext; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="67">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 10.5000pt">0

</td>
<td style="width: 42.35pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top-width: 1pt; border-top-color: windowtext; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="84">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 10.5000pt">1000

</td>
<td style="width: 45.5pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top-width: 1pt; border-top-color: windowtext; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="91">
<p class="Msonormal"><span style="font-family: 宋体; color: #ff0000; font-size: 10.5000pt">copy

</td>
<td style="width: 43.4pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top-width: 1pt; border-top-color: windowtext; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="86">
<p class="Msonormal"><span style="font-family: 宋体; color: #ff0000; font-size: 10.5000pt">replace

</td>
<td style="width: 32.15pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top-width: 1pt; border-top-color: windowtext; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="64">
<p class="Msonormal"><span style="font-family: 宋体; color: #ff0000; font-size: 10.5000pt">keys

</td>

</tr>
<tr>
<td style="width: 44.25pt; padding: 0pt 5.4pt; border-left-width: 1pt; border-left-color: windowtext; border-right-width: 1pt; border-right-color: windowtext; border-top: none; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="88">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 7.5000pt">指令

</td>
<td style="width: 77.15pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top: none; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="154">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 7.5000pt">要迁移的目标<span style="font-family: Calibri">IP

</td>
<td style="width: 35.35pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top: none; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="70">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 7.5000pt">端口

</td>
<td style="width: 43.4pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top: none; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="86">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 7.5000pt">迁移键值

</td>
<td style="width: 33.75pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top: none; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="67">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 7.5000pt">目标库

</td>
<td style="width: 42.35pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top: none; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="84">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 7.5000pt">超时时间

</td>
<td style="width: 45.5pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top: none; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="91">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 7.5000pt">迁移后不删除原键

</td>
<td style="width: 43.4pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top: none; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="86">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 7.5000pt">不管目标库是不存在<span style="font-family: Calibri">test<span style="font-family: 宋体">键都迁移成功

</td>
<td style="width: 32.15pt; padding: 0pt 5.4pt; border-left: none; border-right-width: 1pt; border-right-color: windowtext; border-top: none; border-bottom-width: 1pt; border-bottom-color: windowtext" valign="top" width="64">
<p class="Msonormal"><span style="font-family: 宋体; font-size: 7.5000pt">迁移多个键

</td>

</tr>

5. 键的遍历

redis提供了两个命令来遍历所有的键

1,键全量遍历:

@H_507_4@mset country china city bj name james  //设置3个字符串键值对 

keys  * //返回所有的键,*匹配任意字符多个字符

keys *y //以结尾的键, 

keys n*e //以n开头以e结尾,返回name

keys n?me  //  ?问号代表只匹配一个字符  返回name,全局匹配 

keys n?m*   //返回name

keys [j,l]*  //返回以j l开头的所有键  keys [j]ames 全量匹配james

虑到是单线程, 在生产环境不建议使用,如果键多可能会阻塞,如果键少,可以

2,渐进式遍历

@H_507_4@mset  a a b b c c d d e e f f g g h h i i j j k k l l m m n n o o p p q q r r s s t t u u v v w w x x y y z z    //初始化26个字母键值对

字符串类型: 

scan 0 match n* count 20 //匹配以n开头的键,取20条,第一次scan 0开始

Redis系列五:redis键管理和redis数据库管理

@H_760_197@10@H_760_197@@H_760_197@@H_760_197@@H_760_197@0@H_760_197@

注:渐进式遍历可有效地解决keys命令可能产生的阻塞问题除scan字符串外:还有以下SCAN 命令用于迭代当前数据库中的数据库键。SSCAN 命令用于迭代集合键中的元素。HSCAN 命令用于迭代哈希键中的键值对。ZSCAN 命令用于迭代有序集合中的元素(包括元素成员和元素分值)。用法和scan一样

二、redis数据库管理

SELEct 0 //共16个库, 0 --15, SELEct切换数据库set name jamesSELEct 1get name //隔离了,取不到,和mysql不同库一样

其中redis3.0以后的版本慢慢弱化了这个功能,如在redis cluster中只允许0数据库原因:1,redis单线程,如果用多个库,这些库使用同一个cpu,彼此会有影响2,多数据库,调试与运维麻烦,若有一个慢查询,会影响其它库查询速度3,来回切换,容易混乱

flushdb: 只清空当前数据库的键值对 dbsiz 0flushall: 清空所有库的键值对 (这两个指令慎用!!!!)

大佬总结

以上是大佬教程为你收集整理的Redis系列五:redis键管理和redis数据库管理全部内容,希望文章能够帮你解决Redis系列五:redis键管理和redis数据库管理所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: