A method and system for improving API query concurrency based on time window caching
By using time window caching technology, the problems of low cache hit rate and data real-time performance of API platforms are solved, enabling real-time data access under high concurrency and improving API query concurrency performance.
Patent Information
- Application Number
- CN202311576737.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-22
- Publication Date
- 2026-03-17
- Estimated Expiration
- 2043-11-22
AI Technical Summary
When the cache expiration time is not set properly, the existing API platform will have a low cache hit rate, which will affect the query concurrency performance. Furthermore, traditional caching methods cannot guarantee the real-time performance of data under high concurrency.
A time-window-based caching approach is adopted. By constructing cache keys and control elements, query requests are marked and categorized, cache write status is monitored, header query requests are processed first, and the latest results are rolled into the cache within the time window. Local memory and middleware are used for two-level cache management.
It improved cache hit rate, reduced database query overhead, significantly improved API concurrency performance, and ensured that requests could read real-time data during high-traffic periods.
Smart Images

Figure CN117609287B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of API query request technology, and in particular to a method and system for improving API query concurrency based on time window caching. Background Technology
[0002] Caching API query results is an effective way to improve API performance. Proper use of caching can avoid additional database query overhead, thereby increasing API query concurrency. Currently, API platforms use caching methods such as... Figure 1 As shown, when an API query request is received, the cache is read first. If the cache cannot be read, the database is queried, and then the query result is written to the cache.
[0003] In practical applications, API platforms use, for example... Figure 1 The caching method shown has the following drawbacks: 1. When the cache expiration time is set too short, most requests will not be able to read the cache during peak traffic periods, resulting in a low cache hit rate; 2. When the cache expiration time is set too long, the latest data may not be retrieved, and the cache hit rate will still be low during the first peak traffic period; 3. It is necessary to check the cache to determine its existence, which is too slow and also affects the API's concurrency performance. Taking the scenario of querying weather forecasts by city name as an example, when the API platform receives a large number of requests to query Beijing weather at a certain moment, most requests will not have time to read the cache and will directly use the interface query method; when the next peak of weather queries arrives, it may not be querying Beijing weather, and even if the cache is read again for Beijing weather, the cached data may not be up-to-date.
[0004] Therefore, how to provide a method to improve query concurrency and efficiency while increasing cache hit rate has become an urgent technical problem to be solved. Summary of the Invention
[0005] In view of this, in order to overcome the shortcomings of the prior art, the present invention aims to provide a method and system for improving API query concurrency based on time window caching.
[0006] According to a first aspect of the present invention, a method for improving API query concurrency based on time window caching is provided, comprising:
[0007] Receive API query requests, construct a cache key based on the received API query request and the current time, and match the corresponding control element from the cache control container based on the constructed cache key;
[0008] API query requests are marked and categorized using matching control elements to obtain header query requests and subsequent query requests.
[0009] Monitor the cache write status, and execute header query requests and subsequent query requests according to the monitored cache write status to update the control element.
[0010] Preferably, in the method for improving API query concurrency based on time window caching of the present invention, the steps include receiving an API query request, constructing a cache key based on the received API query request and the current time, and matching the corresponding control element from the cache control container based on the constructed cache key, including:
[0011] Receive API query requests and obtain the interface address and request parameters from the received API query requests;
[0012] Build a cache key based on the current time, the obtained interface address, and the request parameters, and send the built cache key to the cache control container;
[0013] Construct the corresponding control element based on the synchronization counter, cache write status, and the constructed cache key.
[0014] Preferably, in the method for improving API query concurrency based on time window caching of the present invention, a corresponding control element is constructed based on the synchronization counter, cache write status, and the constructed cache key, including:
[0015] Determine whether the constructed cache key already exists in the cache control container;
[0016] If the constructed cache key already exists in the cache control container, the control element with the same cache key is matched from the cache control container and used as the control element for the API query request.
[0017] If the constructed cache key does not exist in the cache controller, a control element in the control container is initialized based on the synchronization counter, cache write status, and constructed cache key, and the initialized control element is used as the control element for API query requests.
[0018] Preferably, in the method for improving API query concurrency based on time window caching of the present invention, API query requests are marked and classified using matching control elements to obtain header query requests and subsequent query requests, including:
[0019] Using a synchronous counter in the control element, a labeling and classification threshold is set according to the request labeling algorithm, and the corresponding API query request is labeled with a sequence number.
[0020] Compare the sequence number of the API query request with the tag classification threshold;
[0021] API query requests with serial numbers less than the classification threshold are treated as header query requests, while API query requests with serial numbers not less than the classification threshold are treated as subsequent query requests.
[0022] Preferably, in the method for improving API query concurrency based on time window caching of the present invention, the cache write status is monitored, and header query requests and subsequent query requests are executed according to the monitored cache write status to update the control element, including:
[0023] For header query requests, the corresponding cache write status listener is initiated first. When the listened cache write status is false, the database is queried, the database query result is used as the query result of the API query request, and the database query result is written into the time window cache constructed by the query result and the cache key. The corresponding cache write status in the control element is updated to true according to the cache key.
[0024] When the monitored cache write status is true, retrieve the corresponding query result from the time window cache according to the cache key, return the retrieved query result to the corresponding API query request, and update the corresponding cache write status in the control element to true according to the cache key.
[0025] Preferably, in the method for improving API query concurrency based on time window caching of the present invention, the cache write status is monitored, and header query requests and subsequent query requests are executed according to the monitored cache write status to update the control element, including:
[0026] Set a maximum data threshold and a time window threshold for header query requests. When the amount of data in the query results obtained by the header query request is not greater than the maximum data threshold and the query time of the header query request is within the time window threshold, cache the query results corresponding to the header query request in local memory.
[0027] If the amount of data obtained from the header query request is greater than the set maximum data threshold, or if the query time of the header query request is not within the time window threshold, the query result corresponding to the header query request will be cached in the middleware.
[0028] Preferably, in the method for improving API query concurrency based on time window caching of the present invention, the method further includes monitoring the cache write status, executing header query requests and subsequent query requests according to the monitored cache write status, and updating the control element.
[0029] When an API query request initiates a query for the current time window, the cache of the previous time window is invalidated. A delayed destruction time threshold is set for the invalidated cache. After the cache invalidation reaches the delayed destruction time threshold, the invalidated cache is destroyed.
[0030] An expiration and destruction time threshold is set for caches written to local memory or middleware. The expiration and destruction time threshold is greater than a time window. When the cache time of caches written to local memory or middleware reaches the expiration and destruction time threshold, the corresponding cache is destroyed.
[0031] Preferably, in the method for improving API query concurrency based on time window caching of the present invention, the cache write status is monitored, and header query requests and subsequent query requests are executed according to the monitored cache write status to update the control element, including:
[0032] Set a polling interval for subsequent query requests, initiate corresponding cache write status monitoring based on the set polling interval, and continue waiting according to the polling interval when the monitored cache write status is false;
[0033] When the monitored cache write status is true, the corresponding query result is retrieved from the time window cache constructed from the query result and the cache key based on the cache key. The retrieved query result is returned to the corresponding API query request, and the corresponding cache write status in the control element is updated to true based on the cache key.
[0034] Preferably, in the method for improving API query concurrency based on time window caching of the present invention, the cache write status is monitored, and header query requests and subsequent query requests are executed according to the monitored cache write status, and control elements are updated. The method further includes: setting a polling timeout threshold and a polling interval extension threshold for subsequent query requests; when the polling timeout threshold or the polling interval extension threshold is exceeded and no cache write status monitoring related to subsequent query requests is initiated, the database is queried, the database query result is used as the query result of the API query request, and the database query result is written into the time window cache constructed by the query result and the cache key.
[0035] According to a second aspect of the present invention, a system for improving API query concurrency based on time-window caching is provided. The system includes a query server, which is used to receive API query requests, construct a cache key based on the received API query request and the current time, match the corresponding control element from the cache control container based on the constructed cache key, mark and classify the API query requests using the matched control element to obtain header query requests and subsequent query requests, monitor the cache write status, and execute the header query request and subsequent query request respectively according to the monitored cache write status, and update the control element.
[0036] According to a third aspect of the present invention, a computer device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the method described in the first aspect of the present invention.
[0037] This invention relates to a method and system for improving API query concurrency based on time-window caching. It solves the problems of low hit rate under high concurrency when using traditional API caching methods and the fact that the pre-warmed API cache is not real-time data. By applying time-window caching to API queries, the latest query results are cached in a rolling manner through time windows, ensuring that the majority of requests can read the real-time cache during any peak traffic period, greatly reducing the overhead of querying the database and significantly improving the API's concurrency performance. Attached Figure Description
[0038] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0039] Figure 1 A flowchart illustrating the caching methods used by API platforms in existing technologies;
[0040] Figure 2 This is a schematic diagram illustrating the principle of a method for improving API query concurrency based on time window caching according to an embodiment of the present invention.
[0041] Figure 3 This is a schematic diagram of a system for improving API query concurrency based on time window caching, applicable to embodiments of the present invention.
[0042] Figure 4 This is a flowchart illustrating the steps of a method for improving API query concurrency based on time window caching according to an embodiment of the present invention.
[0043] Figure 5 This is an execution diagram of a method for improving API query concurrency based on time window caching according to an embodiment of the present invention;
[0044] Figure 6 This is a schematic diagram of the structure of the device provided by the present invention. Detailed Implementation
[0045] The embodiments of the present invention will now be described in detail with reference to the accompanying drawings.
[0046] It should be noted that, in the absence of conflict, the following embodiments and features can be combined with each other; and, based on the embodiments of this disclosure, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this disclosure.
[0047] It should be noted that various aspects of embodiments within the scope of the appended claims are described below. It will be apparent that the aspects described herein can be embodied in a wide variety of forms, and any particular structure and / or function described herein is merely illustrative. Based on this disclosure, those skilled in the art will understand that one aspect described herein can be implemented independently of any other aspect, and two or more of these aspects can be combined in various ways. For example, any number of aspects set forth herein can be used to implement the device and / or practice the method. Additionally, this device and / or method can be implemented using structures and / or functionalities other than one or more of the aspects set forth herein.
[0048] Figure 2 This is a schematic diagram illustrating the principle of a method for improving API query concurrency based on time window caching according to an embodiment of the present invention, as shown below. Figure 2 As shown, the technical principle of this invention is as follows:
[0049] Centered on a time-window cache, this system comprises a cache control container, a request marking algorithm, cache status monitoring, and a polling timeout strategy. During peak periods with relatively fixed request parameters, a small subset of requests to any given API will improve cache hit rate, while the majority will hit the cache. Upon receiving an API query request, a cache key is generated based on the API address, request parameters, and current time. This key, along with a synchronization counter and an initially false cache write status, forms a control element, which is then placed into the cache control container. For the same API request within the same time window, the control element and the time-window cache share the same cache key. Based on the request marking algorithm, requests marked earlier by the synchronization counter are prioritized for cache reading; if no cache is found, the database is queried. Requests marked later enter a polling wait, with a default polling interval of 10 milliseconds and a default timeout equal to the time window. Once the API query retrieves the database result, it is written to the time-window cache, which consists of the cache key and the query result. The control element is then notified to update the cache write status to true based on the cache key. Requests in the polling wait continuously monitor the cache status, retrieving the time-window cache and returning the API query result.
[0050] Figure 3 An exemplary system is shown that utilizes a time-window caching method to improve API query concurrency, applicable to embodiments of the present invention. For example... Figure 3 As shown, the system may include a query server 101, a communication network 102, and / or one or more query clients 103. Figure 3 The example in the text is multiple query clients 103.
[0051] The query server 101 can be any suitable server used to store information, data, programs, and / or any other suitable type of content. In some embodiments, the query server 101 can perform appropriate functions. For example, in some embodiments, the query server 101 can be used for API queries. As an optional example, in some embodiments, the query server 101 can be used to implement API queries by constructing a cache key. For example, the query server 101 can be used to receive API query requests, construct a cache key based on the received API query request and the current time, match the corresponding control element from the cache control container based on the constructed cache key; mark and classify the API query request using the matched control element to obtain the header query request and the subsequent query request; monitor the cache write status, execute the header query request and the subsequent query request respectively according to the monitored cache write status, and update the control element.
[0052] As another example, in some embodiments, the query server 101 may send a method for improving API query concurrency based on time window caching to the query client 103 for user use, according to the request of the query client 103.
[0053] As an optional example, in some embodiments, the query client 103 is used to provide a visual query interface, which is used to receive a user's selection input operation to execute an API query request, and to obtain and display the query interface corresponding to the option selected by the selection input operation from the query server 101 in response to the selection input operation. The query interface displays at least information about executing the API query request and operation options for the information about executing the API query request.
[0054] In some embodiments, communication network 102 may be any suitable combination of one or more wired and / or wireless networks. For example, communication network 102 may include any one or more of the following: the Internet, intranet, wide area network (WAN), local area network (LAN), wireless network, digital subscriber line (DSL) network, frame relay network, asynchronous transfer mode (ATM) network, virtual private network (VPN), and / or any other suitable communication network. Query client 103 may connect to communication network 102 via one or more communication links (e.g., communication link 104), which may be linked to query server 101 via one or more communication links (e.g., communication link 105). Communication links may be any communication link suitable for transmitting data between query client 103 and query server 101, such as network links, dial-up links, wireless links, hardwired links, any other suitable communication links, or any suitable combination of such links.
[0055] Query client 103 may include any one or more clients that present an interface related to executing API query requests in an appropriate form for user use and operation. In some embodiments, query client 103 may include any suitable type of device. For example, in some embodiments, query client 103 may include a mobile device, tablet computer, laptop computer, desktop computer, and / or any other suitable type of client device.
[0056] Although query server 101 is illustrated as a single device, in some embodiments, any suitable number of devices may be used to perform the functions performed by query server 101. For example, in some embodiments, multiple devices may be used to implement the functions performed by query server 101. Alternatively, cloud services may be used to implement the functions of query server 101.
[0057] Based on the above system, this invention provides a method for improving API query concurrency based on time window caching, which is illustrated in the following embodiments.
[0058] Reference Figure 4 The flowchart illustrates the steps of a method for improving API query concurrency based on time window caching according to an embodiment of this application. Figure 5 This is a schematic diagram illustrating the execution of a method for improving API query concurrency based on time window caching according to an embodiment of the present invention.
[0059] The method for improving API query concurrency based on time window caching in this embodiment can be executed on the query server, such as... Figure 4 and Figure 5 As shown, this method for improving API query concurrency based on time window caching includes the following steps:
[0060] Step S201: Receive an API query request, construct a cache key based on the received API query request and the current time, and match the corresponding control element from the cache control container based on the constructed cache key.
[0061] As an optional example, the method of this embodiment of the invention receives an API query request, obtains the interface address and request parameters from the received API query request; constructs a cache key based on the current time and the obtained interface address and request parameters, and sends the constructed cache key to the cache control container; and constructs a corresponding control element based on the synchronization counter, cache write status, and the constructed cache key.
[0062] It is important to note that in the method of this embodiment, API requests with the same interface address and request parameters within the same time period are considered as the same API request. Subsequent request execution and cache hits essentially involve reading the query results of the same API request. In this embodiment, the cache key is a unique key for the same API request within a time window. It is composed of the MD5 hash of the interface address and request parameters, plus the time window. The time window is a valid time segment for the cache, equal to the current timestamp divided by the cache validity period. For example, if the validity period is set to 3 seconds, the same API request can hit the same cache within these 3 seconds, and then enters the next time window. The synchronization counter is used to count and sort the same API requests within the time window. For example, if there are 100 identical API requests within a time window, the synchronization counter will mark each API request sequentially, from request 1 to request 100. The control element consists of the cache key, the synchronization counter, and the cache write status. An API query request generates a cache key. If the key is a new value, it represents the start of a time window, and a control element is generated in the cache control container based on this key.
[0063] As an optional example, in the method of this embodiment of the invention, when constructing the corresponding control element based on the synchronization counter, cache write status, and the constructed cache key, it is necessary to determine whether the constructed cache key already exists in the cache control container. If the constructed cache key already exists in the cache control container, a control element with the same cache key is matched from the cache control container and used as the control element for the API query request. If the constructed cache key does not exist in the cache controller, a control element in the control container is initialized based on the synchronization counter, cache write status, and the constructed cache key, and the initialized control element is used as the control element for the API query request. It should be noted that after the control element is initialized, the cache write status is false.
[0064] Step S202: Use matching control elements to mark and classify API query requests to obtain header query requests and subsequent query requests.
[0065] As an optional example, this embodiment uses a synchronization counter in the control element to set a marking and classification threshold according to the request marking algorithm, and marks the corresponding API query requests with sequence numbers. The sequence number of the API query request is compared with the marking and classification threshold. API query requests with sequence numbers less than the marking and classification threshold are designated as header query requests, and API query requests with sequence numbers not less than the marking and classification threshold are designated as subsequent query requests. For example, the request marking algorithm identifies identical API requests pointing to the same control element within the same time window. The synchronization counter marks and classifies the requests, and according to the set marking and classification threshold, requests marked earlier (less than the threshold) are called header requests, and those marked later are called subsequent requests. This embodiment of the invention, through the request marking algorithm, ensures that requests hit the cache when the API processes a large number of identical requests in a short period of time. It should be noted that in practical applications, the labeling and classification threshold can be adjusted according to the time taken for online API testing. For example, the default threshold is 10, which means that if more than 10 requests for the same API are received within a time window, the API will enter a round-robin waiting state. For APIs with fast query speeds, the labeling and classification threshold should be reduced so that more requests can directly read the cache, and vice versa.
[0066] Step S203: Monitor the cache write status, and execute the header query request and subsequent query request according to the monitored cache write status to update the control element.
[0067] As an optional example, this embodiment's method prioritizes initiating corresponding cache write status monitoring for header query requests. When the monitored cache write status is false, a database query is performed, and the database query result is used as the query result of the API query request. The database query result is then written to a time-window cache constructed from the query result and the cache key. The corresponding cache write status in the control element is updated to true based on the cache key. When the monitored cache write status is true, the corresponding query result is retrieved from the time-window cache based on the cache key, and the retrieved query result is returned to the corresponding API query request. The corresponding cache write status in the control element is updated to true based on the cache key. For example, in this embodiment's method, the time-window cache is a caching module that continuously snapshots API query results for each time window to local memory or middleware. The cache key has a built-in time attribute, and caches corresponding to the same API request in different time periods are isolated and automatically expire. By storing the cache status in memory, the speed of monitoring the cache for the same request within the same time window is faster, further improving the API query cache hit rate.
[0068] As an optional example, the method of this embodiment can set a maximum data threshold and a time window threshold for header query requests. When the amount of data in the query result obtained by the header query request is not greater than the maximum data threshold and the query time of the header query request is within the time window threshold, the query result corresponding to the header query request is cached in local memory. When the amount of data in the query result obtained by the header query request is greater than the set maximum data threshold or the query time of the header query request is not within the time window threshold, the query result corresponding to the header query request is cached in middleware. For example, after a header request retrieves a query result within the time window, the query result is first cached in local memory. This is because under high concurrency, the middleware's read performance may drop rapidly due to factors such as network bottlenecks, while the data in local memory can be read instantly by subsequent requests. If the amount of data retrieved by the header request exceeds the set maximum data threshold or time window threshold, middleware caching will be triggered. This is a two-level caching mechanism. In practical applications, when the time window is large or the data volume is large, the query result is stored in the Elasticsearch search middleware; when the time window is small or the data volume is small, the query result is stored in Redis.
[0069] Because cache keys have an inherent time attribute, the cache from the previous time window will automatically expire as the API request time progresses. This delays cache destruction after expiration, avoiding resource contention between cache expiration deletion and frequent cache retrieval. As an optional example, the method in this embodiment invalidates the cache from the previous time window when an API query request initiates a query within the current time window. A delayed destruction time threshold is set for the invalidated cache. Once the cache expiration time threshold is reached, the invalidated cache is destroyed. For instance, when the request time enters the next time window, the previous cache key will automatically expire. The API request can match the corresponding time window cache through the call time, ensuring real-time cache validity while avoiding resource contention between cache expiration deletion and frequent cache retrieval, thereby improving API concurrency performance.
[0070] To promptly release memory space and thus ensure system security, the method in this embodiment of the invention also sets a cache destruction time. As an optional example, the method in this embodiment of the invention can also set an expiration and destruction time threshold for caches written to local memory or middleware. This expiration and destruction time threshold is greater than a time window. When the cache time for caches written to local memory or middleware reaches the expiration and destruction time threshold, the corresponding cache is destroyed. For example, the expiration and destruction time threshold in local memory is on the order of seconds.
[0071] As an optional example, the method of this embodiment of the invention can also set a polling interval for subsequent query requests, initiate corresponding cache write status monitoring according to the set polling interval, and continue to wait according to the polling interval when the monitored cache write status is false; when the monitored cache write status is true, retrieve the corresponding query result from the time window cache constructed by the query result and the cache key according to the cache key, return the retrieved query result to the corresponding API query request, and update the corresponding cache write status in the control element to true according to the cache key.
[0072] For example, for APIs with high real-time data requirements, the default settings for the time window threshold and polling interval can be used. The default time window threshold is 3 seconds. API query requests that are marked later will poll the cache write status every 10 milliseconds within 3 seconds. If a timeout occurs, the database will be queried directly, ensuring that most requests hit the cache. For APIs with low real-time data requirements, the time window threshold can be set to one hour or one day. Based on the query results retrieved from the header query request, the system will determine whether the cache is in Redis or Elasticsearch. It will not cache in memory. Subsequent requests that time out can choose to extend the polling wait time. APIs like these can all hit the cache.
[0073] As an optional example, the method of this embodiment can also set a polling timeout threshold and a polling interval extension threshold for subsequent query requests. When the polling timeout threshold or the polling interval extension threshold is exceeded and no cache write status monitoring related to the subsequent query request has been initiated, the database is queried, the database query result is used as the query result of the API query request, and the database query result is written into a time window cache constructed from the query result and the cache key. For example, when setting a polling timeout threshold for subsequent query requests, the polling wait time for subsequent requests is equal to the time window by default. The default timeout strategy is to directly query the database upon timeout, but for APIs with slow responses, an extended polling wait time can be set, and the database is queried only when the extended time is exhausted.
[0074] This invention relates to a method and system for improving API query concurrency based on time-window caching. Upon receiving an API query request, the time-window caching control container generates a cache key based on the interface address, request parameters, and current time. This cache key, along with a synchronization counter and cache write status, forms a control element. The cache control container sends several requests from the control element to a request marking algorithm. This algorithm uses the synchronization counter to classify and mark requests. Requests marked earlier are prioritized for cache reading, while requests marked later enter a round-robin waiting state. After classification by the marking algorithm, each request obtains its cache status through cache status monitoring. If the cache status monitoring of the first request is false, the database is queried first, and then the cache status is updated. If the cache status monitoring of subsequent requests is false, round-robin waiting continues. If the cache status monitoring of a request is true, it proceeds to the time-window cache, directly retrieving the query result from the cache based on the cache key of the request control element. The API uses a time-window caching strategy. Published APIs can choose to disable or enable time-window caching based on their own characteristics. Through appropriate cache configuration or by selecting the default configuration, it can be ensured that most APIs have a high cache hit rate when dealing with traffic peaks.
[0075] This invention solves the problem that traditional API caching methods result in low hit rates under high concurrency, and that pre-warmed API caches are not real-time data. By applying time window caching to API queries, the latest query results are cached in rolling time windows, ensuring that the majority of requests can read the real-time cache during any peak traffic period, greatly reducing the overhead of querying the database and significantly improving the API's concurrency performance.
[0076] like Figure 6 As shown, the present invention also provides a device including a processor 310, a communication interface 320, a memory 330 for storing processor-executable computer programs, and a communication bus 340. The processor 310, communication interface 320, and memory 330 communicate with each other via the communication bus 340. The processor 310 implements the aforementioned method for improving API query concurrency based on time-window caching by running the executable computer program.
[0077] The computer program in memory 330, when implemented as a software functional unit and sold or used as an independent product, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a 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 in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0078] The system embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and 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 network units. Some or all of the modules can be selected based on actual needs to achieve the purpose of this embodiment. Those skilled in the art can understand and implement this without any creative effort.
[0079] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, 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 can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., including several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods of various embodiments or some parts of embodiments.
[0080] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A method for caching API query concurrency based on time window, characterized in that, The method comprises: receiving an API query request, constructing a cache key according to the received API query request and the current time, and matching a corresponding control element from the cache control container according to the constructed cache key; using the matched control element to mark and classify the API query request to obtain a head query request and a subsequent query request; listening to the cache write state, and respectively executing the head query request and the subsequent query request according to the listened cache write state, and updating the control element; receiving an API query request, constructing a cache key according to the received API query request and the current time, and matching a corresponding control element from the cache control container according to the constructed cache key, comprising: receiving an API query request, obtaining an interface address and request parameters from the received API query request; constructing a cache key according to the current time and the obtained interface address and request parameters, and sending the constructed cache key to the cache control container; constructing a corresponding control element according to the synchronization counter, the cache write state and the constructed cache key; listening to the cache write state, and respectively executing the head query request and the subsequent query request according to the listened cache write state, and updating the control element, comprising: initiating the corresponding cache write state listening for the head query request first, when the listened cache write state is false, querying the database, taking the database query result as the query result of the API query request, and writing the database query result into the time window cache constructed by the query result and the cache key, and updating the corresponding cache write state in the control element to true according to the cache key; when the listened cache write state is true, obtaining the corresponding query result from the time window cache according to the cache key, returning the obtained query result to the corresponding API query request, and updating the corresponding cache write state in the control element to true according to the cache key.
2. The method for caching API query concurrency based on time window according to claim 1, characterized in that, constructing a corresponding control element according to the synchronization counter, the cache write state and the constructed cache key, comprising: determining whether the cache control container already contains the constructed cache key; when the cache control container already contains the constructed cache key, matching a control element with the same cache key from the cache control container according to the constructed cache key as the control element of the API query request; when the cache controller does not contain the constructed cache key, initializing a control element in the control container according to the synchronization counter, the cache write state and the constructed cache key, and taking the initialized control element as the control element of the API query request.
3. The method for caching API query concurrency based on time window according to claim 1, wherein, using the matched control element to mark and classify the API query request to obtain a head query request and a subsequent query request, comprising: using the synchronization counter in the control element to set a marking and classification threshold according to a request marking algorithm, and performing serial number marking for the corresponding API query request; comparing the serial number marking of the API query request with the marking and classification threshold; The API query request with a sequence number less than the marking classification threshold is marked as a head query request, and the API query request with a sequence number not less than the marking classification threshold is marked as a subsequent query request.
4. The method for caching API query concurrency based on time window according to claim 1, wherein, The cache write state is listened to, and the head query request and the subsequent query request are executed according to the listened cache write state, and the control element is updated, including: A maximum data threshold and a time window threshold are set for the head query request, when the data amount of the query result obtained by the head query request is not greater than the maximum data threshold and the query time of the head query request is within the time window threshold, the query result corresponding to the head query request is cached to the local memory; When the data amount of the query result obtained by the head query request is greater than the set maximum data threshold or the query time of the head query request is not within the time window threshold, the query result corresponding to the head query request is cached to the middleware.
5. The method for caching API query concurrency based on time window according to claim 1, wherein, The cache write state is listened to, and the head query request and the subsequent query request are executed according to the listened cache write state, and the control element is updated, and further comprising: When the API query request initiates the query of the current time window, the cache of the previous time window is invalidated, and a delay destruction time threshold is set for the invalidated cache, and after the cache invalidation processing reaches the delay destruction time threshold, the invalidated cache is destroyed; An expiration destruction time threshold is set for the cache written to the local memory or the middleware, and the expiration destruction time threshold is greater than the time window, and when the cache time of the cache written to the local memory or the middleware reaches the expiration destruction time threshold, the corresponding cache is destroyed.
6. The method for caching API query concurrency based on time window according to claim 1, wherein, The cache write state is listened to, and the head query request and the subsequent query request are executed according to the listened cache write state, and the control element is updated, including: A polling interval is set for the subsequent query request, and the corresponding cache write state listening is initiated according to the set polling, when the listened cache write state is false, the waiting is continued according to the polling interval; When the listened cache write state is true, the corresponding query result is obtained from the time window cache constructed by the query result and the cache key according to the cache key, the obtained query result is returned to the corresponding API query request, and the corresponding cache write state in the control element is updated to true according to the cache key.
7. The method for caching API query concurrency based on time window according to claim 1, wherein, The cache write state is listened to, and the head query request and the subsequent query request are executed according to the listened cache write state, and the control element is updated, and further comprising: a polling timeout threshold and a polling interval extension threshold are set for the subsequent query request, when the cache write state listening related to the subsequent query request has not been initiated after greater than the polling timeout threshold or the polling interval extension threshold, the database is queried, the database query result is taken as the query result of the API query request, and the database query result is written to the time window cache constructed by the query result and the cache key.
8. A system for caching API query concurrency based on time windows, the system comprising: The system comprises a query server, the query server is used for receiving an API query request, constructing a cache key according to the received API query request and a current time, and matching a corresponding control element from a cache control container according to the constructed cache key; The API query request is marked and classified by matching control elements to obtain a head query request and a subsequent query request; The cache write state is listened to, and the head query request and the subsequent query request are respectively executed according to the listened cache write state, and the control element is updated; wherein, the API query request is received, the cache key is constructed according to the received API query request and the current time, and the corresponding control element is matched from the cache control container according to the constructed cache key, including: receiving the API query request, obtaining the interface address and the request parameter from the received API query request; constructing the cache key according to the current time and the obtained interface address and request parameter, and sending the constructed cache key to the cache control container; constructing the corresponding control element according to the synchronization counter, the cache write state and the constructed cache key; wherein, the cache write state is listened to, and the head query request and the subsequent query request are respectively executed according to the listened cache write state, and the control element is updated, including: the corresponding cache write state listening is preferentially initiated for the head query request, when the listened cache write state is false, the database is queried, the database query result is taken as the query result of the API query request, and the database query result is written into the time window cache constructed by the query result and the cache key, the corresponding cache write state in the control element is updated to true according to the cache key; when the listened cache write state is true, the corresponding query result is obtained from the time window cache according to the cache key, the obtained query result is returned to the corresponding API query request, and the corresponding cache write state in the control element is updated to true according to the cache key.
Citation Information
Patent Citations
Data storage and query method and device, storage medium and electronic equipment
CN114116795A
Data query method and device, computer equipment and storage medium
CN115422237A