If the MySQL database is large, we can easily find out which tables occupy the space, but if the Redis memory is relatively large, we are not very easy to find out which (kind) keys Occupy space.
There are tools that can provide the necessary help, such as redis-rdb-tools can directly analyze the Rdb file to generate the report, but it does not achieve my requirements, and I do not want to build on it two times. In fact, developing a dedicated tool is very simple, with commands such as SCAN and DEBUG, and not many lines of code can be implemented:
Copy Code code as follows:
<?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 sum up the possible key patterns in advance, simple but not rigorous method is MONITOR:
Copy Code code as follows:
Shell>/PATH/TO/REDIS-CLI Monitor |
Awk-f ' "' $ ~" add| Set| store| PUSH "{print $}"
In addition, it is important to note that because DEBUG returns a serializedlength length, the resulting value is less than the actual memory footprint, but it is still useful to consider the relative size.