文件包含漏洞:从原理到实战的Web安全攻防指南
2026/6/21 6:03:02
maxmemory:Redis 允许使用的最大内存上限。淘汰策略:在内存达到上限时选择哪些键被删除(LRU/LFU/TTL 等)。LRU:最近最少使用,优先淘汰最久未被访问的键。LFU:最不常用,优先淘汰访问频率低的键。SLOWLOG:慢查询日志,记录执行时间超过阈值的命令。Pipeline:批量发送命令减少往返时间,提高吞吐。热 Key:访问异常频繁的键,可能成为性能瓶颈。大 Key:体积或成员过大的键,影响内存与网络传输效率。命中率:缓存命中次数与总请求次数的比例,衡量缓存有效性。maxmemory与淘汰策略为 LRU/LFU,观察命中率与内存变化。maxmemory 4gb maxmemory-policy allkeys-lrumaxmemory控制上限,超出写入触发淘汰;策略如 LRU/LFU/TTL 等。/etc/redis/redis.conf或/usr/local/etc/redis/redis.confC:\redis\redis.conf/usr/local/etc/redis/redis.confmaxmemory 4gb maxmemory-policy allkeys-lruredis-cli CONFIG SET maxmemory4294967296redis-cli CONFIG SET maxmemory-policy allkeys-lru redis-cli CONFIG GET maxmemory redis-cli CONFIG GET maxmemory-policy redis-cli INFO memorysudovi/etc/redis/redis.conf# 修改 maxmemory 与 maxmemory-policy 后保存退出sudosystemctl restart redis-server 或sudoserviceredis-server restart redis-cli CONFIG GET maxmemory redis-cli CONFIG GET maxmemory-policynotepad C:\redis\redis.conf# 保存后重启进程Stop-Process-Name redis-server-ForceStart-Process"C:\redis\redis-server.exe"-ArgumentList"C:\redis\redis.conf"redis-cliCONFIG GET maxmemory redis-cliCONFIG GET maxmemory-policyslowlog-log-slower-than 10000 slowlog-max-len 256slowlog-log-slower-than以微秒为单位;设得过低会产生大量记录。slowlog-max-len控制日志长度,定期导出分析。slowlog-log-slower-than 10000 slowlog-max-len 256redis-cli CONFIG SET slowlog-log-slower-than10000redis-cli CONFIG SET slowlog-max-len256redis-cli CONFIG GET slowlog-log-slower-than redis-cli CONFIG GET slowlog-max-len# 拉取慢日志样本redis-cli SLOWLOG GET10# 查看总量redis-cli SLOWLOG LEN# 导出 CSV 便于分析redis-cli --csv SLOWLOG GET128>slowlog.csv# 清空慢日志(谨慎操作)redis-cli SLOWLOG RESET# 通用指标redis-cli INFO redis-cli INFO memory redis-cli INFO stats# 延迟诊断redis-cli LATENCY DOCTOR# 内存诊断redis-cli MEMORY STATS redis-cli MEMORY DOCTOR# 大 Key/热 Key 采样(需新版本 redis-cli)redis-cli --bigkeys redis-cli --hotkeysINFO查看实例指标;SLOWLOG GET拉取慢日志样本。INFO包含内存、连接、命中率、持久化等多维指标。SLOWLOG GET N拉取最近 N 条慢日志,辅助定位问题命令。importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.data.redis.core.RedisCallback;importjava.util.List;@ServicepublicclassPipelinePerf{@AutowiredRedisTemplate<String,String>tpl;publicList<Object>writeMany(){// 执行管道批量写入,减少网络往返returntpl.executePipelined((RedisCallback<Object>)connection->{for(inti=0;i<1000;i++){// 逐条写入字符串键值connection.stringCommands().set(("k:"+i).getBytes(),("v:"+i).getBytes());}returnnull;});}}# 使用 redis-benchmark 测试基础吞吐(Linux/macOS)redis-benchmark -t set,get -n100000-q# 使用管道并发提升吞吐(-P 指定并发 pipeline 深度)redis-benchmark -tset-n200000-P50-q# 通过 --csv 输出便于分析redis-benchmark -t set,get -n100000-P20--csv>benchmark.csv# Windows 使用 redis-benchmark.exe(路径按安装调整)&"C:\redis\redis-benchmark.exe"-tset,get-n 100000-P 50-q# 使用 redis-cli --pipe 进行批量导入(Linux/macOS)seq1100000|awk'{print "SET k:"$1" v:"$1}'|redis-cli --pipe# 小型生产环境(折中可靠性与性能) maxmemory 2gb maxmemory-policy allkeys-lru slowlog-log-slower-than 10000 slowlog-max-len 256 # 高吞吐环境(优先稳态吞吐) maxmemory 16gb maxmemory-policy allkeys-lfu slowlog-log-slower-than 5000 slowlog-max-len 1024