Redis methods for statistics of various sizes in the _redis

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.