Skip to content

Instantly share code, notes, and snippets.

@fieg
Last active October 10, 2022 23:09
Show Gist options
  • Save fieg/e3b74fe9bed6ec1f2a1c to your computer and use it in GitHub Desktop.
Save fieg/e3b74fe9bed6ec1f2a1c to your computer and use it in GitHub Desktop.

Revisions

  1. fieg revised this gist Dec 2, 2015. No changes.
  2. fieg revised this gist Dec 2, 2015. No changes.
  3. fieg created this gist Dec 2, 2015.
    36 changes: 36 additions & 0 deletions redis-expire.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/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