Keyword query method and device, nonvolatile storage medium and electronic device

By combining a first-in-first-out queue with Lua scripts and a second-level cache, the problem of low query efficiency and deadlock of cold data caused by cache penetration is solved, achieving efficient querying and stability.

CN117271589BActive Publication Date: 2026-04-14CHINA TELECOM CORP LTD
View PDF 3 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-09-22
Publication Date
2026-04-14

AI Technical Summary

Technical Problem

Existing methods for preventing cache breakdown have low query efficiency under high concurrency and are prone to deadlock and cold data issues.

Method used

A pre-defined query queue with a first-in-first-out (FIFO) approach is used, combined with Lua scripts and a second-level cache. By checking cache queries and query counts, duplicate query requests are avoided, thus preventing cache breakdown.

Benefits of technology

It improves query efficiency, avoids deadlock and cold data issues, and ensures system stability and response speed under high concurrency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117271589B_ABST
    Figure CN117271589B_ABST
Patent Text Reader

Abstract

The application discloses a keyword query method and device, a nonvolatile storage medium and an electronic device. The method comprises the following steps: obtaining a first query request of a preset query queue, wherein the preset query queue is a first-in first-out queue, and the first query request is used for indicating a first keyword; in response to the first query request, querying the first keyword in a first cache, wherein the first cache is used for storing at least one preset keyword obtained from a preset database in advance; in the case that the first keyword does not exist in the first cache, obtaining a query number of the preset database based on the first query request, wherein the query number of zero indicates that the preset database does not execute the first query request; in the case that the query number is zero, sending the first query request to the preset database, wherein the preset database returns the first keyword in response to the first query request. The application solves the technical problem of low query efficiency of the existing method for preventing cache breakage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data processing, and more specifically, to a keyword query method, apparatus, non-volatile storage medium, and electronic device. Background Technology

[0002] Cache breakdown occurs when a key is highly sought after and accessed frequently, resulting in concentrated high concurrency. When this key expires, a large number of requests bypass the cache and directly request the database, essentially creating a hole in a barrier. For example, if one million requests simultaneously access a hot key that is not in the cache, all one million requests will be redirected to the database, causing a massive surge in database traffic.

[0003] Currently, common methods to prevent cache breakdown include locking or setting permanent expiration times. These methods are prone to deadlocks and cold data issues, and are only suitable for single-service systems. For distributed scenarios, distributed locks are also required. The mainstream methods to prevent cache breakdown are mutex locks or setting hot keys to never expire, where the lock is applied to the hot key.

[0004] However, the above method has the following problems:

[0005] 1) In high-concurrency scenarios, only one request can acquire the lock resource for querying, while all other requests will be in a state of starvation for a long time until they acquire the lock resource and can obtain the hot key.

[0006] 2) If the cache is set to never expire for hot keys, in a read-heavy, write-light scenario, dirty data will never have a chance to be updated, and there will also be the problem of cold data. Data that has not been accessed for a period of time will continue to occupy memory space, resulting in wasted space.

[0007] Furthermore, query efficiency is affected when query requests are starved for a long time or when cold data is present. Therefore, existing methods to prevent cache breakdown suffer from low query efficiency.

[0008] There is currently no effective solution to the problem of low query efficiency in existing methods for preventing cache breakdown. Summary of the Invention

[0009] This invention provides a keyword query method, apparatus, non-volatile storage medium, and electronic device to at least solve the technical problem of low query efficiency in existing methods for preventing cache breakdown.

[0010] According to one aspect of the present invention, a keyword query method is provided, comprising: obtaining a first query request of a preset query queue, wherein the preset query queue is a first-in-first-out queue, the preset query queue includes a plurality of preset query requests, the first query request is the preset query request output by the preset query queue, the first query request being used to indicate querying a first keyword; responding to the first query request, querying the first keyword in a first cache, wherein the first cache is used to store at least one preset keyword pre-obtained from a preset database, the preset database being used to record a plurality of the preset keywords; if the first keyword is not present in the first cache, obtaining the number of queries to the preset database based on the first query request, wherein the number of queries being zero indicates that the preset database has not executed the first query request; if the number of queries is zero, sending the first query request to the preset database, wherein the preset database responds to the first query request by returning the first keyword.

[0011] Optionally, before the first query request in the preset query queue, the method further includes: obtaining at least one second query request, wherein the second query request is used to indicate a query for a second keyword; responding to the second query request by querying the second keyword in a second cache, wherein the second cache is used to store at least one preset keyword obtained in advance from a preset database, each preset keyword is used to respond to a corresponding preset query request, and the preset keyword responding to the second query request is the second keyword; and inputting the second query request for which the second keyword is not found in the second cache into the preset query queue.

[0012] Optionally, after obtaining the number of queries to the preset database based on the first query request, the method further includes: if the number of queries is not zero, responding to the first query request by querying the first keyword in a second cache, wherein the number of queries is not zero indicates that the preset database is executing the first query request; if the first keyword is not found in the second cache, inputting the first query request into the preset query queue.

[0013] Optionally, after receiving the first keyword returned by the preset database in response to the first query request, the method further includes: storing the first keyword returned by the preset database into a preset cache, wherein the preset cache includes the first cache and the second cache; and clearing the query count to zero.

[0014] Optionally, storing the first keyword returned by the preset database into a preset cache includes: determining the validity period of the first keyword, wherein the validity period is used to represent the storage period of the first keyword in the preset cache; and storing the first keyword into the preset cache according to the validity period.

[0015] Optionally, after querying the first keyword in the first cache in response to the first query request, the method further includes: if the first keyword exists in the first cache, obtaining the first keyword returned by the first cache in response to the first query request.

