Get the App
SLTechnology News&Howtos  ›  Database  › 

The method of deleting neutralizing key in a rule in batch by redis

Shulou Source: shulou.com Published: 2022-05-31 23:08:39 09月24日 Update

This article mainly introduces redis batch delete some rules and key method, the article is very detailed, has a certain reference value, interested friends must read!

I. Foreword

In our work, we often encounter situations where we need to delete keys of certain rules in batches, such as cached course data "course-course uid", course uid is a variable, and we need to delete data like "course-*". However, although redis has commands keys that provide batch queries for this type of key, it does not provide commands to delete certain types of keys in batches.

II. Solutions

Let's see how we figure this out.

1. Enter the redis client first

cd redis directory/src./ redis-cli

2, initialization data, simulation data

127.0.0.1:6379> set course-1 1OK127.0.0.1:6379> set course-2 2OK127.0.0.1:6379> set course-3 3OK

3. You can see through the keys command that there are now three keys above

127.0.0.1:6379> keys course-*1) "course-3"2) "course-2"3) "course-1"

4. Exit redis client

127.0.0.1:6379> exit

5.1 Delete key locally

./ redis-cli keys "course-*" | xargs ./ redis-cli del

The three keys associated with course-* have been deleted.

Principle analysis:

First, execute the keys command through the redis client, fuzzy search out all the keys, and use the xargs command to use the previously queried key as the input of the del command in the following redis.

The result of the final execution can be understood as

(Learn video sharing: redis video tutorial)

1. Fuzzy query

keys "course-*"

Find the three keys above course-1 course-2 course-3

2. Delete key

del's three keys come from the previous keys query

del course-1 course-2 course-3

5.2 Remove key remotely.

Often when we develop, redis are public, maybe redis not local, we can delete them remotely through redis client.

./ redis-cli -h redis server ip -p port keys "course-*"| xargs ./ redis-cli -h redis server ip -p port del

III. Additional knowledge

1. Redis of a remote machine

The following example demonstrates how to connect to the redis service on host 127.0.0.1, port 6379, and password mypass.

redis-cli -h 127.0.0.1 -p 6379 -a "mypass"

2. xargs command

The xargs command is a filter for passing arguments to other commands and a tool for combining multiple commands.

The above is "redis batch delete a certain rule and key method" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Tags: Command query client client data rules three location port course service method content server knowledge article video face host value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Shulou Technology Shulou Tech Info Apple