这是一个 Go 语言实现的 Redis 内存分析工具:用于找出占用内存较多的 Key,将结果导出至 csv 文件。
示例代码
通过直接连接到 redis 来分析 key
analysis := NewAnalysis() //Open redis: 127.0.0.1:6379 without password err := analysis.Open("127.0.0.1", 6379, "") defer analysis.Close() if err != nil { fmt.Println("something wrong:", err) return } //Scan the keys which can be split by '#' ':' //Special pattern characters need to escape by '\' analysis.Start([]string{"#", ":"}) //Find the csv file in default target folder: ./reports //CSV file name format: redis-analysis-{host:port}-{db}.csv //The keys order by count desc err = analysis.SaveReports("./reports") if err == nil { fmt.Println("done") } else { fmt.Println("error:", err) }
通过 redis RDB 文件分析 key,但无法计算出 key 的大小
analysis := NewAnalysis() //Open redis rdb file: ./6379_dump.rdb err := analysis.OpenRDB("./6379_dump.rdb") defer analysis.CloseRDB() if err != nil { fmt.Println("something wrong:", err) return } //Scan the keys which can be split by '#' ':' //Special pattern characters need to escape by '\' analysis.StartRDB([]string{"#", ":"}) //Find the csv file in default target folder: ./reports //CSV file name format: redis-analysis-{host:port}-{db}.csv //The keys order by count desc err = analysis.SaveReports("./reports") if err == nil { fmt.Println("done") } else { fmt.Println("error:", err) }
Demo