Response Method and Device for Query Request, Storage Medium, and Electronic Device

By storing query timestamps in the cache library and judging the validity of key-value pairs based on them, the problem of not being able to accurately query data when the data value in the cache library is empty, and faster and more accurate data query is achieved.

CN114254001BActive Publication Date: 2025-06-27QINGDAO HAIER TECH +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111464893.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-02
Publication Date
2025-06-27
Estimated Expiration
2041-12-02

AI Technical Summary

Technical Problem

In the case where the data value is empty in the cache library, it may cause the accurate data to be unable to be accurately queried, and the use of a long cache validity period will cause the user to wait too long to obtain the accurate data.

Method used

By storing a timestamp in the cache library, the time of the last database query is indicated. When the requester initiates a query request, it checks whether there is a key-value pair corresponding to the keyword to be queried in the cache library. If it exists and the data value is a timestamp, the validity of the key-value pair is judged based on the timestamp. If the key-value pair is invalid, query the database to get the latest data and update the cache.

Benefits of technology

It effectively solves the problem that data cannot be accurately queried when the data value in the cache library is empty, avoids users waiting too long to obtain accurate data, and improves the response speed and accuracy of query requests.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114254001B_ABST
    Figure CN114254001B_ABST
Patent Text Reader

Abstract

The present invention discloses a caching method and device for key-value pairs, a storage medium, and an electronic device. Among them, the above method includes: when receiving a query request initiated by a requester, determining whether there is a first key-value pair corresponding to the keyword to be queried in the cache library, where the query request at least includes: the keyword to be queried; when there is a first key-value pair corresponding to the keyword to be queried in the cache library, determining whether the data value in the first key-value pair is a timestamp, where the timestamp is used to indicate the first query time when the data value corresponding to the keyword was last queried in the database; when the data value in the first key-value pair is a timestamp, determining whether the first key-value pair is valid according to the timestamp; when the first key-value pair is valid, returning a null value to the requester.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communications, and in particular, to a method and apparatus for responding to a query request, a storage medium, and an electronic device. Background Art

[0002] In background software services, it is generally necessary to query an external database to obtain data, which is then processed through business logic and returned to the requester. To reduce the response time of requests and improve the processing performance of background services, caching previously queried data in memory is a commonly used technical means. In this way, when the same request is encountered next time, there is no need to perform the time-consuming operation of querying the external database, but the corresponding data can be directly retrieved from the cache and returned.

[0003] When the data value corresponding to the keyword in the request data of the requester does not exist in the database (i.e., the data value is a null value), the key-value pair of the null value returned and the keyword corresponding to the keyword is cached. In this way, when the same request occurs next time, the previously cached null value can also be immediately returned to the requester to avoid cache breakdown.

[0004] However, the disadvantages of using cache null values to prevent cache breakdown are as follows:

[0005] 1. In some scenarios, the data is rarely changed. For example, the hourly energy consumption value of a Haier refrigerator hardly changes once it is calculated. Therefore, a relatively long cache validity period can be set, such as 1 day. This cache duration is appropriate for the generated data. That is, for the same request, starting from the time of the first query, queries within 1 day are directly retrieved from the cache without querying the database.

[0006] 2. If the above cache validity period is used to cache null values, users may need to wait too long to query accurate data due to the long cache time. Suppose the energy consumption data for the previous hour is calculated at 5 minutes past each hour, and the current time is 12:04 on a certain day. When a user queries the energy consumption data for 11 - 12 o'clock on the same day, since it is not yet 12:05 and the data has not been calculated at this time, a null value is returned and cached. Then the user has to wait until 12:05 the next day to query the energy consumption data for 11 - 12 o'clock yesterday. This cache effect is completely unacceptable for the user experience.

[0007] In view of the problems in the related art, such as the situation where the data value in the key-value pair in the cache library is a null value, it is very likely that accurate data cannot be accurately queried, no effective solution has been proposed yet. Summary of the Invention

[0008] An embodiment of the present invention provides a method and device for responding to a query request, a storage medium, and an electronic device, so as to at least solve the problem that in the related art, when the data value in the key-value pair in the cache library is a null value, it is very likely that accurate data cannot be accurately queried.

