redis使用和通信
redis-cli使用
redis是C/S架构,redis-cli便是用于连接redsi server的客户端工具
-h:指定server ip
-p:指定server port
-a:指定密码(如果启用了密码验证)
# -h:指定server ip, -p:指定server port;
neo@neo:~$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> ping hi
"hi"
127.0.0.1:6379> exit
# 不带参,等同于redis-cli -h 127.0.0.1 -p 6379(带上默认值)
neo@neo:~$ redis-cli
127.0.0.1:6379> ping hello
"hello"
127.0.0.1:6379> exit
# 以上都是“长连接模式”,也可以使用“短连接模式“
neo@neo:~$ redis-cli ping nihao
"nihao"
redis运行信息
想要获取redis server运行信息,可以执行info命令,如果只想获取某个模块,可以带上模块参数 eg:info cpu
neo@neo:~$ redis-cli
127.0.0.1:6379> info
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:b671f408463590b9
redis_mode:standalone
os:Linux 4.15.0-30deepin-generic x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.3.0
process_id:1
run_id:53b5e621234de359d86a77d4c9f8480e741a6c1f
tcp_port:6379
uptime_in_seconds:20796
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:3593961
executable:/data/redis-server
config_file:
# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
# Memory
used_memory:854160
used_memory_human:834.14K
used_memory_rss:6356992
used_memory_rss_human:6.06M
used_memory_peak:854160
used_memory_peak_human:834.14K
used_memory_peak_perc:100.12%
used_memory_overhead:840958
used_memory_startup:791264
used_memory_dataset:13202
used_memory_dataset_perc:20.99%
allocator_allocated:1261792
allocator_active:1552384
allocator_resident:12386304
total_system_memory:8216010752
total_system_memory_human:7.65G
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.23
allocator_frag_bytes:290592
allocator_rss_ratio:7.98
allocator_rss_bytes:10833920
rss_overhead_ratio:0.51
rss_overhead_bytes:-6029312
mem_fragmentation_ratio:7.83
mem_fragmentation_bytes:5544832
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1580631469
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
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
# Stats
total_connections_received:6
total_commands_processed:9
instantaneous_ops_per_sec:0
total_net_input_bytes:212
total_net_output_bytes:57435
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:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
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
# Replication
role:master
connected_slaves:0
master_replid:39e7a5ddecf35f2055904b32a7378152d803b0d1
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
# CPU
used_cpu_sys:9.550919
used_cpu_user:8.733070
used_cpu_sys_children:0.007164
used_cpu_user_children:0.009925
# Cluster
cluster_enabled:0
# Keyspace
redis事件模型
redis之所以高效,是因为最大化的利用单线程(顺序处理命令,序列化方式解决并发)、非阻塞、多路复用I的I/O模型来处理命令; 该模型的主要优点:轻量级上下文切换, 上下文切换的开销小(某些情况,redis也会创建线程或进程来处理任务)
redis通信协议
redis客户端和服务端之间使用的通信协议是基于TCP,叫做redis系列化协议(RESP:redis serialization protocol),下面我们不使用redis-cli,而是使用RESP构造命令来和服务端通信
# 发送ping命令, nc: netcat工具
neo@neo:~$ echo -e "*1\r\n\$4\r\nping\r\n" | nc 127.0.0.1 6379
+PONG
# 发送set mykey myval命令
neo@neo:~$ echo -e "*3\r\n\$3\r\nset\r\n\$5\r\nmykey\r\n\$5\r\nmyval\r\n" | nc 127.0.0.1 6379
+OK
# 发送get mykey命令
neo@neo:~$ echo -e "*2\r\n\$3\r\nget\r\n\$5\r\nmykey\r\n" | nc 127.0.0.1 6379
$5
myval
# 可以多个命令合并发送
neo@neo:~$ echo -e "*1\r\n\$4\r\nping\r\n*3\r\n\$3\r\nset\r\n\$5\r\nmykey\r\n\$5\r\nmyval\r\n*2\r\n\$3\r\nget\r\n\$5\r\nmykey\r\n" | nc 127.0.0.1 6379
+PONG
+OK
$5
myval
那么RESP到底是怎么构造的这么一大串命令的呢,其实去掉换行符就很清晰了(\r\n实际是每个部分的分隔符)
## 解密,去掉换行符,我们就可以清晰看到RESP到底是怎么构造命令的了
neo@neo:~$ echo -e "*1\r\n\$4\r\nping\r\n"
*1
$4
ping
## 解密
neo@neo:~$ echo -e "*2\r\n\$3\r\nget\r\n\$5\r\nmykey\r\n"
*2
$3
get
$5
mykey
网友评论