[0016] Optionally, after obtaining the number of queries to the preset database based on the first query request, the method further includes: if the number of queries is not zero, inputting the first query request into the preset query queue, wherein the number of queries being not zero indicates that the preset database is executing the first query request.

[0017] According to another aspect of the present invention, a keyword query device is also provided, comprising: an acquisition module, configured to acquire a first query request from a preset query queue, wherein the preset query queue is a first-in-first-out queue, the preset query queue includes a plurality of preset query requests, the first query request being the preset query request output by the preset query queue, the first query request being used to indicate a query for a first keyword; a query module, configured to respond to the first query request and query the first keyword in a first cache, wherein the first cache is used to store at least one preset keyword pre-acquired from a preset database, the preset database being used to record a plurality of the preset keywords; an acquisition module, configured to acquire the number of queries to the preset database based on the first query request when the first keyword is not present in the first cache, wherein a query count of zero indicates that the preset database has not executed the first query request; and a sending module, configured to send the first query request to the preset database when the query count is zero, wherein the preset database responds to the first query request and returns the first keyword.

[0018] According to another aspect of the present invention, a non-volatile storage medium is also provided, the non-volatile storage medium being used to store a program, wherein the program controls the device where the non-volatile storage medium is located to execute the above-described keyword query method during runtime.

[0019] According to another aspect of the present invention, an electronic device is also provided, including: a memory and a processor, the processor being configured to run a program stored in the processor, wherein the program executes the above-described keyword query method when it runs.

[0020] In this embodiment of the invention, since the preset query queue is a first-in-first-out queue, the input first query request needs to be sorted by the queue before being sent to the preset database for execution. Therefore, there is a waiting time during the transmission of each first query request from the preset query queue to the preset database. If the same first query request has already been sent to the preset database during this waiting time, repeatedly sending the first query request will increase the unnecessary load on the preset database. Therefore, after the first query request is output from the preset query queue and before being sent to the preset database, it is necessary to first query the first cache to determine whether the first cache has received the first keyword returned by the preset database based on the same first query request during the waiting time. Then, in the... If the first keyword is not present in the cache, the query count needs to be checked to determine if the same first query request has already been sent to the preset database to query for the first keyword. This avoids sending the same first query request to the preset database repeatedly. Therefore, after the first query request is output from the preset query queue, it will only be sent to the preset database if the first keyword is not stored in the first cache and the same first query request has not been sent to the preset database. This avoids the preset database repeatedly obtaining the same first query request, thus achieving the purpose of preventing cache breakdown and improving query efficiency. This solves the problem of low query efficiency in existing cache breakdown prevention methods. Attached Figure Description

[0021] The accompanying drawings, which are included to provide a further understanding of the invention and form part of this application, illustrate exemplary embodiments of the invention and, together with their description, serve to explain the invention and do not constitute an undue limitation thereof. In the drawings:

[0022] Figure 1 This is a flowchart of a keyword query method according to an embodiment of the present invention;

[0023] Figure 2 This is a schematic diagram of a method for preventing cache breakdown according to an embodiment of the present invention;

[0024] Figure 3 This is a schematic diagram of a keyword search device according to an embodiment of the present invention;

[0025] Figure 4 This is a structural block diagram of a computer terminal according to an embodiment of the present invention. Detailed Implementation

[0026] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.

[0027] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0028] For ease of description, the following explains some of the nouns or terms used in the embodiments of this application:

[0029] Cache penetration refers to a situation where, when a user executes a query or request on a web application or website, if the requested data is not in the cache, the caching system sends a request to the backend service to retrieve the data required for the request. If the website suddenly requests a large number of data points from the same key at this time, a large number of requests will instantly penetrate the cache and directly fall into the backend query logic, causing a surge in system load.

[0030] According to an embodiment of the present invention, a keyword query method embodiment is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.

[0031] Figure 1 This is a flowchart of a keyword search method according to an embodiment of the present invention, such as... Figure 1 As shown, the method includes the following steps:

[0032] Step S102: Obtain the first query request of the preset query queue, wherein the preset query queue is a first-in-first-out queue, the preset query queue includes multiple preset query requests, the first query request is the preset query request output by the preset query queue, and the first query request is used to indicate the query for the first keyword;

[0033] Step S104: In response to the first query request, query the first keyword in the first cache, wherein the first cache is used to store at least one preset keyword obtained in advance from the preset database, and the preset database is used to record multiple preset keywords;

[0034] Step S106: If the first keyword does not exist in the first cache, obtain the number of queries to the preset database based on the first query request, wherein a query count of zero indicates that the preset database has not executed the first query request;

[0035] Step S108: If the number of queries is zero, a first query request is sent to a preset database, wherein the preset database responds to the first query request by returning the first keyword.

[0036] In this embodiment of the invention, since the preset query queue is a first-in-first-out queue, the first query request needs to be sorted by the queue before being sent to the preset database for execution. Therefore, there is a waiting time during the transmission of each first query request from the preset query queue to the preset database. If the same first query request has already been sent to the preset database during this waiting time, repeatedly sending the first query request will increase the unnecessary load on the preset database. Therefore, after the first query request is output from the preset query queue and before being sent to the preset database, it is necessary to first query the first cache to determine whether the first cache has received the first keyword returned by the preset database based on the same first query request during the waiting time. Even if the first keyword is not present in the cache, the query count still needs to be checked to determine if the same first query request has already been sent to the preset database to query for the first keyword. This avoids sending the same first query request to the preset database repeatedly. Therefore, after the first query request is output from the preset query queue, it will only be sent to the preset database if the first keyword is not stored in the first cache and the same first query request has not been sent to the preset database. This avoids the preset database repeatedly obtaining the same first query request, thus achieving the goal of preventing cache breakdown and improving query efficiency. This solves the problem of low query efficiency in existing methods for preventing cache breakdown.

[0037] Alternatively, the keyword search method described above can be executed using Lua scripts.

[0038] It should be noted that LUA scripting is a lightweight scripting language written in standard C and open-source. Its design purpose is to be embedded in applications, thereby providing flexible extension and customization capabilities.

[0039] In step S102 above, the first keyword can be a hot topic key.

[0040] In step S102 above, the preset query queue can be a first-in-first-out queue.

[0041] In the above embodiments of the present invention, a first keyword can be queried in a preset database based on a preset query queue. Since the preset query queue is a first-in-first-out queue, the preset database can respond to each first query request one by one according to the queue order, avoiding cache breakdown caused by multiple first query requests being sent to the preset database at the same time.

[0042] Optionally, the default database can be a Redis database, which can be a non-relational database written in C.

[0043] It's worth noting that Redis offers a very rich command set and provides Lua script support. Users can send Lua scripts to Redis to execute custom actions and retrieve response data. Redis executes Lua scripts atomically in a single thread, ensuring that the script processing is not interrupted by any other requests.

[0044] Alternatively, using Lua scripts can reduce network overhead; by sending multiple requests at once via scripts, network latency can be reduced.

[0045] Optionally, atomic operations in Lua scripts are possible. Redis executes the entire Lua script as a single unit, without interruption by other requests. Therefore, there's no need to worry about race conditions during Lua script execution, and transactions are unnecessary.

[0046] Optionally, Lua scripts can be reused. Lua scripts sent by clients are permanently stored in Redis, allowing other clients to reuse them without having to implement the same logic in their own code.

[0047] Optionally, Lua scripts are embeddable, allowing them to be embedded in multiple programming languages ​​such as JAVA and C#, and supporting cross-platform interaction across different operating systems.

[0048] In step S104 above, the first cache can be a cache space dedicated to Lua scripts, used to store the first keyword returned by the preset database.

[0049] Optionally, before sending the first query request to the preset database via Lua script, a query for the first keyword can be performed in the first cache to determine whether the preset database has already returned the first keyword based on the same first query request within the waiting time of the first query request in the preset query queue.

[0050] In step S106 above, the number of queries represents the number of first query requests sent to the preset database. If the number is greater than 0, it means that the first query request has been sent to the preset database; if the number is equal to 0, it means that the first query request has not been sent to the preset database. Thus, before sending the first query request to the preset database, the execution status of the preset database for the first query request can be known through the number of queries, so as to avoid the situation where the preset database responds to multiple first query requests at the same time, which would reduce the query efficiency and improve the response efficiency of the preset database to the first query request.

[0051] Optionally, to avoid cache breakdown, there is usually only one first query request sent to the preset database, so the number of queries can be 0 or 1.

[0052] Optionally, to ensure the efficiency of the preset database in responding to the first query request, different first query requests can be sent to the preset database one by one. For example, a first query request can only be sent again after the previous first query request has been sent to the preset database and feedback has been received from the preset database, thereby ensuring that the preset database only needs to respond to a maximum of one first query request. In this case, the number of queries can represent the total number of various first query requests sent to the preset database.

[0053] It should be noted that different first query requests indicate queries for different first keywords. When the query count represents the total number of multiple first query requests, if a first query request of the first type has been sent and no feedback has been received from the preset database, then the next first query request of the second type will only be sent after the feedback from the preset database has been received.

[0054] Optionally, the query count can represent the number of identical first query requests. For example, if the query count represents different first query requests, the preset database can receive different first query requests sequentially. Since the first query requests received by the preset database are not duplicated, the number of first query requests received by the preset database will not be too high, ensuring that the preset database can operate efficiently.

[0055] It's important to note that Redis cannot connect to a pre-defined database within a Lua script, thus preventing database queries from being processed atomically. By leaving traces within the script and clearing them outside the script, hot keys do not require permanent expiration.

[0056] Optionally, the database query trace is retained within the Lua script: when the query in the Lua script misses the first cache, it is checked whether the current query count is 0. If it is, it means that there is no request to query the preset database, so the count is incremented by 1 and the Lua script is exited to query the database.

[0057] Optionally, clear the query count outside the Lua script: After querying the preset database outside the Lua script, store the query results in the first cache and set an expiration time, then clear the query count to zero.

[0058] In this way, after the hot key (i.e. the first keyword) expires, the first query request received again will query the preset database. Other identical first requests will find that the query count is 1 and will not query the first database. This prevents cache breakdown and eliminates the need to set a permanent expiration period.

[0059] In step S108 above, after sending the first query request to the preset database, you can return to step S102 to obtain the next first query request in the preset query queue and perform the query again.

[0060] As an optional embodiment, before the first query request in the preset query queue, the method further includes: obtaining at least one second query request, wherein the second query request is used to indicate querying a second keyword; responding to the second query request, querying the second keyword in a second cache, wherein the second cache is used to store at least one preset keyword obtained in advance from a preset database, each preset keyword is used to respond to a corresponding preset query request, and the preset keyword responding to the second query request is the second keyword; and inputting the second query request for which the second keyword is not found in the second cache into the preset query queue.

[0061] In the above embodiments of the present invention, before querying via Lua script, a conventional query can be performed through a second cache, and then a Lua script query can be performed on the content that cannot be queried based on the second cache, thereby reducing the number of Lua script executions and improving the overall query efficiency.

[0062] Optionally, the second query request can be performed in parallel in the second cache, which can improve query efficiency.

[0063] It should be noted that the first query request and the second query request can both indicate the retrieval of the same keyword key. The query in the second cache is the second query request, while the query in the first cache and the preset database via Lua script is the first query request.

[0064] It should be noted that the first keyword and the second keyword can refer to the same keyword.

[0065] Optionally, the second keyword can be a trending keyword.

[0066] Optionally, Lua scripts can completely replace distributed locks in processing the steps of distributed lock judgment, query, and storage into atomic operations, without causing deadlock issues. Furthermore, if multiple threads are in a starved state for a certain period, Lua scripts will report a busy exception to notify the threads, preventing them from remaining starved for extended periods and causing a poor user experience.

[0067] However, Redis's execution of Lua scripts has a certain drawback. When the Lua script reports a busy exception to notify the thread, the thread's original execution flow will terminate, meaning the process ends before the hot key (such as the first or second keyword) is retrieved. This drawback forces the client to send another request to query the hot key. To solve this drawback, this application adds a second-level cache as a transition.

[0068] Optionally, when querying hot keywords (such as the first keyword or the second keyword), the second-level cache L2 (i.e., the second cache) is queried first instead of L1 (i.e., the first cache). When the Lua script processes atomic operations, the first step is to query the L1 cache (i.e., the first cache).

[0069] Optionally, when the Lua script reports a busy exception to notify the thread, it queries the second-level cache again instead of ending directly. If the L2 cache is hit, the query result is returned; otherwise, it returns to the script execution queue to prevent the client from sending another request to query the hot key.

[0070] As an optional embodiment, after obtaining the number of queries to the preset database based on the first query request, the method further includes: if the number of queries is not zero, responding to the first query request by querying the first keyword in the second cache, wherein a non-zero number of queries indicates that the preset database is executing the first query request; if the first keyword is not found in the second cache, inputting the first query request into the preset query queue.

[0071] In the above embodiments of the present invention, when the number of queries is not zero, it indicates that the preset database is executing the first query request. Therefore, the preset database may have already responded to the same first query request. If the preset database has already responded to the same first query request, the first keyword returned by the preset database may have been stored in the second cache. Therefore, the first keyword can be quickly queried through the second cache. Since the second cache does not need to be queried through a first-in-first-out queue, the query result can be obtained quickly. If the second cache cannot obtain the query result, it means that the preset database is executing the same first query request but has not yet responded. Therefore, the first query request is input into the preset query queue again. During the waiting period of the first keyword in the preset query queue, the preset database is sufficient to respond to the first query request that is being executed. Therefore, after the first keyword is output from the preset query queue, the first cache may have already stored the first keyword returned by the preset database. The query for the first keyword can then be completed through the first cache.

[0072] As an optional embodiment, after receiving the first keyword returned by the preset database in response to the first query request, the method further includes: storing the first keyword returned by the preset database into a preset cache, wherein the preset cache includes a first cache and a second cache; and clearing the query count to zero.

[0073] In the above embodiments of the present invention, after the first keyword returned by the preset database, the first keyword returned by the preset database can be stored in the first cache and the second cache. Subsequent query requests can be quickly completed through the second cache. Query requests that have entered the preset query queue can be completed in the first cache after being output from the preset query queue. Since the first keyword returned by the preset database has been stored in the first cache and the second cache, the step of sending the first query request to the preset database will not be performed during the query process for this keyword. Therefore, it is not necessary to judge the number of queries. So after storing the first keyword returned by the preset database in the preset cache, the number of queries can be cleared to zero until the first keyword cannot be found in the first cache and the second cache. Since the number of queries has been cleared to zero, the first query request can be sent to the preset database according to the process.

[0074] As an optional embodiment, storing the first keyword returned by the preset database into the preset cache includes: determining the validity period of the first keyword, wherein the validity period is used to represent the storage period of the first keyword in the preset cache; and storing the first keyword into the preset cache according to the validity period.

[0075] In the above embodiments of the present invention, an expiration date is set for the first keyword in the preset cache. After the expiration date, the first keyword can be automatically deleted from the preset cache to avoid cold data in the preset cache.

[0076] As an optional embodiment, after responding to the first query request and querying the first keyword in the first cache, the method further includes: if the first keyword exists in the first cache, obtaining the first keyword returned by the first cache in response to the first query request.

[0077] In the above embodiments of the present invention, when the first keyword exists in the first cache, the first cache can respond to the first query request, quickly realize the query for the first keyword, and avoid repeatedly sending the same first query request to the preset database.

[0078] As an optional embodiment, after obtaining the number of queries to the preset database based on the first query request, the method further includes: if the number of queries is not zero, inputting the first query request into a preset query queue, wherein a non-zero number of queries indicates that the preset database is executing the first query request.

[0079] In the above embodiments of the present invention, when the number of queries is not zero, it indicates that the preset database is executing the first query request. The first query request is then input into the preset query queue again. During the waiting period of the first keyword in the preset query queue, the preset database has enough time to respond to the first query request being executed. Therefore, after the first keyword is output from the preset query queue, the first cache may have already stored the first keyword returned by the preset database, and the query for the first keyword can be completed through the first cache.

[0080] Figure 2 This is a schematic diagram of a method for preventing cache breakdown according to an embodiment of the present invention, as shown below. Figure 2 As shown, when millions of concurrent requests query a hot key, the steps include the following:

[0081] Step 1a: Check if the hot key (e.g., the second keyword) exists in the L2 cache (e.g., the second cache); if it is found, proceed to step 7a; otherwise, proceed to step 2a.

[0082] Step 2a: If step 1a finds that the hot key is not in the L2 cache (such as the second cache), then pass it to a Lua script and have Redis execute the Lua script.

[0083] Step 3a: If the Lua script in step 2a returns true, then execute step 4a; if it returns a hot key (such as the first keyword), it means that the value exists in the L1 cache (such as the first cache), then execute step 7a; if it returns false or Redis reports a busy exception, then execute step 1a.

[0084] Step 4a: Query the preset database for the hot topic key (such as the first keyword).

[0085] Step 5a: Store the query results (i.e. hot keywords, such as the first keyword or the second keyword) in both L1 and L2 caches (i.e., store both the first cache and the second cache at the same time).

[0086] Step 6a: Clear the query count of hot keys in the cache to zero.

[0087] Step 7a returns the query results for the hot keywords (such as the first keyword or the second keyword), and the step ends.

[0088] As an optional embodiment, such as Figure 2 As shown, the Lua script in step 2a includes the following steps:

[0089] Step 1b: Query whether the hot key (e.g., the first keyword) exists in the L1 cache (e.g., the first cache).

[0090] Step 2b: If step 1b finds that the hot key (e.g., the first keyword) is not in the L1 cache (e.g., the first cache), then proceed to step 3b.

[0091] Step 3b: Determine if the number of queries for the hot key (such as the first keyword) is 0.

[0092] Step 4b: If step 3b finds that the number of queries is 0, it means that no query has been performed in the preset database. Increment the number of queries for the hot key (such as the first keyword) by 1, and then proceed to step 5b.

[0093] In step 5b, the Lua script returns true, and the script execution ends; proceed to step 4a.

[0094] Step 6b: If step 3b finds that the number of queries is not 0, it means that a request has already been made to query the preset database. In this case, the Lua script returns false and proceeds to step 1a.

[0095] Step 7b: If step 1b finds that the hot key (such as the first keyword) exists in the L1 cache (such as the first cache), then proceed to step 7a.

[0096] As an optional example, to make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions in the embodiments of the present invention are described clearly and completely below; taking the simulation of 1 million requests to query a hot key (2SB6uecg9B) using JMeter as an example:

[0097] Step 1c: Query whether the hot key (2SB6uecg9B) exists in the L2 cache (such as the second cache).

[0098] If step 1c finds that 2SB6uecg9B is not in the L2 cache (such as the second cache), then pass it to the Lua script and have Redis execute the Lua script.

[0099] If step 3c is the first thread querying the hot key (2SB6uecg9B), and it does not exist in the L1 cache (such as the second cache) and the query count (2SB6uecg9B_count) is 0, then the query count is incremented by 1 (incrby command +1), and the Lua script returns true(1), and step 4c is executed; if it is not the first thread querying 2SB6uecg9B, and the query count (2SB6uecg9B_count) is not 0 or Redis reports a busy exception, then the Lua script returns false(0), and step 1c is executed.

[0100] Step 4c queries 2SB6uecg9B in the preset database.

[0101] Step 5c stores the query results in L1 and L2 caches (such as the first cache and the second cache).

[0102] Step 6c resets the query count of 2SB6uecg9B to zero (decrby command decrements by 1).

[0103] Step 7c returns the query result 2SB6uecg9B, and the step ends.

[0104] If step 1a finds that 2SB6uecg9B exists in the L2 cache, then return the query result and the step ends.

[0105] Optionally, after the first thread enters the Lua script, it successfully queries the preset database:

[0106] "E:\software\redis>redis-cli-a 123456--eval control.lua2SB6uecg9B2SB6uecg9B_count(intger)1

[0107] E:\software\redis>".

[0108] Optionally,

[0109] After a non-first thread enters the Lua script, it enters the following... Figure 2 Branch number 13 shown:

[0110] "E:\software\redis>redis-cli-a 123456--eval control.lua2SB6uecg9B2SB6uecg9B_count(intger)0

[0111] E:\software\redis>".

[0112] Optionally, if a thread other than the first receives an exception notification after waiting for the Lua script, it may enter a state such as... Figure 2 Branch number 13 shown:

[0113] "E:\software\redis>redis-cli-a 123456--eval control.lua2SB6uecg9B2SB6uecg9B_count(error)BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.

[0114] E:\software\redis>".

[0115] Optionally, the Lua script executed in the above steps is as follows:

[0116]

[0117] The above embodiments of this application utilize script-based trace retention and script-based zeroing. When Lua scripts cannot connect to MySQL (such as the default database), this method eliminates the need to set a permanent expiration date for hot keys, effectively solving the cold data problem.

[0118] In order to solve the busy exception defect of Redis executing Lua scripts, the above embodiments of this application introduce a two-level cache (such as a first cache and a second cache) to solve the problem of resending a query request after the hot key is not found, and at the same time improve the efficiency of querying hot keys.

[0119] Compared with distributed locks, the advantages of introducing Lua scripts in the above embodiments of this application are that there is no need to request and release lock resources, and deadlock problems will not occur.

[0120] In summary, the technical solution provided in this application can prevent cache breakdown and avoid requests being starved for a long time under high concurrency, thus avoiding deadlock problems. Furthermore, it eliminates the need to set permanent expiration for hot keys, preventing cache breakdown while also avoiding cold data issues in read-heavy, write-light scenarios.

[0121] According to an embodiment of the present invention, a keyword query device embodiment is also provided. It should be noted that the keyword query device can be used to execute the keyword query method in the embodiment of the present invention, and the keyword query method in the embodiment of the present invention can be executed in the keyword query device.

[0122] Figure 3 This is a schematic diagram of a keyword search device according to an embodiment of the present invention, such as... Figure 3 As shown, the device may include: an acquisition module 31, configured to acquire a first query request from a preset query queue, wherein the preset query queue is a first-in-first-out queue, the preset query queue includes multiple preset query requests, the first query request is a preset query request output by the preset query queue, and the first query request is used to indicate the query for a first keyword; a query module 33, configured to respond to the first query request and query the first keyword in a first cache, wherein the first cache is used to store at least one preset keyword pre-acquired from a preset database, and the preset database is used to record multiple preset keywords; an acquisition module 35, configured to acquire the number of queries to the preset database based on the first query request when the first keyword is not present in the first cache, wherein a query count of zero indicates that the preset database has not executed the first query request; and a sending module 37, configured to send the first query request to the preset database when the query count is zero, wherein the preset database responds to the first query request and returns the first keyword.

[0123] It should be noted that the acquisition module 31 in this embodiment can be used to execute step S102 in this application embodiment, the query module 33 in this embodiment can be used to execute step S104 in this application embodiment, the acquisition module 35 in this embodiment can be used to execute step S106 in this application embodiment, and the sending module 37 in this embodiment can be used to execute step S108 in this application embodiment. The examples and application scenarios implemented by the above modules and corresponding steps are the same, but are not limited to the content disclosed in the above embodiments.

[0124] In this embodiment of the invention, since the preset query queue is a first-in-first-out queue, the first query request needs to be sorted by the queue before being sent to the preset database for execution. Therefore, there is a waiting time during the transmission of each first query request from the preset query queue to the preset database. If the same first query request has already been sent to the preset database during this waiting time, repeatedly sending the first query request will increase the unnecessary load on the preset database. Therefore, after the first query request is output from the preset query queue and before being sent to the preset database, it is necessary to first query the first cache to determine whether the first cache has received the first keyword returned by the preset database based on the same first query request during the waiting time. Even if the first keyword is not present in the cache, the query count still needs to be checked to determine if the same first query request has already been sent to the preset database to query for the first keyword. This avoids sending the same first query request to the preset database repeatedly. Therefore, after the first query request is output from the preset query queue, it will only be sent to the preset database if the first keyword is not stored in the first cache and the same first query request has not been sent to the preset database. This avoids the preset database repeatedly obtaining the same first query request, thus achieving the goal of preventing cache breakdown and improving query efficiency. This solves the problem of low query efficiency in existing methods for preventing cache breakdown.

[0125] As an optional embodiment, the apparatus further includes: an acquisition submodule, configured to acquire at least one second query request before a first query request in a preset query queue, wherein the second query request is used to indicate a query for a second keyword; a first query submodule, configured to respond to the second query request by querying the second keyword in a second cache, wherein the second cache is used to store at least one preset keyword pre-acquired from a preset database, each preset keyword is used to respond to a corresponding preset query request, and the preset keyword responding to the second query request is the second keyword; and a first queue submodule, configured to input second query requests for which the second keyword is not found in the second cache into the preset query queue.

[0126] As an optional embodiment, the device further includes: a second query submodule, configured to, after obtaining the number of queries to the preset database based on the first query request, query the first keyword in the second cache in response to the first query request if the number of queries is not zero, wherein a non-zero number of queries indicates that the preset database is executing the first query request; and a second queue submodule, configured to input the first query request into the preset query queue if the first keyword is not found in the second cache.

[0127] As an optional embodiment, the device further includes: a caching submodule, configured to store the first keyword returned by the preset database into a preset cache after receiving the first keyword returned by the preset database in response to the first query request, wherein the preset cache includes a first cache and a second cache; and a counting submodule, configured to reset the query count to zero.

[0128] As an optional embodiment, the caching submodule includes: a determining unit, used to determine the validity period of the first keyword, wherein the validity period is used to represent the storage period of the first keyword in the preset cache; and a caching unit, used to store the first keyword into the preset cache according to the validity period.

[0129] As an optional embodiment, the apparatus further includes: an acquisition unit, configured to, after responding to a first query request and querying for a first keyword in a first cache, acquire the first keyword returned by the first cache in response to the first query request if the first keyword exists in the first cache.

[0130] As an optional embodiment, the apparatus further includes: a queue unit, configured to input the first query request into a preset query queue after obtaining the number of queries to the preset database based on the first query request, provided that the number of queries is not zero, wherein a non-zero number of queries indicates that the preset database is executing the first query request.

[0131] Embodiments of the present invention can provide a computer terminal, which can be any computer terminal device in a group of computer terminals. Optionally, in this embodiment, the computer terminal can also be replaced by a mobile terminal or other terminal device.

[0132] Optionally, in this embodiment, the computer terminal may be located in at least one of a plurality of network devices in a computer network.

[0133] In this embodiment, the computer terminal described above can execute the program code for the following steps in the keyword query method: obtaining a first query request from a preset query queue, wherein the preset query queue is a first-in-first-out queue, the preset query queue includes multiple preset query requests, the first query request is a preset query request output by the preset query queue, and the first query request is used to indicate querying a first keyword; responding to the first query request, querying the first keyword in a first cache, wherein the first cache is used to store at least one preset keyword obtained in advance from a preset database, and the preset database is used to record multiple preset keywords; if the first keyword is not in the first cache, obtaining the number of queries to the preset database based on the first query request, wherein a query count of zero indicates that the preset database has not executed the first query request; if the query count is zero, sending the first query request to the preset database, wherein the preset database responds to the first query request and returns the first keyword.

[0134] Optionally, Figure 4 This is a structural block diagram of a computer terminal according to an embodiment of the present invention. Figure 4 As shown, the computer terminal 40 may include one or more (only one is shown in the figure) processors 42 and memory 44.

[0135] The memory can be used to store software programs and modules, such as the program instructions / modules corresponding to the keyword query method and apparatus in this embodiment of the invention. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, thereby realizing the aforementioned keyword query method. The memory may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include memory remotely located relative to the processor, and these remote memories can be connected to the terminal 40 via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.

[0136] The processor can invoke information and application programs stored in memory via a transmission device to perform the following steps: obtaining a first query request from a preset query queue, wherein the preset query queue is a first-in-first-out queue, the preset query queue includes multiple preset query requests, the first query request is a preset query request output by the preset query queue, and the first query request is used to indicate a query for a first keyword; responding to the first query request, querying the first keyword in a first cache, wherein the first cache is used to store at least one preset keyword pre-obtained from a preset database, and the preset database is used to record multiple preset keywords; if the first keyword is not present in the first cache, obtaining the number of queries to the preset database based on the first query request, wherein a query count of zero indicates that the preset database has not executed the first query request; if the query count is zero, sending the first query request to the preset database, wherein the preset database responds to the first query request by returning the first keyword.

