Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report