[0009] According to an embodiment of the present invention, a method for responding to a query request is provided, including: when receiving a query request initiated by a requester, determining whether there is a first key-value pair corresponding to a keyword to be queried in a cache library, where at least the query request includes: a keyword to be queried; when there is a first key-value pair corresponding to the keyword to be queried in the cache library, determining whether the data value in the first key-value pair is a timestamp, where the timestamp is used to indicate a first query time when the data value corresponding to the keyword was last queried in a database; when the data value in the first key-value pair is a timestamp, determining whether the first key-value pair is valid according to the timestamp; when the first key-value pair is valid, returning a null value to the requester.

[0010] In an exemplary embodiment, after determining whether there is a first key-value pair corresponding to a keyword to be queried in the cache library, the method further includes: when there is no first key-value pair corresponding to the keyword to be queried in the cache library, determining whether there is a second key-value pair corresponding to the keyword to be queried in the database; when there is no second key-value pair corresponding to the keyword to be queried in the database, caching the key-value pair of the keyword to be queried and a target timestamp into the cache library, where the target timestamp is used to indicate a second query time when the server queries the data value corresponding to the keyword to be queried in the database.

[0011] In an exemplary embodiment, caching the key-value pair of the keyword to be queried and a target timestamp into the cache library includes: determining a second query time when the server queries the data value corresponding to the keyword to be queried in the database; converting the second query time into a target timestamp according to a preset rule, and caching the key-value pair of the keyword to be queried and the target timestamp into the cache library, where the preset rule at least includes: multiplying the query time by a preset value.

[0012] In an exemplary embodiment, determining whether the first key-value pair is valid according to the timestamp includes: determining a time difference between the current time and the first query time corresponding to the timestamp, and determining a magnitude relationship between the time difference and a first valid duration; determining whether the first key-value pair is valid according to the magnitude relationship between the time difference and the first valid duration.

[0013] In an exemplary embodiment, determining whether the first key-value pair is valid according to the magnitude relationship between the time difference and the first effective duration includes: when the magnitude relationship indicates that the time difference is greater than the first effective duration, determining that the first key-value pair is invalid; when the magnitude relationship indicates that the time difference is less than the first effective duration, determining that the first key-value pair is valid.

[0014] In an exemplary embodiment, after determining whether the first key-value pair is valid according to the time stamp, the method further includes: when the first key-value pair is invalid, determining whether there is a second key-value pair corresponding to the keyword to be queried in the database; when there is a second key-value pair corresponding to the keyword to be queried in the database, returning the data value corresponding to the second key-value pair to the requester and caching the second key-value pair in the cache library.

[0015] Before receiving a query request initiated by a requester, the method further includes: receiving a target operation sent by a target object; setting a first effective duration and a second effective duration for caching key-value pairs in the cache library according to the target operation, where the first effective duration is the duration for which the key-value pair of the keyword and the time stamp is stored in the cache library; and the second effective duration is the duration for which the key-value pair of the keyword and the service data is stored in the cache library.

[0016] According to another embodiment of the embodiments of the present invention, there is also provided a query request response device, including: a first determination module, configured to determine whether there is a first key-value pair corresponding to the keyword to be queried in the cache library when receiving a query request initiated by a requester, where the query request at least includes: the keyword to be queried; a second determination module, configured to determine whether the data value in the first key-value pair is a time stamp when there is a first key-value pair corresponding to the keyword to be queried in the cache library, where the time stamp is used to indicate the time of the last query of the data value corresponding to the keyword in the database; a third determination module, configured to determine whether the first key-value pair is valid according to the time stamp when the data value in the first key-value pair is a time stamp; and a response module, configured to return a null value to the requester when the first key-value pair is valid.

[0017] According to yet another aspect of the embodiments of the present invention, there is also provided a computer-readable storage medium storing a computer program, where the computer program is configured to execute the above-mentioned query request response method when running.

[0018] According to another aspect of the embodiments of the present invention, an electronic device is further provided, including a memory, a processor, and a computer program stored on the memory and executable on the processor. Wherein, the processor executes the above response method of the query request through the computer program.

[0019] In the embodiments of the present invention, when a query request initiated by a requester is received, it is determined whether there is a first key-value pair corresponding to the keyword to be queried in the cache library. Wherein, the query request at least includes: the keyword to be queried; when there is a first key-value pair corresponding to the keyword to be queried in the cache library, it is determined whether the data value in the first key-value pair is a timestamp, where the timestamp is used to indicate the first query time when the data value corresponding to the keyword was last queried in the database; when the data value in the first key-value pair is a timestamp, it is determined whether the first key-value pair is valid according to the timestamp; when the first key-value pair is valid, a null value is returned to the requester. By adopting the above technical solution, the problems in the related art, such as when the data value in the key-value pair in the cache library is a null value, it is very likely that accurate data cannot be accurately queried, are solved. Furthermore, in the embodiments of the present invention, when the data value corresponding to the keyword in the query request is a null value, it is determined whether the first key-value pair is valid according to the timestamp, so that the target key-value pair returns a null value only when the invalid duration has not elapsed. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] The drawings described herein are used to provide a further understanding of the present invention, and constitute a part of this application. The schematic embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:

[0021] Figure 1 is a hardware structure block diagram of a computer terminal for a response method of a query request according to an embodiment of the present invention;

[0022] Figure 2 is a flowchart of a response method of a query request according to an embodiment of the present invention;

[0023] Figure 3 is a schematic diagram of a response method of a query request according to an embodiment of the present invention;

[0024] Figure 4 is a structural block diagram (one) of a response device for a query request according to an embodiment of the present invention;

[0025] Figure 5 is a structural block diagram (two) of a response device for a query request according to an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0026] To enable those skilled in the art to better understand the solution of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.

[0027] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and do not necessarily need to be used to describe a specific order or sequence. It should be understood that such data can be interchanged under appropriate circumstances, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device comprising a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.

[0028] The method embodiments provided in the embodiments of the present application can be executed on a mobile terminal, a computer terminal or a similar computing device. Taking running on a computer terminal as an example, Figure 1 is a hardware structure block diagram of a computer terminal for a method of responding to a query request according to an embodiment of the present invention. As Figure 1 shown, the computer terminal may include one or more ( Figure 1 only one is shown in the figure) processors 102 (the processor 102 may include, but is not limited to, a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data. In an exemplary embodiment, the above computer terminal may further include a transmission device 106 for communication functions and an input / output device 108. Those of ordinary skill in the art can understand that Figure 1 the structure shown is only illustrative and does not limit the structure of the above computer terminal. For example, the computer terminal may further include more or fewer components than those shown in Figure 1 , or have an equivalent function to that shown in Figure 1 or a different configuration with more functions than that shown in Figure 1 .

[0029] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the query request response method in the embodiments of the present invention. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, implements the above-mentioned method. The memory 104 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 memories. In some instances, the memory 104 may further include a memory remotely disposed relative to the processor 102, and these remote memories can be connected to the computer terminal through a network. Examples of the above network include but are not limited to the Internet, intranet, local area network, mobile communication network, and combinations thereof.

[0030] The transmission device 106 is used to receive or send data via a network. Specific examples of the above network may include a wireless network provided by a communication provider of the computer terminal. In one instance, the transmission device 106 includes a network adapter (abbreviated as NIC), which can be connected to other network devices through a base station and thus communicate with the Internet. In one instance, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0031] In this embodiment, a method for responding to a query request is provided, which is applied to the above computer terminal. Figure 2 It is a flowchart of the method for responding to a query request according to the embodiments of the present invention, and the process includes the following steps:

[0032] Step S202, when receiving a query request initiated by a requester, determine whether there is a first key-value pair corresponding to the keyword to be queried in the cache library, where at least the following is included in the query request: the keyword to be queried;

[0033] Step S204, when there is a first key-value pair corresponding to the keyword to be queried in the cache library, determine whether the data value in the first key-value pair is a timestamp, where the timestamp is used to indicate the first query time when querying the data value corresponding to the keyword in the database last time;

[0034] Step S206, when the data value in the first key-value pair is a timestamp, determine whether the first key-value pair is valid according to the timestamp;

[0035] Step S208, when the first key-value pair is valid, return a null value to the requester.

[0036] Through the above steps, when a query request initiated by a requesting party is received, it is determined whether there is a first key-value pair corresponding to the keyword to be queried in the cache library. The query request at least includes: the keyword to be queried. When there is a first key-value pair corresponding to the keyword to be queried in the cache library, it is determined whether the data value in the first key-value pair is a timestamp, where the timestamp is used to indicate the first query time when the data value corresponding to the keyword was last queried in the database. When the data value in the first key-value pair is a timestamp, it is determined whether the first key-value pair is valid according to the timestamp. When the first key-value pair is valid, a null value is returned to the requesting party, solving the problem in the related art that when the data value in the key-value pair in the cache library is a null value, it is very likely that accurate data cannot be accurately queried. Furthermore, in the embodiment of the present invention, when the data value corresponding to the keyword in the query request is a null value, it is determined whether the first key-value pair is valid according to the timestamp, so that the target key-value pair returns a null value only when it is not valid for a long time.

[0037] In an exemplary embodiment, after determining whether there is a first key-value pair corresponding to the keyword to be queried in the cache library, when there is no first key-value pair corresponding to the keyword to be queried in the cache library, it is determined whether there is a second key-value pair corresponding to the keyword to be queried in the database. When there is no second key-value pair corresponding to the keyword to be queried in the database, the target key-value pair of the keyword to be queried and the target timestamp is cached to the cache library, where the target timestamp is used to indicate the second query time when the server queries the data value corresponding to the keyword to be queried in the database.

[0038] That is to say, when there is no first key-value pair corresponding to the keyword to be queried in the cache library, it means that the data value corresponding to the keyword to be queried has not been queried yet. Therefore, it is queried whether there is a second key-value pair corresponding to the keyword to be queried in the database. If there is still no second key-value pair corresponding to the keyword to be queried in the database, the target timestamp corresponding to the second query time this time and the keyword to be queried are cached to the cache library, and a null value is returned to the requesting party.

[0039] There are many ways to cache the target key-value pair of the keyword to be queried and the target timestamp to the cache library. An implementation method is given in the embodiment of the present invention. Specifically: determine the second query time when the server queries the data value corresponding to the keyword to be queried in the database; convert the second query time into a target timestamp according to a preset rule, and cache the target key-value pair of the keyword to be queried and the target timestamp to the cache library, where the preset rule at least includes: multiplying the query time by a preset value.

[0040] That is, determine the second query time when the server queries the data value corresponding to the keyword to be queried in the database, convert the second query time to obtain a target timestamp, and cache the target timestamp and the target key-value pair of the keyword to be queried in the cache library. For example, in the scenario where the cached real business data is a positive number, when the data value corresponding to the keyword to be queried is a null value, the negative value of the query time can be cached, that is, the query is multiplied by -1. This facilitates distinguishing whether the cached data is real business data or a timestamp in the case of a null value.

[0041] In an exemplary embodiment, determining whether the first key-value pair is valid according to the timestamp includes: determining the time difference between the current time and the first query time corresponding to the timestamp, and determining the magnitude relationship between the time difference and the first valid duration; determining whether the first key-value pair is valid according to the magnitude relationship between the time difference and the first valid duration.

[0042] That is, determining whether the first key-value pair is valid according to the time difference between the current time and the first query time; specifically, in the case where the magnitude relationship indicates that the time difference is greater than the first valid duration, determining that the first key-value pair is invalid; in the case where the magnitude relationship indicates that the time difference is less than the first valid duration, determining that the first key-value pair is valid.

[0043] In an exemplary embodiment, after determining whether the first key-value pair is valid according to the timestamp, in the case where the first key-value pair is invalid, determining whether there is a second key-value pair corresponding to the keyword to be queried in the database; in the case where there is a second key-value pair corresponding to the keyword to be queried in the database, returning the data value corresponding to the second key-value pair to the requester and caching the second key-value pair in the cache library.

[0044] That is to say, in the case where the first key-value pair is invalid, it means that the data in the first key-value pair needs to be updated. Therefore, query the database again to check whether there is a second key-value pair corresponding to the keyword to be queried. In the case where there is no second key-value pair corresponding to the keyword to be queried in the database, cache the timestamp corresponding to the current query time and the keyword to be queried in the cache library, and return a null value to the requester. In the case where there is a second key-value pair corresponding to the keyword to be queried in the database, return the data value corresponding to the second key-value pair to the requester and cache the second key-value pair in the cache library.

[0045] In an exemplary embodiment, before receiving a query request initiated by a requesting party, a target operation sent by a target object is received; a first valid duration and a second valid duration of a cache key-value pair in the cache library are set according to the target operation, where the first valid duration is the duration for which a key-value pair of a keyword and a timestamp is stored in the cache library; and the second valid duration is the duration for which a key-value pair of a keyword and service data is stored in the cache library.

[0046] That is, the valid duration of the key-value pair of the keyword and the timestamp cached in the cache library is different from the valid duration of the key-value pair of the keyword and the service data. For example, the valid duration of the key-value pair of the keyword and the service data is 24 hours, and the valid duration of the key-value pair of the keyword and the timestamp is 30 minutes. The valid durations of the key-value pair of the keyword and the timestamp and the key-value pair of the keyword and the service data can not only ensure the long-term validity of the cache when there is data, but also update the cache in a timely manner to ensure data consistency, and reduce the cache breakdown frequency when there is no data.

[0047] To better understand the process of the above cache method for key-value pairs, the following further describes the implementation method flow of the above cache for key-value pairs in combination with optional embodiments, but it is not used to limit the technical solutions of the embodiments of the present invention.

[0048] In this embodiment, a method for responding to a query request is provided. Figure 3 It is a schematic diagram of the method for responding to a query request according to an embodiment of the present invention, as Figure 3 shown, and the specific steps are as follows:

[0049] Step S301: The background service receives a query request initiated by a requesting party, and determines whether there is a first key-value pair corresponding to the keyword in the query request in the cache library. If there is a first key-value pair corresponding to the keyword in the query request in the cache library, steps S302 - S312 are executed; if there is no first key-value pair corresponding to the keyword in the query request in the cache library, steps S307 - S312 are executed.

[0050] Step S302: Determine whether the data value in the first key-value pair is real service data. If the data value in the first key-value pair is real service data, step S303 is executed; if the data value in the first key-value pair is a timestamp, steps S304 - S312 are executed.

[0051] Step S303: Return service data to the requesting party.

[0052] Step S304: Determine whether the first key-value pair has passed the expiration date (equivalent to the first valid duration in the above embodiment). If the first key-value pair has not exceeded the expiration date, step S305 is executed; if the first key-value pair has exceeded the expiration date, step S306 is executed.

[0053] Specifically, when the data value in the first key-value pair is a time stamp, the data cached in the first key-value pair is not real business data, but the time stamp of the last database query. This indicates that there was no data when the request last queried the database. At this time, subtract the last query time stamp from the current time stamp to obtain the duration of the cache null value, and compare this duration with the set null value validity period: if the duration of the cache null value >= the validity period, it means that the duration since the last query returned a null value has exceeded the validity period, indicating that the cache is invalid. Clear the cache and query the database again; if the duration of the cache null value < the null value validity period, it means that the duration since the last query returned a null value has not expired, and there is no need to frequently query the database. Just return a null value to the requester.

[0054] Step S305: Return a null value to the requester;

[0055] Step S306: Clear the first key-value pair;

[0056] Step S307: Query the database;

[0057] Step S308: Determine whether there is a second key-value pair corresponding to the keyword in the query request in the database. If there is a second key-value pair corresponding to the keyword in the query request in the database, execute steps S309 - S310; if there is no second key-value pair corresponding to the keyword in the query request in the database, execute steps S311 - S312;

[0058] Step S309: Cache the second key-value pair in the cache library;

[0059] Step S310: Return business data to the requester;

[0060] Step S311: Cache the time stamp corresponding to the current query time and the target key-value pair corresponding to the keyword in the cache library;

[0061] Step S312: Return a null value to the requester.

[0062] In the embodiment of the present invention, the cache validity period of business data and the cache validity period of null values can be set separately, but it is necessary to satisfy that the cache validity period of null values <= the cache validity period of real values, which also conforms to the general scenario of caching unchanging data. When the database query returns a null value, save the last null value query time stamp instead of the null value in the cache for judging whether it is necessary to query the database during the next query. Reduce the number of times of frequently querying the database when the cache is penetrated due to null values, improve service performance, and reduce query latency. For the scenario where the real business data in the cache is a positive number, when encountering a null value, the negative value of the query time stamp can be directly cached, that is, the time stamp is multiplied by (-1). This is convenient for distinguishing whether the cached data is real business data or the query time stamp in the case of a null value.

[0063] Through the description of the above embodiments, those skilled in the art can clearly understand that the method according to the above embodiments can be implemented by means of software plus a necessary general hardware platform. Of course, it can also be implemented by hardware, but in many cases, the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes several instructions for causing a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods of the various embodiments of the present invention.

[0064] In this embodiment, a response device for a query request is also provided. The response device for the query request is used to implement the above embodiments and preferred implementation manners, and those that have been described will not be repeated. As used hereinafter, the term "module" can be a combination of software and / or hardware that can implement a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, implementation in hardware, or a combination of software and hardware is also possible and contemplated.

[0065] Figure 4 is a structural block diagram (one) of a response device for a query request according to an embodiment of the present invention; as Figure 4 shown, it includes:

[0066] A first determination module 42, configured to determine whether there is a first key-value pair corresponding to the keyword to be queried in the cache library when receiving a query request initiated by a requester, where at least the following is included in the query request: the keyword to be queried;

[0067] A second determination module 44, configured to determine whether the data value in the first key-value pair is a time stamp when there is a first key-value pair corresponding to the keyword to be queried in the cache library, where the time stamp is used to indicate a first query time when the data value corresponding to the keyword was last queried in the database;

[0068] A third determination module 46, configured to determine whether the first key-value pair is valid according to the time stamp when the data value in the first key-value pair is a time stamp;

[0069] A response module 48, configured to return a null value to the requester when the first key-value pair is valid.

[0070] Through the above device, the first determination module 42 determines whether there is a first key-value pair corresponding to the keyword to be queried in the cache library when receiving a query request initiated by a requester. The query request at least includes: the keyword to be queried; the second determination module 44 determines whether the data value in the first key-value pair is a timestamp when there is a first key-value pair corresponding to the keyword to be queried in the cache library. The timestamp is used to indicate the first query time when the data value corresponding to the keyword was last queried in the database; the third determination module 46 determines whether the first key-value pair is valid according to the timestamp when the data value in the first key-value pair is a timestamp; the response module 48 returns a null value to the requester when the first key-value pair is valid, solving the problem in the related art that when the data value in the key-value pair in the cache library is a null value, it is very likely that accurate data cannot be accurately queried. Furthermore, in the embodiment of the present invention, when the data value corresponding to the keyword in the query request is a null value, it is determined whether the first key-value pair is valid according to the timestamp, so that the target key-value pair returns a null value only when the invalid duration has not elapsed.

[0071] In an exemplary embodiment, Figure 5 is the structural block diagram (II) of a response device for a query request according to an embodiment of the present invention; as Figure 5 shown, the above device further includes: a cache module 52, configured to determine whether there is a second key-value pair corresponding to the keyword to be queried in the database when there is no first key-value pair corresponding to the keyword to be queried in the cache library; when there is no second key-value pair corresponding to the keyword to be queried in the database, cache the target key-value pair of the keyword to be queried and the target timestamp into the cache library, where the target timestamp is used to indicate the second query time when the server queries the data value corresponding to the keyword to be queried in the database.

[0072] In an exemplary embodiment, the cache module 52 is further configured to determine the second query time when the server queries the data value corresponding to the keyword to be queried in the database; convert the second query time into a target timestamp according to a preset rule, and cache the target key-value pair of the keyword to be queried and the target timestamp into the cache library, where the preset rule at least includes: multiplying the query time by a preset value.

[0073] In an exemplary embodiment, the third determination module is further configured to determine the time difference between the current time and the first query time corresponding to the timestamp, and determine the magnitude relationship between the time difference and the first valid duration; determine whether the first key-value pair is valid according to the magnitude relationship between the time difference and the first valid duration.

[0074] In an exemplary embodiment, the third determination module is further configured to determine that the first key-value pair is invalid when the size relationship indicates that the time difference is greater than the first effective duration; and determine that the first key-value pair is valid when the size relationship indicates that the time difference is less than the first effective duration.

[0075] In an exemplary embodiment, the cache module is further configured to determine whether there is a second key-value pair corresponding to the keyword to be queried in the database when the first key-value pair is invalid; and when there is a second key-value pair corresponding to the keyword to be queried in the database, return the data value corresponding to the second key-value pair to the requester and cache the second key-value pair in the cache library.

[0076] In an exemplary embodiment, the third determination module is further configured to receive a target operation sent by a target object; and set a first effective duration and a second effective duration for caching key-value pairs in the cache library according to the target operation, where the first effective duration is the duration for which the key-value pair of the keyword and the timestamp is stored in the cache library; and the second effective duration is the duration for which the key-value pair of the keyword and the service data is stored in the cache library.

[0077] An embodiment of the present invention further provides a storage medium, which includes a stored program, where the above program executes the method of any one of the above when running.

[0078] Optionally, in this embodiment, the above storage medium may be set to store program code for performing the following steps:

[0079] S1. When receiving a query request initiated by a requester, determine whether there is a first key-value pair corresponding to the keyword to be queried in the cache library, where the query request at least includes: the keyword to be queried;

[0080] S2. When there is a first key-value pair corresponding to the keyword to be queried in the cache library, determine whether the data value in the first key-value pair is a timestamp, where the timestamp is used to indicate the time of the last query of the data value corresponding to the keyword in the database;

[0081] S3. When the data value in the first key-value pair is a timestamp, determine whether the first key-value pair is valid according to the timestamp;

[0082] S4. When the first key-value pair is valid, return a null value to the requester.

[0083] An embodiment of the present invention further provides an electronic device, including a memory and a processor. A computer program is stored in the memory, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.

[0084] Optionally, the above electronic device may further include a transmission device and an input / output device. Among them, the transmission device is connected to the above processor, and the input / output device is connected to the above processor.

[0085] Optionally, in this embodiment, the above processor may be configured to execute the following steps through a computer program:

[0086] S1. When receiving a query request initiated by a requester, determine whether there is a first key-value pair corresponding to the keyword to be queried in the cache library. Among them, at least the following is included in the query request: the keyword to be queried;

[0087] S2. When there is a first key-value pair corresponding to the keyword to be queried in the cache library, determine whether the data value in the first key-value pair is a timestamp, where the timestamp is used to indicate the time of the last query of the data value corresponding to the keyword in the database;

[0088] S3. When the data value in the first key-value pair is a timestamp, determine whether the first key-value pair is valid according to the timestamp;

[0089] S4. When the first key-value pair is valid, return a null value to the requester.

[0090] Optionally, in this embodiment, the above storage medium may include but is not limited to: various media such as USB flash drives, read-only memories (ROM for short), random access memories (RAM for short), mobile hard disks, magnetic disks, or optical discs that can store program codes.

[0091] Optionally, specific examples in this embodiment may refer to the examples described in the above embodiments and optional implementation manners, and will not be elaborated herein.

[0092] Obviously, those skilled in the art should understand that the above-mentioned modules or steps of the present invention can be implemented by a general-purpose computing device. They can be concentrated on a single computing device or distributed on a network composed of multiple computing devices. Optionally, they can be implemented by program codes executable by the computing device. Thus, they can be stored in a storage device and executed by the computing device. And in some cases, the steps shown or described can be executed in a different order from here, or they can be separately fabricated into individual integrated circuit modules, or multiple modules or steps among them can be fabricated into a single integrated circuit module to implement. In this way, the present invention is not limited to any specific combination of hardware and software.

[0093] The above are only the preferred embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the principle of the present invention shall be included within the protection scope of the present invention.

Claims

