redis_keys命令&&redis的持久化方案

本文详细介绍了Redis中键值的各种操作命令,包括设置生存时间、查询、删除及重命名等,同时深入探讨了RDB与AOF两种持久化方案的原理与配置方法。
  1. Keys命令
    1. 设置key的生存时间

Redis在实际使用过程中更多的用作缓存,然而缓存的数据一般都是需要设置生存时间的,即:到期后数据销毁。

 

EXPIRE key seconds                设置key的生存时间(单位:秒)key在多少秒后会自动删除

TTL key                                    查看key生于的生存时间

PERSIST key                      清除生存时间

PEXPIRE key milliseconds      生存时间设置单位为:毫秒

 

例子:

192.168.101.3:7002> set test 1            设置test的值为1

OK

192.168.101.3:7002> get test               获取test的值

"1"

192.168.101.3:7002> EXPIRE test 5      设置test的生存时间为5秒

(integer) 1

192.168.101.3:7002> TTL test              查看test的生于生成时间还有1秒删除

(integer) 1

192.168.101.3:7002> TTL test

(integer) -2

192.168.101.3:7002> get test               获取test的值,已经删除

(nil)

 

 

      1. keys

返回满足给定pattern 的所有key

redis 127.0.0.1:6379> keys mylist*

1) "mylist"

2) "mylist5"

3) "mylist6"

4) "mylist7"

5) "mylist8"

 

 

      1. exists

确认一个key 是否存在

示例:从结果来看,数据库中不存在HongWan 这个key,但是age 这个key 是存在的

redis 127.0.0.1:6379> exists HongWan

(integer) 0

redis 127.0.0.1:6379> exists age

(integer) 1

redis 127.0.0.1:6379>

 

      1. del

删除一个key

redis 127.0.0.1:6379> del age

(integer) 1

redis 127.0.0.1:6379> exists age

(integer) 0

 

      1. rename

重命名key

rename oldKey new Key

示例:age 成功的被我们改名为age_new 了

redis 127.0.0.1:6379[1]> keys *

1) "age"

redis 127.0.0.1:6379[1]> rename age age_new

OK

redis 127.0.0.1:6379[1]> keys *

1) "age_new"

redis 127.0.0.1:6379[1]>

 

      1. type

返回值的类型

示例:这个方法可以非常简单的判断出值的类型

redis 127.0.0.1:6379> type addr

string

redis 127.0.0.1:6379> type myzset2

zset

redis 127.0.0.1:6379> type mylist

list

redis 127.0.0.1:6379>

 

--------------------------------------------------------------------------------------------

  1. Redis持久化方案
    1. RDB持久化

RDB方式的持久化是通过快照(snapshotting)完成的,当符合一定条件时Redis会自动将内存中的数据进行快照并持久化到硬盘

 

RDB是Redis默认采用的持久化方式。

 

save 900 1

save 300 10

save 60 10000

      1. 持久化条件配置

save 开头的一行就是持久化配置,可以配置多个条件(每行配置一个条件),每个条件之间是“或”的关系。

“save 900 1”表示15分钟(900秒钟)内至少1个键被更改则进行快照。

“save 300 10”表示5分钟(300秒)内至少10个键被更改则进行快照。

 

      1. 配置快照文件目录

配置dir指定rdb快照文件的位置

# Note that you must specify a directory here, not a file name.

dir ./

 

 

      1. 配置快照文件的名称

设置dbfilename指定rdb快照文件的名称

# The filename where to dump the DB

dbfilename dump.rdb

 

 

       Redis启动后会读取RDB快照文件,将数据从硬盘载入到内存。根据数据量大小与结构和服务器性能不同,这个时间也不同。通常将记录一千万个字符串类型键、大小为1GB的快照文件载入到内存中需要花费20~30秒钟。

 

      1. 问题总结

       通过RDB方式实现持久化,一旦Redis异常退出,就会丢失最后一次快照以后更改的所有数据。这就需要开发者根据具体的应用场合,通过组合设置自动快照条件的方式来将可能发生的数据损失控制在能够接受的范围。

如果数据很重要以至于无法承受任何损失,则可以考虑使用AOF方式进行持久化

 

    1. AOF持久化

默认情况下Redis没有开启AOF(append only file)方式的持久化

 

  • 可以通过修改redis.conf配置文件中的appendonly参数开启

appendonly yes

 

开启AOF持久化后每执行一条会更改Redis中的数据的命令,Redis就会将该命令写入硬

盘中的AOF文件。

  • AOF文件的保存位置和RDB文件的位置相同,都是通过dir参数设置的。

dir ./

 

  • 默认的文件名是appendonly.aof,可以通过appendfilename参数修改:

appendfilename appendonly.aof

 

请根据下述 Prometheus 采集得到的 redis 监控指标,给出 redis 集群建议的监控告警指标,并附上对应的告警表达式 redis_active_defrag_running、redis_allocator_active_bytesredis_allocator_allocated_bytes redis_allocator_frag_bytes redis_allocator_frag_ratio redis_allocator_resident_bytes redis_allocator_rss_bytes redis_allocator_rss_ratio redis_aof_base_size_bytes redis_aof_buffer_length redis_aof_current_rewrite_duration_sec redis_aof_current_size_bytes redis_aof_delayed_fsync redis_aof_enabled redis_aof_last_bgrewrite_status redis_aof_last_cow_size_bytes redis_aof_last_rewrite_duration_sec redis_aof_last_write_status redis_aof_pending_bio_fsync redis_aof_pending_rewrite redis_aof_rewrite_buffer_length redis_aof_rewrite_in_progress redis_aof_rewrite_scheduled redis_blocked_clients redis_client_recent_max_input_buffer_bytes redis_client_recent_max_output_buffer_bytes redis_cluster_connections redis_cluster_current_epoch redis_cluster_enabled redis_cluster_known_nodes redis_cluster_messages_received_total redis_cluster_messages_sent_total redis_cluster_my_epoch redis_cluster_size redis_cluster_slots_assigned redis_cluster_slots_fail redis_cluster_slots_ok redis_cluster_slots_pfail redis_cluster_state redis_cluster_stats_messages_auth_ack_sent redis_cluster_stats_messages_auth_req_received redis_cluster_stats_messages_fail_received redis_cluster_stats_messages_fail_sent redis_cluster_stats_messages_ping_received redis_cluster_stats_messages_ping_sent redis_cluster_stats_messages_pong_received redis_cluster_stats_messages_pong_sent redis_cluster_stats_messages_update_received redis_cluster_stats_messages_update_sent redis_commands_duration_seconds_total redis_commands_processed_total redis_commands_total redis_config_io_threads redis_config_maxclients redis_config_maxmemory redis_connected_clients redis_connected_slave_lag_seconds redis_connected_slave_offset_bytes redis_connected_slaves redis_connections_received_total redis_cpu_sys_children_seconds_total redis_cpu_sys_seconds_total redis_cpu_user_children_seconds_
最新发布
03-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值