Skip to content

Instantly share code, notes, and snippets.

@tjmehta
Forked from fieg/redis-expire.sh
Created July 8, 2018 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjmehta/09c3a01b47e3fea712fc952726cfa9af to your computer and use it in GitHub Desktop.
Save tjmehta/09c3a01b47e3fea712fc952726cfa9af to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment