Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash script that prints out the largest key in your Redis store.
#!/usr/bin/env bash
# This script prints out the largest key in your Redis store.
# This script was forked from Brent O'Connor at https://gist.github.com/epicserve/5699837. It was subsequently modified by Aaron Alfson.
# License: http://www.apache.org/licenses/LICENSE-2.0
human_size() {
awk -v sum="$1" ' BEGIN {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; } '
}
redis_cmd='redis-cli'
max_key='';
max_key_size=0;
# get keys and sizes
for k in `$redis_cmd keys "*"`; do
key_size_bytes=`$redis_cmd debug object $k | perl -wpe 's/^.+serializedlength:([\d]+).+$/$1/g'`;
# check to see if current key size is greater than the current max key size.
if [ $key_size_bytes -gt $max_key_size ]
then
max_key=$k;
max_key_size=$key_size_bytes;
size=`echo $l | perl -wpe 's/^(\d+).+/$1/g'`;
hsize=`human_size "$size"`
key_type=`$redis_cmd TYPE $max_key`;
echo "Current max_key: $max_key, max_key_size: $hsize, TYPE: $key_type"
fi
done
# print out current key size in human readable form.
size=`echo $l | perl -wpe 's/^(\d+).+/$1/g'`;
hsize=`human_size "$size"`
key_type=`$redis_cmd TYPE $max_key`;
echo "Final max_key: $max_key, max_key_size: $hsize, TYPE: $key_type"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment