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

How to get all the keys in Redis

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to get all the keys in Redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In daily development, we sometimes have to traverse all the keys in Redis, as we have described in the previous article, we can use the keys command to get all the keys, so in Redis in addition to the keys command, we can also use the scan command to get. Let's take a closer look at the relevant contents of these two commands.

1. Full ergodic key

Keys pattern

We have already introduced it in the previous article, and you can use it directly.

Keys *

Command to traverse all the keys in Redis, then the keys command can actually support pattern matching. Let's demonstrate what has been said above through an example.

Let's take a look at the detailed description of the pattern wildcard:

-* means to match any character

-? Means to match a character.

-[] stands for matching some characters, for example, [1Jing 3] represents matching 1 and 3, and [1-10] represents any number that matches 1 to 10.

-x transfer character, for example, to match the asterisk, the question mark needs to be escaped

Let's move on to the relevant use cases.

The keys command in Redis is a very useful command. For example, if I want to delete a key at the beginning of a specified name, I can also use the keys command to find it and then delete it. The specific commands are as follows:

. / redis-cli keys J* | xargs. / redis-cli del

Note: when executing the above delete command, it is not necessary to execute it in the Redis client.

We know that there is a single schema in Redis, so sometimes blocking occurs when executing keys commands, so we should be careful when using keys commands. Let's take a look at the considerations for using the keys command.

Because the keys command is blocked, we use the keys command in a non-business client, so that even if the keys command blocks, it does not affect the related business.

For example, when the total number of keys in Redis is small, you can use the keys command directly.

If the total number of keys in Redis is large, and we have to get all the keys in a client in a business environment, such as a production client, then we can use the scan command because it does not block the client.

Let's take a look at the scan command in Redis.

two。 Progressive traversal

After version 2. 8, Redis provides the scan command, which can effectively solve the problem of blocking keys commands. However, unlike the keys command, the scan command traverses the keys in a progressive manner. And the time complexity of the scan command is O (1). So scan commands if you want to implement the functions of the keys command, you have to execute the scan command many times. Because the storage of keys in Redis actually uses hashtable data structures. So when we use the scan command, we can understand that we only get part of the dictionary, and if we want to get all the keys, we have to call the scan command multiple times.

Scan cursor [MATCH pattern] [COUNT count]

Let's describe the relevant parameters of the scan command in detail:

Cursor: required parameter. Cursor is a cursor parameter. The first traversal of the cursor starts at 0, and each scan traversal returns the value of the current cursor.

MATCH pattern: optional parameter, the same as the pattern parameter in keys.

COUNT count: optional parameter. The count parameter refers to the number of keys returned by the scan command at a time.

Let's take a look at an example of the scan command.

Just like the scan command executed above, the scan command is returned every time it is executed. The parameter of the last cursor, when the cursor parameter is 0, indicates that all the keys in the Redis have been traversed.

In addition to the scan command, Redis also provides hscan, sscan, zscan and other commands, all of which are used in the same way as the scan command.

Let's take a look at the considerations for the scan command.

Advantage: it can solve the problem of command blocking in keys commands.

Disadvantages: if you use the scan command, if the keys in the Redis have changed, such as: add, delete, modify and other operations, the scan command may encounter traversing not all the keys, which is also the key we use the scan command to traverse the key to pay special attention to.

This is the end of "how to get all the keys in Redis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report