If the MySQL database is large, we can easily find out which tables occupy the space. However, if the Redis memory is large, it is not easy to find out which tables occupy the space) the space occupied by the key.
Some tools can provide necessary help. For example, redis-rdb-tools can directly analyze RDB files to generate reports. Unfortunately, it cannot fully meet my needs, I don't want to perform secondary development on the basis of it. In fact, developing a dedicated tool is very simple. Using commands such as SCAN and DEBUG, there is not much code to achieve:
<? Php
$ Patterns = array (
'Foo:. + ',
'Bar:. + ',
'. + ',
);
$ Redis = new Redis ();
$ Redis-> setOption (Redis: OPT_SCAN, Redis: SCAN_RETRY );
$ Result = array_fill_keys ($ patterns, 0 );
While ($ keys = $ redis-> scan ($ it, $ match = '*', $ count = 1000 )){
Foreach ($ keys as $ key ){
Foreach ($ patterns as $ pattern ){
If (preg_match ("/^ {$ pattern} $/", $ key )){
If ($ v = $ redis-> debug ($ key )){
$ Result [$ pattern] + = $ v ['serializedlength'];
}
Break;
}
}
}
}
Var_dump ($ result );
?>
Of course, the premise is that you need to summarize the possible key modes in advance, the simple but not rigorous method is MONITOR:
Shell>/path/to/redis-cli monitor |
Awk-f' "'' $2 ~ "ADD | SET | STORE | PUSH" {print $4 }'
In addition, it should be noted that, because the serializedlength returned by DEBUG is the length after serialization, the final calculated value is smaller than the actual memory usage, but considering the relative size, it is still of reference significance.