Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set expire on large set of keys using pattern in Redis
#!/bin/bash
if [ $# -ne 4 ]
then
echo "Usage: $0 <host> <port> <pattern> <seconds>"
exit 1
fi
cursor=-1
keys=""
ttl=0
expire="$4"
while [ $cursor -ne 0 ]; do
if [ $cursor -eq -1 ]
then
cursor=0
fi
reply=`redis-cli -h $1 -p $2 SCAN $cursor MATCH $3`
cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'`
keys=`echo $reply | cut -d' ' -f2-`
for key in ${keys// / } ; do
ttl=`redis-cli -h $1 -p $2 TTL $key`
act=""
if [ $ttl -eq -1 ]
then
result=`redis-cli -h $1 -p $2 EXPIRE $key $expire`
act=" -> $expire"
fi
echo "$key: $ttl$act"
done
done
@fieg

This comment has been minimized.

Owner

fieg commented Dec 2, 2015

@DevBrent

This comment has been minimized.

DevBrent commented May 27, 2017

640000 keys laitaire....

@jlnewton87

This comment has been minimized.

jlnewton87 commented Jan 31, 2018

This was a huge help...thanks for taking the time to put this together, and sharing it with the internet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment