Skip to content

Instantly share code, notes, and snippets.

@milosjanda
Created June 15, 2017 08:28
Show Gist options
  • Save milosjanda/1e2fe7c1d6a4cf534da8a07ee03a4760 to your computer and use it in GitHub Desktop.
Save milosjanda/1e2fe7c1d6a4cf534da8a07ee03a4760 to your computer and use it in GitHub Desktop.
Script to get KEYS and theirs TTL
#!/usr/bin/env bash
start=$(date +%s.%N)
# how many keys get in one time
COUNT=10000
# count of loops
LOOP=$(seq 0 1 10)
# delay between requests to redis
DELAY=0.001
# info files
POINTER_FILE="redis-last-pointer.txt"
KEY_FILE="redis-last-key.txt"
LAST_POINTER=0
if [ -f $POINTER_FILE ]; then
LAST_POINTER=`tail -1 $POINTER_FILE`
else
echo 0 > $POINTER_FILE
fi
for i in $LOOP
do
DATA=`redis-cli -p 6379 scan $LAST_POINTER MATCH "*" COUNT $COUNT`
FIRST_LINE=1
for LINE in $DATA ; do
if [ $FIRST_LINE -eq 1 ]; then
FIRST_LINE=0
LAST_POINTER=$LINE
echo "$LINE" >> $POINTER_FILE
else
sleep $DELAY
TTL=`redis-cli ttl "$LINE"`
TTL_DAYS=$(($TTL/3600/24))
if [ $TTL -lt 0 ]; then # in seconds
echo "$LINE TTL: $TTL days" >> $KEY_FILE
elif [ $TTL_DAYS -gt 30 ]; then # in days
echo "$LINE TTL: $(($TTL/3600/24)) days" >> $KEY_FILE
fi
fi
done
done
dur=$(echo "$(date +%s.%N) - $start" | bc)
printf "Execution time: %.6f seconds" $dur
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment