redis gets keys according to regular

The keys of redis can obtain matching keys according to the regularity, for example, to find a certain type of sql, as follows to find the keys of the show type under the table name
keys db:table:[a-zA-Z_/d]*:show:*


It is also possible to delete based on the query structure:
redis-cli --raw keys "$PATTERN" | xargs redis-cli del


move
redis-cli --raw keys "$PATTERN" | xargs -L1 -I{} redis-cli move {} 1


First delete db0, and then move all data of db1 to db0
$ select 0
$ redis-cli flushdb
$ redis-cli --raw -n 1 keys "$PATTERN" | xargs -L1 -I{} redis-cli move {} 0


These commands can be written into shell scripts.
redis-delkeys.sh
#!/bin/sh
#
# Usage: ./redis-delkeys.sh [-h host] [-p port] [-n db] pattern
#
# Matches keys with the KEYS command matching pattern
#   and deletes them from the specified Redis DB.

set -e

HOST="localhost"
PORT="6379"
DB="0"
while getopts "h:p:n:" opt; do
    case $opt in
        h)  HOST=$OPTARG;;
        p)  PORT=$OPTARG;;
        n)  DB=$OPTARG;;
        \?) echo "invalid option: -$OPTARG" >&2; exit 1;;
    esac
done
shift $(( $OPTIND -1 ))
PATTERN="$@"
if [ -z "$PATTERN" ]; then
    echo "pattern required" >&2
    exit 2
be

redis-cli -h $HOST -p $PORT -n $DB --raw keys $PATTERN |
    xargs redis-cli -h $HOST -p $PORT -n $DB del


redis-movekeys.sh
#!/bin/sh
#
# Usage: ./redis-movekeys.sh [-h host] [-p port] [-n src] [-m dest] pattern
#
# Move keys matching pattern from the src Redis database to the
# dest Redis database.

set -e

HOST="localhost"
PORT="6379"
SRCDB="0"
DESTDB="0"
while getopts "h:p:n:m:" opt; do
    case $opt in
        h)  HOST=$OPTARG;;
        p)  PORT=$OPTARG;;
        n)  SRCDB=$OPTARG;;
        m)  DESTDB=$OPTARG;;
        \?) echo "invalid option: -$OPTARG" >&2; exit 1;;
    esac
done
shift $(( $OPTIND -1 ))
PATTERN="$@"
if [ -z "$PATTERN" ]; then
    echo "pattern required" >&2
    exit 2
be

redis-cli -h "$HOST" -p "$PORT" -n "$SRCDB" --raw keys "$PATTERN" |
    xargs -I{} redis-cli -h "$HOST" -p "$PORT" -n "$SRCDB" move {} "$DESTDB"


Reference:
https://coderwall.com/p/jw7uoa/quickly-move-and-delete-redis-keys-by-pattern

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326572120&siteId=291194637