1. Foreword
At work, we often encounter situations where we need to delete the keys of certain rules in batches, such as the cached course data "course-course uid", Course uid is a variable, we need to delete data similar to "course-*". However, although redis provides commands for batch querying keys of this type, it does not provide commands for batch deletion of certain types of keys.
Now I will sort out the solutions, I hope it can help everyone.
2. Solution
Let’s see how we solve it first.
1. First enter the redis client
cd redis所在目录/src ./redis-cli
2. Initialize data and simulate data
127.0.0.1:6379> set course-1 1 OK 127.0.0.1:6379> set course-2 2 OK 127.0.0.1:6379> set course-3 3 OK
3. You can see it through the keys command. Now there are three above key
127.0.0.1:6379> keys course-* 1) "course-3" 2) "course-2" 3) "course-1"
4. Exit the redis client
127.0.0.1:6379> exit
5.1. Local batch deletion of key
./redis-cli keys "course-*" | xargs ./redis-cli del
The 3 related keys of course-* have been deleted just now.
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 keys as the input of the subsequent redis del command.
The final execution result can be understood as
(Learning video sharing: redis video tutorial)
1. Fuzzy query
keys "course-*"
Query the three keys of course-1 course-2 course-3 above
2. Execute deletion key
The three keys of del come from the previous keys query
del course-1 course-2 course-3
5.2. Remote batch deletion of keys
Often when we develop, redis is public. Maybe redis is not local and we can delete it remotely through the redis client
./redis-cli -h redis所在服务器ip -p 端口 keys "course-*" |xargs ./redis-cli -h redis所在服务器ip -p 端口 del
3 , Supplementary knowledge
1. Redis of a remote machine
The following example demonstrates how to connect to the redis service with the host 127.0.0.1, the port 6379, and the password mypass.
redis-cli -h 127.0.0.1 -p 6379 -a "mypass"
2. xargs command
The xargs command is a filter for passing parameters to other commands, and it is also a tool for combining multiple commands. Detailed courseware http://man.linuxde.net/xargs
Related recommendations: redis database tutorial
The above is the detailed content of redis deletes keys of certain rules in batches. For more information, please follow other related articles on the PHP Chinese website!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

redis error就是redis数据库和其组合使用的部件出现错误,这个出现的错误有很多种,例如Redis被配置为保存数据库快照,但它不能持久化到硬盘,用来修改集合数据的命令不能用。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function