[0137] Optionally, the processor may also execute program code that performs the following steps: before a first query request in a preset query queue, obtain at least one second query request, wherein the second query request is used to indicate a query for a second keyword; in response to the second query request, query the second keyword in a second cache, wherein the second cache is used to store at least one preset keyword obtained in advance from a preset database, each preset keyword is used to respond to a corresponding preset query request, and the preset keyword responding to the second query request is the second keyword; and input the second query request for which the second keyword is not found in the second cache into the preset query queue.

[0138] Optionally, the processor may also execute program code that performs the following steps: after obtaining the number of queries to the preset database based on the first query request, if the number of queries is not zero, responding to the first query request by querying the first keyword in the second cache, wherein a non-zero number of queries indicates that the preset database is executing the first query request; if the first keyword is not found in the second cache, inputting the first query request into the preset query queue.

[0139] Optionally, the processor may also execute program code that performs the following steps: after receiving the first keyword returned by the preset database in response to the first query request, the first keyword returned by the preset database is stored in a preset cache, wherein the preset cache includes a first cache and a second cache; and the query count is cleared to zero.

[0140] Optionally, the processor may also execute program code that performs the following steps: determining the validity period of the first keyword, wherein the validity period is used to represent the storage period of the first keyword in the preset cache; and storing the first keyword in the preset cache according to the validity period.

[0141] Optionally, the processor may also execute program code that performs the following steps: after responding to the first query request and querying the first keyword in the first cache, if the first keyword exists in the first cache, retrieve the first keyword returned by the first cache in response to the first query request.

[0142] Optionally, the processor may also execute program code that performs the following steps: after obtaining the number of queries to the preset database based on the first query request, if the number of queries is not zero, input the first query request into the preset query queue, wherein a non-zero number of queries indicates that the preset database is executing the first query request.

[0143] This invention provides a keyword query scheme. Since the preset query queue is a first-in-first-out queue, the first query request needs to be sorted by the queue before being sent to the preset database for execution. Therefore, each first query request experiences a waiting time during transmission from the preset query queue to the preset database. If the same first query request has already been sent to the preset database during this waiting time, repeatedly sending the first query request will increase the unnecessary load on the preset database. Therefore, after the first query request is output from the preset query queue and before being sent to the preset database, it needs to be queried in the first cache to determine whether the first keyword returned by the preset database based on the same first query request has been received within the waiting time. If so, it is not stored in the first cache. In the case of the first keyword, it is also necessary to check the number of queries to determine whether the same first query request has been sent to the preset database to query the first keyword. This avoids sending the same first query request to the preset database repeatedly. Therefore, after the first query request is output from the preset query queue, it will only be sent to the preset database if the first keyword is not stored in the first cache and the same first query request has not been sent to the preset database. This avoids the preset database from repeatedly obtaining the same first query request, thus achieving the purpose of preventing cache breakdown and improving query efficiency. This solves the problem of low query efficiency in existing cache breakdown prevention methods.

[0144] Those skilled in the art will understand that Figure 4 The structure shown is for illustrative purposes only. The computer terminal can also be a smartphone (such as an Android phone, an iOS phone, etc.), a tablet computer, a mobile internet device (MID), a PAD, and other terminal devices. Figure 4 This does not limit the structure of the aforementioned electronic device. For example, computer terminal 40 may also include components that are more... Figure 4 The more or fewer components shown (such as network interfaces, display devices, etc.), or having the same Figure 4 The different configurations shown.

[0145] Those skilled in the art will understand that all or part of the steps in the various methods of the above embodiments can be implemented by a program instructing the hardware related to the terminal device. The program can be stored in a non-volatile medium, which may include: flash drive, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc.

[0146] Embodiments of the present invention also provide a non-volatile storage medium. Optionally, in this embodiment, the aforementioned non-volatile storage medium can be used to store the program code executed by the keyword search method provided in the above embodiments.

[0147] Optionally, in this embodiment, the non-volatile storage medium may be located in any computer terminal in a group of computer terminals in a computer network, or in any mobile terminal in a group of mobile terminals.

[0148] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: obtaining a first query request from a preset query queue, wherein the preset query queue is a first-in-first-out queue, the preset query queue includes multiple preset query requests, the first query request is a preset query request output by the preset query queue, and the first query request is used to indicate querying a first keyword; responding to the first query request, querying the first keyword in a first cache, wherein the first cache is used to store at least one preset keyword obtained in advance from a preset database, and the preset database is used to record multiple preset keywords; if the first keyword is not in the first cache, obtaining the number of queries to the preset database based on the first query request, wherein a query count of zero indicates that the preset database has not executed the first query request; if the query count is zero, sending the first query request to the preset database, wherein the preset database responds to the first query request and returns the first keyword.

[0149] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: obtaining at least one second query request before a first query request in a preset query queue, wherein the second query request is used to indicate querying a second keyword; responding to the second query request, querying the second keyword in a second cache, wherein the second cache is used to store at least one preset keyword obtained in advance from a preset database, each preset keyword is used to respond to a corresponding preset query request, and the preset keyword responding to the second query request is the second keyword; and inputting a second query request for which the second keyword is not found in the second cache into the preset query queue.

[0150] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: after obtaining the number of queries to the preset database based on the first query request, if the number of queries is not zero, responding to the first query request by querying the first keyword in the second cache, wherein a non-zero number of queries indicates that the preset database is executing the first query request; if the first keyword is not found in the second cache, the first query request is input into the preset query queue.

[0151] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: after receiving the first keyword returned by the preset database in response to the first query request, the first keyword returned by the preset database is stored in a preset cache, wherein the preset cache includes a first cache and a second cache; the query count is cleared to zero.

[0152] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: determining the validity period of the first keyword, wherein the validity period is used to represent the storage period of the first keyword in the preset cache; and storing the first keyword in the preset cache according to the validity period.

[0153] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: after querying the first keyword in the first cache in response to the first query request, if the first keyword exists in the first cache, obtain the first keyword returned by the first cache in response to the first query request.

[0154] Optionally, in this embodiment, the non-volatile storage medium is configured to store program code for performing the following steps: after obtaining the number of queries to the preset database based on the first query request, if the number of queries is not zero, the first query request is input into the preset query queue, wherein a non-zero number of queries indicates that the preset database is executing the first query request.

[0155] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.

[0156] In the above embodiments of the present invention, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.

[0157] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units can be a logical functional division, and in actual implementation, there may be other division methods. For instance, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual coupling, direct coupling, or communication connection may be through some interfaces; the indirect coupling or communication connection between units or modules may be electrical or other forms.

[0158] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0159] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0160] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a non-volatile storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a non-volatile storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned non-volatile storage medium includes various media capable of storing program code, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.

[0161] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.

Claims

1. A keyword search method, characterized in that, include: Obtain the first query request of the preset query queue, wherein the preset query queue is a first-in-first-out queue, the preset query queue includes multiple preset query requests, the first query request is the preset query request output by the preset query queue, and the first query request is used to indicate the query for the first keyword; In response to the first query request, the first keyword is queried in the first cache, wherein the first cache is used to store at least one preset keyword obtained in advance from a preset database, and the preset database is used to record multiple preset keywords; If the first keyword is not present in the first cache, obtain the number of queries to the preset database based on the first query request, wherein a query count of zero indicates that the preset database did not execute the first query request; If the number of queries is zero, the first query request is sent to the preset database, wherein the preset database responds to the first query request by returning the first keyword; The method further includes the following steps before the first query request in the preset query queue: Obtain at least one second query request, wherein the second query request is used to indicate querying a second keyword; In response to the second query request, the second keyword is queried in the second cache, wherein the second cache is used to store at least one preset keyword obtained in advance from a preset database, each preset keyword is used to respond to the corresponding preset query request, and the preset keyword responding to the second query request is the second keyword; The second query request that does not find the second keyword in the second cache is entered into the preset query queue; The method further includes, after obtaining the number of queries to the preset database based on the first query request: If the number of queries is not zero, the first keyword is queried in the second cache in response to the first query request, wherein the number of queries is not zero, indicating that the preset database is executing the first query request; If the first keyword is not found in the second cache, the first query request is input into the preset query queue.

2. The method according to claim 1, characterized in that, After receiving the first keyword returned by the preset database in response to the first query request, the method further includes: The first keyword returned by the preset database is stored in a preset cache, wherein the preset cache includes the first cache and the second cache; Clear the query count to zero.

3. The method according to claim 2, characterized in that, Storing the first keyword returned by the preset database into the preset cache includes: Determine the validity period of the first keyword, wherein the validity period is used to represent the storage period of the first keyword in the preset cache; The first keyword is stored in the preset cache according to the validity period.

4. The method according to claim 1, characterized in that, After responding to the first query request and querying the first keyword in the first cache, the method further includes: If the first keyword exists in the first cache, retrieve the first keyword returned by the first cache in response to the first query request.

5. The method according to claim 1, characterized in that, After obtaining the number of queries to the preset database based on the first query request, the method further includes: If the number of queries is not zero, the first query request is input into the preset query queue, wherein the number of queries being not zero indicates that the preset database is executing the first query request.

6. A keyword search device, characterized in that, include: The acquisition module is used to acquire a first query request from a preset query queue, wherein the preset query queue is a first-in-first-out queue, the preset query queue includes multiple preset query requests, the first query request is the preset query request output by the preset query queue, and the first query request is used to indicate the query for a first keyword; A query module is used to respond to the first query request and query the first keyword in the first cache, wherein the first cache is used to store at least one preset keyword obtained in advance from a preset database, and the preset database is used to record multiple preset keywords; The acquisition module is used to acquire the number of queries to the preset database based on the first query request when the first keyword does not exist in the first cache, wherein the number of queries being zero indicates that the preset database has not executed the first query request; The sending module is configured to send the first query request to the preset database when the number of queries is zero, wherein the preset database responds to the first query request by returning the first keyword; The device further includes: The acquisition submodule is used to acquire at least one second query request before the first query request in the preset query queue, wherein the second query request is used to indicate the query for a second keyword; The first query submodule is used to respond to the second query request and query the second keyword in the second cache. The second cache is used to store at least one preset keyword obtained from a preset database in advance. Each preset keyword is used to respond to the corresponding preset query request. The preset keyword that responds to the second query request is the second keyword. The first queue submodule is used to input the second query request that does not find the second keyword in the second cache into the preset query queue; The device further includes: The second query submodule is used to, after obtaining the number of queries to the preset database based on the first query request, and if the number of queries is not zero, respond to the first query request by querying the first keyword in the second cache, wherein the number of queries is not zero indicates that the preset database is executing the first query request; The second queue submodule is used to input the first query request into the preset query queue if the first keyword cannot be found in the second cache.

7. A non-volatile storage medium, characterized in that, The non-volatile storage medium is used to store a program, wherein, when the program is running, the device where the non-volatile storage medium is located is controlled to execute the keyword query method according to any one of claims 1 to 5.

8. An electronic device, characterized in that, include: A memory and a processor, the processor being configured to run a program stored in the processor, wherein the program, when running, executes the keyword query method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Database query method and system

    CN105243072A

  • Database query method, query device and terminal

    CN110909025A

  • Method and device for optimizing cache

    CN113220981A