一、数据结构
1.五种类型
1.字符串(String) 2.哈希(Hash) 3.列表(List) 4.集合(Set) 5.有序集合(Zset)
二、控制台操作
1.Redis服务安装(自行查找)
2.进入Redis客户端
/usr/local/bin/redis-cli
select 1
3.查看支持的最大连接数
CONFIG GET maxclients
4.查看Redis运行信息(info)
1.Server 服务器运行的环境参数
2.Clients 客户端相关信息
3.Memory 服务器运行内存统计数据
4.Persistence 持久化信息
5.Stats 通用统计数据
6.Replication 主从复制相关信息
7.CPU CPU 使用情况
8.Cluster 集群信息
9.KeySpace 键值对统计数量信息
127.0.0.1:6379> info
redis_version:6.2.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:fdd28bd28db05332
redis_mode:standalone
os:Linux 3.10.0-1160.41.1.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:10.2.1
process_id:1
process_supervised:no
run_id:aab8921018c8bc6d3da65cfc8e4d92173f656667
tcp_port:6379
server_time_usec:1653745310451756
uptime_in_seconds:5981287
uptime_in_days:69
hz:10
configured_hz:10
lru_clock:9578142
executable:/data/redis-server
config_file:/usr/local/etc/redis.conf
io_threads_active:0
connected_clients:2
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:32
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0
used_memory:920312
used_memory_human:898.74K
used_memory_rss:3059712
used_memory_rss_human:2.92M
used_memory_peak:1732344
used_memory_peak_human:1.65M
used_memory_peak_perc:53.13%
used_memory_overhead:851104
used_memory_startup:810088
used_memory_dataset:69208
used_memory_dataset_perc:62.79%
allocator_allocated:964152
allocator_active:1228800
allocator_resident:3600384
total_system_memory:4142100480
total_system_memory_human:3.86G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.27
allocator_frag_bytes:264648
allocator_rss_ratio:2.93
allocator_rss_bytes:2371584
rss_overhead_ratio:0.85
rss_overhead_bytes:-540672
mem_fragmentation_ratio:3.49
mem_fragmentation_bytes:2182160
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:41016
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0
loading:0
current_cow_size:0
current_cow_size_age:0
current_fork_perc:0.00
current_save_keys_processed:0
current_save_keys_total:0
rdb_changes_since_last_save:244
rdb_bgsave_in_progress:0
rdb_last_save_time:1648010183
rdb_last_bgsave_status:err
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:307200
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
module_fork_in_progress:0
module_fork_last_cow_size:0
total_connections_received:16094
total_commands_processed:1272
instantaneous_ops_per_sec:0
total_net_input_bytes:354548
total_net_output_bytes:917925
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:12
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:139396
evicted_keys:0
keyspace_hits:2
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:426
total_forks:956523
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:17444
dump_payload_sanitizations:0
total_reads_processed:32909
total_writes_processed:16682
io_threaded_reads_processed:0
io_threaded_writes_processed:0
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:bc9570d83101478a9f2c3e06621fd4f89922bba3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
used_cpu_sys:4533.539237
used_cpu_user:5258.822171
used_cpu_sys_children:354.305248
used_cpu_user_children:288.748708
used_cpu_sys_main_thread:4531.954658
used_cpu_user_main_thread:5257.710509
errorstat_ERR:count=1646
errorstat_MISCONF:count=20
errorstat_NOAUTH:count=15175
errorstat_WRONGPASS:count=603
cluster_enabled:0
5.命令提示
三、应用场景
1.字符串操作
·单值缓存 ·对象缓存(JsonString) ·分布式锁 ·计数器
单值缓存
set myKey moon
mset k1 v1 k2 v2 k3 v3
setnx k1 v
get mykEY
mget k1 k2 k3
keys *
del k1 k2
expire myKey 3
分布式锁
set product:001 true ex 5 nx
计数器(点赞量、阅读量、序列号生成)
set pv 0
incr pv
decr pv
incrby pv 3
decrby pv 2
get pv
2.Hash缓存
·将多个 field-value (字段-值) 对设置到哈希表中
优缺点
优点
同类数据归类整合储存,方便数据管理:类似于表
相比string操作消耗内存与cpu更小、更节省储存空间
缺点
过期功能不能使用在field上,只能用在key上
Redis集群架构下不适合大规模使用
数据存储
hmset user 1:name moon 1:age 18
hmget user 1:name 1:age
hmget user 1:name
hmset user 2:name sun
hmget user 2:name
hset msg:001 friend:001 1
hincrby msg:001 friend:001 3
hlen msg:001
hdel msg:001 friend:002
hgetall msg:001
Redis集群模式下的Hash槽简介
单台机器性能不满足业务需求时,我们会考虑集群部署,即数据分片存储;此时对于Hash数据来说,要有一个分片标准:Redis Key 的路由计算公式:slot = CRC16(key) % 16384 CRC16 算法,产生的hash值有 16 bit 位,可以产生 65536(2^16)个值 ,也就是说值分布在 0 ~ 65535 之间 那么为什么槽位数取了 2^14=16384 而不是 2^16=65536 因为集群下,各个主从节点间要进行数据同步,同步过程会包含槽位信息,信息大小如下: 16384÷8÷1024=2kb 65536÷8÷1024=8kb 综合性能、集群容量、网络带宽等,作者将槽位数定为:16384 集群扩容后,槽位调整,会将某些哈希槽挪到新节点;如果插入数据时,当前节点发现当前数据不属于本节点,则会返回正确节点信息给客户端,客户端自动重发到正确节点
3.List列表结构(微博/朋友圈消息流…)
lpush mnt 1 2 3 4 5
lrange mnt 0 4
rpush mnt 6 7 8 9
lrange mnt 0 9
blpop mnt 0
brpop mnt 0
Set集合结构
抽奖(将用户添加到集合内,随机选取3个)
sadd mnt 1 2 3
srem mnt 1
smembers mnt
scard mnt
sismember mnt 1
sadd mnt 0 1 2 3 4 5 6 7 8 9
srandmember mnt 3
spop mnt 3
共同关注人|按产品属性多条件筛选(交集)、可能认识的人(差集)
sadd qps 4 11 7 2
sinter mnt qps
sinterstore destination mnt qps
sunion mnt qps
sunionstore dest mnt qps
sdiff mnt qps
sdiffstore des mnt qps
5.Zset有序集合
有序集合的分数(score)可以重复,但成员是唯一的
zadd mnt 1 math 3 chinese 2 english
zrem mnt math
zscore mnt english
zincrby mnt 4 chinese
zcard mnt
zrange mnt 0 1
zrevrange mnt 0 1
排行榜前10
zadd mnt 1 a 2 b 3 c
zadd qps 4 d 5 e 6 f 7 a
zinterstore dest 2 mnt qps
zunionstore dest 2 mnt qps
|