#!/bin/bash
# 定义Redis服务器地址和端口
REDIS_HOST="localhost"
REDIS_PORT=6380
# 获取匹配键的数量
function get_matching_keys() {
local key_pattern="$1"
redis-cli -h $REDIS_HOST -p $REDIS_PORT -n 0 KEYS "$key_pattern"
}
# 删除匹配的键
function delete_matching_keys() {
local key_pattern="$1"
local keys=$(get_matching_keys "$key_pattern")
local keys_count=$(echo "$keys" | wc -l)
if [ "$keys_count" -eq 0 ]; then
echo "没有找到匹配的键: $key_pattern"
return
fi
# 统计待删除的总键数
total_keys_to_delete=$((total_keys_to_delete + keys_count))
keys_to_delete+="$keys "
}
# 定义匹配键的模式列表
KEY_PATTERNS=("saas_kline.*" "saas_depth.*" "saas_trade.*" "saas_ticker.*" "saas_depth20.*" "saas_mark_price.*")
# 初始化待删除键统计
total_keys_to_delete=0
keys_to_delete=""
# 逐个处理每个匹配模式
for pattern in "${KEY_PATTERNS[@]}"; do
delete_matching_keys "$pattern"
done
# 显示所有匹配的键和键的数量
echo "$keys_to_delete"
echo "找到 $total_keys_to_delete 个匹配的键:"
# 等待用户确认
echo -n "你确定要删除以上键吗?(y/n): "
read -r confirm_delete
if [ "$confirm_delete" == "y" ]; then
# 最终删除所有匹配的键
for key in $keys_to_delete; do
redis-cli -h $REDIS_HOST -p $REDIS_PORT -n 0 DEL "$key" > /dev/null
echo "正在删除 $key"
done
echo "已删除 $total_keys_to_delete 个键"
else
echo "操作已取消"
fi