1. A method for responding to a query request, characterized in that, Including: When receiving a query request initiated by a requester, determining whether there is a first key-value pair corresponding to the keyword to be queried in the cache library, where the query request at least includes: the keyword to be queried; When there is a first key-value pair corresponding to the keyword to be queried in the cache library, determining whether the data value in the first key-value pair is a timestamp, where the timestamp is used to indicate the first query time when the data value corresponding to the keyword was last queried in the database; When the data value in the first key-value pair is a timestamp, determining whether the first key-value pair is valid according to the timestamp; When the first key-value pair is valid, returning a null value to the requester; Wherein, after determining whether there is a first key-value pair corresponding to the keyword to be queried in the cache library, the method further includes: when there is no first key-value pair corresponding to the keyword to be queried in the cache library, determining whether there is a second key-value pair corresponding to the keyword to be queried in the database; when there is no second key-value pair corresponding to the keyword to be queried in the database, caching the target key-value pair of the keyword to be queried and the target timestamp into the cache library, where the target timestamp is used to indicate the second query time when the server queries the data value corresponding to the keyword to be queried in the database; Wherein, determining whether the first key-value pair is valid according to the timestamp includes: determining the time difference between the current time and the first query time corresponding to the timestamp, and determining the magnitude relationship between the time difference and the first valid duration; determining whether the first key-value pair is valid according to the magnitude relationship between the time difference and the first valid duration; Wherein, after determining whether the first key-value pair is valid according to the timestamp, the method further includes: when the first key-value pair is invalid, determining whether there is a second key-value pair corresponding to the keyword to be queried in the database; when there is a second key-value pair corresponding to the keyword to be queried in the database, returning the data value corresponding to the second key-value pair to the requester and caching the second key-value pair into the cache library.

2. The response method for a query request according to claim 1, wherein Caching the target key-value pair of the keyword to be queried and the target timestamp into the cache library includes: Determining the second query time when the server queries the data value corresponding to the keyword to be queried in the database; Converting the second query time into a target timestamp according to a preset rule and caching the target key-value pair of the keyword to be queried and the target timestamp into the cache library, where the preset rule at least includes: multiplying the query time by a preset value.

3. The response method for a query request according to claim 1, characterized in that, Determining whether the first key-value pair is valid according to the magnitude relationship between the time difference and the first valid duration includes: When the magnitude relationship indicates that the time difference is greater than the first valid duration, determining that the first key-value pair is invalid; When the magnitude relationship indicates that the time difference is less than the first valid duration, determining that the first key-value pair is valid.

4. The response method for a query request according to claim 1, characterized in that Before receiving the query request initiated by the requester, the method further includes: Receiving a target operation sent by a target object; Set a first valid duration and a second valid duration for caching key-value pairs in the cache library according to the target operation, where the first valid duration is the duration for which the key-value pair of the keyword and the timestamp is stored in the cache library; the second valid duration is the duration for which the key-value pair of the keyword and the service data is stored in the cache library.

5. A response device for a query request, characterized in that, Including: A first determination module, configured to determine whether there is a first key-value pair corresponding to the keyword to be queried in the cache library when receiving a query request initiated by a requester, where the query request at least includes: the keyword to be queried; A second determination module, configured to determine whether the data value in the first key-value pair is a timestamp when there is a first key-value pair corresponding to the keyword to be queried in the cache library, where the timestamp is used to indicate the first query time when the data value corresponding to the keyword was last queried in the database; A third determination module, configured to determine whether the first key-value pair is valid according to the timestamp when the data value in the first key-value pair is a timestamp; A response module, configured to return a null value to the requester when the first key-value pair is valid; Wherein, the device further includes: a cache module, configured to determine whether there is a second key-value pair corresponding to the keyword to be queried in the database when there is no first key-value pair corresponding to the keyword to be queried in the cache library; when there is no second key-value pair corresponding to the keyword to be queried in the database, cache the target key-value pair of the keyword to be queried and the target timestamp into the cache library, where the target timestamp is used to indicate the second query time when the server queries the data value corresponding to the keyword to be queried in the database; wherein, the third determination module is further configured to determine the time difference between the current time and the first query time corresponding to the timestamp, and determine the magnitude relationship between the time difference and the first valid duration; determine whether the first key-value pair is valid according to the magnitude relationship between the time difference and the first valid duration; Wherein, the cache module is further configured to determine whether there is a second key-value pair corresponding to the keyword to be queried in the database when the first key-value pair is invalid; when there is a second key-value pair corresponding to the keyword to be queried in the database, return the data value corresponding to the second key-value pair to the requester and cache the second key-value pair into the cache library.

6. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored program, where the program, when running, executes the method described in any one of claims 1 to 4 above.

7. An electronic device, comprising a memory and a processor, characterized in that, A computer program is stored in the memory, and the processor is configured to execute the method described in any one of claims 1 to 4 above through the computer program.

Citation Information

Patent Citations

  • Method for preventing cache penetration and terminal

    CN110109956A

  • Data management method of water resource management decision support system

    CN111127252A