Data query method and device based on multi-level cache

By adopting a multi-level caching mechanism in a system with high concurrency and large traffic access, cache inventory status data of preset callers, goods without goods and hot items, the problem of long response time and weak concurrency ability is solved, and efficient data query and stable user experience.

CN113360528BActive Publication Date: 2025-02-21BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202010152491.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-03-06
Publication Date
2025-02-21
Estimated Expiration
2040-03-06

AI Technical Summary

Technical Problem

In the case of high concurrency and large traffic access, the system may have problems such as long response time or even short-term unavailability, which affects the user experience. The existing technology has high cost, poor stability, stand-alone throughput and weak system concurrency capabilities, etc. question.

Method used

Using a multi-level caching mechanism including a first cache, a second cache and a third cache, the first cache is used to cache the inventory status data of the preset caller, the second cache is used to cache the inventory status data of the goods without goods, and the third Caches are used to cache inventory status data for hotspot items, thereby intercepting large amounts of inventory query requests and reducing access to the database.

Benefits of technology

In the case of high concurrency and large traffic access, the stand-alone throughput and system concurrency capabilities are improved, stable and reliable, thereby improving user experience and saving costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113360528B_ABST
    Figure CN113360528B_ABST
Patent Text Reader

Abstract

The present invention discloses a data query method and device based on multi-level cache, and relates to the field of computer technology. A specific implementation of the method includes: receiving an inventory query request; judging whether there is inventory status data corresponding to the inventory query request in the cache; the cache includes: a first cache, a second cache, and a third cache; the first cache caches the inventory status data of the preset caller, the second cache caches the inventory status data of out-of-stock items; the third cache caches the inventory status data of hot items; if yes, the inventory status data is used as the result data; otherwise, the basic data corresponding to the inventory query request is obtained from the database, the inventory status data is determined according to the basic data, and the inventory status data is used as the result data. This implementation method can improve the single-machine throughput and system concurrency capability in the case of high concurrency and large traffic access, is stable and reliable, thereby improving the user experience, and saving costs.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a data query and device based on multi-level cache. Background Art

[0002] In the case of high concurrency and high traffic, the system may have a long response time or even be unavailable for a short period of time, affecting the user experience. Existing technologies generally deal with this problem through hardware expansion, distribution or hierarchical caching, but these methods have problems such as high cost, poor stability, and weak single-machine throughput and system concurrency capabilities in the case of high concurrency and high traffic. Summary of the invention

[0003] In view of this, an embodiment of the present invention provides a data query method and device based on multi-level cache, which can improve the single-machine throughput and system concurrency capability under high concurrency and large-traffic access conditions, and is stable and reliable, thereby improving user experience and saving costs.

[0004] To achieve the above object, according to one aspect of an embodiment of the present invention, a data query method based on a multi-level cache is provided, comprising:

[0005] Receive inventory query requests;

[0006] Determine whether there is inventory status data corresponding to the inventory query request in the cache; the cache includes: a first cache, a second cache, and a third cache; the first cache is used to cache the inventory status data of the preset caller, the second cache is used to cache the inventory status data of out-of-stock items; the third cache is used to cache the inventory status data of hot items;

[0007] If so, the inventory status data is used as the result data; otherwise, basic data corresponding to the inventory query request is obtained from the database, the inventory status data is determined according to the basic data, and the inventory status data is used as the result data.

[0008] Optionally, determining whether there is inventory status data corresponding to the inventory query request in the cache includes:

[0009] Determine whether there is inventory status data corresponding to the inventory query request in the first cache; if so, use the inventory status data as the result data; otherwise, determine whether there is inventory status data corresponding to the inventory query request in the second cache;

[0010] If yes, use the inventory status data as the result data; otherwise, determine whether there is inventory status data corresponding to the inventory query request in the third cache;

[0011] If so, the inventory status data is used as the result data; otherwise, it is determined that there is no inventory status data corresponding to the inventory query request in the cache.

[0012] Optionally, the first cache includes: a first local cache unit and a distributed cache unit;

[0013] Determining whether there is inventory status data corresponding to the inventory query request in the first cache includes: determining whether there is inventory status data corresponding to the inventory query request in the first local cache unit; if so, using the inventory status data as the result data; otherwise, determining whether there is inventory status data corresponding to the inventory query request in the distributed cache unit; if so, writing the inventory status data into the first local cache unit, and using the inventory status data as the result data; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the first cache.

[0014] Optionally, the first local cache unit includes: a preset caller directory, and cache records of each query object corresponding to the preset caller; the cache records are stored in a key-value pair format, wherein the key is the query object identifier and the value is the inventory status data corresponding to the query object;

[0015] Determining whether there is inventory status data corresponding to the inventory query request in the first local cache unit includes: determining whether there is a caller identifier of the inventory query request in the preset caller directory; if so, obtaining inventory status data corresponding to the query object identifier of the inventory query request from the cache record; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the first local cache unit.

[0016] Optionally, the third cache includes: a second local cache unit for storing hotspot item identifiers, and a third local cache unit for storing inventory status data of hotspot items;

[0017] Determining whether there is inventory status data corresponding to the inventory query request in the third cache includes: determining whether there is an item identifier of the inventory query request in the second local cache unit; if so, obtaining the inventory status data corresponding to the item identifier of the inventory query request from the third local cache unit; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the hotspot cache.

[0018] Optionally, the method of the embodiment of the present invention further includes: if the inventory status data corresponding to the item identifier of the inventory query request does not exist in the third local cache unit, writing a placeholder in the third local cache unit, and then using an asynchronous thread to load the inventory status data corresponding to the inventory query request from the database, so as to obtain the inventory status data as the result data and write the obtained inventory status data back to the third local cache unit.

[0019] Optionally, the method of the embodiment of the present invention further includes: monitoring hot items in the second local cache unit; for any hot item in the second local cache unit, when the hot item does not meet the hot condition, deleting the hot item.

[0020] Optionally, after obtaining the inventory status data corresponding to the inventory query request from the database, it also includes: writing the obtained inventory status data into a first cache, and, when the obtained inventory status data indicates that the item requested by the inventory query is out of stock, writing the obtained inventory status data into a second cache.

[0021] Optionally, the query object identifier includes: an item identifier, a channel identifier, and an address identifier of the inventory query request.

[0022] According to a second aspect of an embodiment of the present invention, a data query device based on a multi-level cache is provided, comprising:

[0023] A request receiving module receives inventory query requests;

[0024] A cache query module determines whether there is inventory status data corresponding to the inventory query request in the cache; the cache includes: a first cache, a second cache, and a third cache; the first cache is used to cache the inventory status data of the preset caller, the second cache is used to cache the inventory status data of out-of-stock items; the third cache is used to cache the inventory status data of hot items;

[0025] The result determination module, if yes, uses the inventory status data as the result data; otherwise, obtains basic data corresponding to the inventory query request from the database, determines the inventory status data according to the basic data, and uses the inventory status data as the result data.

[0026] Optionally, the cache query module determines whether there is inventory status data corresponding to the inventory query request in the cache, including:

[0027] Determine whether there is inventory status data corresponding to the inventory query request in the first cache; if so, use the inventory status data as the result data; otherwise, determine whether there is inventory status data corresponding to the inventory query request in the second cache;

[0028] If yes, use the inventory status data as the result data; otherwise, determine whether there is inventory status data corresponding to the inventory query request in the third cache;

[0029] If so, the inventory status data is used as the result data; otherwise, it is determined that there is no inventory status data corresponding to the inventory query request in the cache.

[0030] Optionally, the first cache includes: a first local cache unit and a distributed cache unit;

[0031] The cache query module determines whether there is inventory status data corresponding to the inventory query request in the first cache, including: determining whether there is inventory status data corresponding to the inventory query request in the first local cache unit; if so, using the inventory status data as the result data; otherwise, determining whether there is inventory status data corresponding to the inventory query request in the distributed cache unit; if so, writing the inventory status data into the first local cache unit, and using the inventory status data as the result data; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the first cache.

[0032] Optionally, the first local cache unit includes: a preset caller directory, and cache records of each query object corresponding to the preset caller; the cache records are stored in a key-value pair format, wherein the key is the query object identifier and the value is the inventory status data corresponding to the query object;

[0033] The cache query module determines whether there is inventory status data corresponding to the inventory query request in the first local cache unit, including: determining whether there is a caller identifier of the inventory query request in the preset caller directory; if so, obtaining the inventory status data corresponding to the query object identifier of the inventory query request from the cache record; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the first local cache unit.

[0034] Optionally, the third cache includes: a second local cache unit for storing hotspot item identifiers, and a third local cache unit for storing inventory status data of hotspot items;

[0035] The cache query module determines whether there is inventory status data corresponding to the inventory query request in the third cache, including: determining whether there is an item identifier of the inventory query request in the second local cache unit; if so, obtaining the inventory status data corresponding to the item identifier of the inventory query request from the third local cache unit; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the hotspot cache.

[0036] Optionally, the cache query module is also used to: if the inventory status data corresponding to the item identifier of the inventory query request does not exist in the third local cache unit, write a placeholder in the third local cache unit, and then use an asynchronous thread to load the inventory status data corresponding to the inventory query request from the database, so as to obtain the inventory status data as the result data and write the obtained inventory status data back to the third local cache unit.

[0037] Optionally, the cache query module is further used to: monitor the hot items in the second local cache unit; for any hot item in the second local cache unit, when the hot item does not meet the hot condition, delete the hot item.

[0038] Optionally, the result determination module is further used to: after obtaining inventory status data corresponding to the inventory query request from the database, write the obtained inventory status data into the first cache; and, when the obtained inventory status data indicates that the item requested by the inventory query is out of stock, write the obtained inventory status data into the second cache.

[0039] Optionally, the query object identifier includes: an item identifier, a channel identifier, and an address identifier of the inventory query request.

[0040] Optionally, the cache has four or more levels.

[0041] According to a third aspect of an embodiment of the present invention, there is provided an electronic device for querying data based on a multi-level cache, comprising:

[0042] one or more processors;

[0043] a storage device for storing one or more programs,

[0044] When the one or more programs are executed by the one or more processors, the one or more processors implement the method provided in the first aspect of the embodiment of the present invention.

[0045] According to a fourth aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored, and when the program is executed by a processor, the method provided by the first aspect of the embodiment of the present invention is implemented.

[0046] An embodiment of the above invention has the following advantages or beneficial effects: a multi-level cache mechanism including a first cache, a second cache and a third cache is adopted, which can improve the single-machine throughput and system concurrency capability under high concurrency and large-traffic access conditions, and is stable and reliable, thereby improving user experience and saving costs.

[0047] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0049] Figure 1 It is a schematic diagram of the principle of a data query method based on multi-level cache according to an embodiment of the present invention;

[0050] Figure 2 It is a main flow chart of a data query method based on multi-level cache in an optional embodiment of the present invention;

[0051] Figure 3 is a schematic diagram of the main process of querying inventory status data in the first cache in an embodiment of the present invention;

[0052] Figure 4 is a schematic diagram of the main process of querying inventory status data in the second cache in an embodiment of the present invention;

[0053] Figure 5 is a schematic diagram of the main process of querying inventory status data in the third cache in an embodiment of the present invention;

[0054] Figure 6 It is a schematic diagram of main modules of a data query device based on multi-level cache according to an embodiment of the present invention;

[0055] Figure 7 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0056] Figure 8 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0057] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0058] According to one aspect of an embodiment of the present invention, a data query method based on a multi-level cache is provided.

[0059] Figure 1 FIG. 1 is a schematic diagram of the principle of a data query method based on a multi-level cache according to an embodiment of the present invention. Figure 1As shown, the present invention adopts a multi-level cache mechanism including a first cache, a second cache and a third cache. The first cache is used to cache the inventory status data of the preset caller, the second cache is used to cache the inventory status data of out-of-stock items; and the third cache is used to cache the inventory status data of hot items. Figure 1 The process of data query of the inventory mechanism shown includes: receiving an inventory query request; determining whether there is inventory status data corresponding to the inventory query request in the cache; if so, using the inventory status data as result data; otherwise, obtaining basic data corresponding to the inventory query request from the database, determining the inventory status data based on the basic data, and using the inventory status data as result data.

[0060] Figure 2 FIG. 1 is a schematic diagram of the main flow of a data query method based on a multi-level cache in an optional embodiment of the present invention. Figure 2 As shown, the data query method includes:

[0061] Step S201: receiving an inventory query request;

[0062] Step S202: obtaining inventory status data corresponding to the inventory query request from the first cache;

[0063] Step S203, determine whether the query result is found in the first cache; if so, jump to step S209; otherwise, jump to step S204;

[0064] Step S204, obtaining inventory status data corresponding to the inventory query request from the second cache;

[0065] Step S205, determine whether the query result is found in the second cache; if so, jump to step S209; otherwise, jump to step S206;

[0066] Step S206: Obtain inventory status data corresponding to the inventory query request from the third cache;

[0067] Step S207, determine whether the query result is found in the third cache; if so, jump to step S209; otherwise, jump to step S208;

[0068] Step S208: acquiring basic data corresponding to the inventory query request from the database, and determining inventory status data according to the basic data;

[0069] Step S209: Use the queried inventory status data as result data.

[0070] Inventory status data is used to indicate whether an item is in stock or out of stock. The first cache caches the inventory status data of preset callers, such as callers for personalized recommendations, advertisements, and the like. The characteristics of the caller are a large number of interface calls, a large coverage of query objects, and an acceptable delay for a certain period of time for the result data of the query object. The first cache caches the inventory status data of query objects such as commodities based on the caller dimension, which can intercept a large number of inventory query requests, reduce the resource consumption caused by a large number of requests calling the database query interface at the same time in the case of high concurrency and high traffic access, improve the single-machine throughput and system concurrency capabilities, and be stable and reliable, thereby improving the user experience and saving costs.

[0071] The second cache is used to cache the inventory status data of out-of-stock items, and can intercept inventory query requests for out-of-stock items. Taking the scenario of flash sales or rush purchases as an example, the inventory quantity of hot items is limited, the purchase volume is large, and they may be sold out in just a few seconds, and they are usually not restocked immediately. Using the second cache to cache the inventory status data of out-of-stock items can avoid repeated and invalid basic information queries and calculations, and at the same time play a peak-cutting role (i.e., reducing access to the database under high concurrency and high-traffic access). In actual application, a certain cache expiration time can be set according to the actual business scenario, which can not only ensure that out-of-stock items will not penetrate subsequent queries and calculations in a short time, but also ensure that the cache data can be updated in time after the inventory status data of the item is updated.

[0072] The third cache is used to cache the inventory status data of hot items. The concurrent volume of hot items is large. By caching the inventory status data of hot items, a large number of requests can be avoided from calling the database query interface at the same time, reducing resource consumption.

[0073] Although Figure 1 Only three levels of cache, namely the first cache, the second cache and the third cache, are shown. Those skilled in the art should understand that the multi-level cache mechanism may also include four or more levels of cache, and the data cached in each cache of the fourth level or other levels may be selectively determined based on actual conditions, such as inventory data of member-exclusive items, inventory data of newly launched items, etc.

[0074] When there is no inventory status data corresponding to the inventory query request in the cache, the basic data corresponding to the inventory query request, such as product information, inventory quantity and warehouse attributes, etc., is obtained from the database, and then the warehouse covering the address is matched according to the order address, and the inventory status data is obtained by status calculation according to the inventory quantity. The database can use Redis (an open source storage system written in ANSI C language) and the like. In actual application, the database can be queried through the RPC (Romote Procedure Call) interface. Querying basic data depends on network interaction and RPC interface, which has a great impact on interface performance. Subsequent calculation processing consumes high system resources. In the case of high concurrency and large traffic access, the system may have a long response time or even a short-term unavailable problem, which affects the user experience. The present invention adopts a multi-level cache mechanism including a first cache, a second cache and a third cache, which can intercept a large number of inventory query requests through the cache, avoid unnecessary resource consumption, improve the stand-alone throughput and system concurrency capability in the case of high concurrency and large traffic access, and is stable and reliable, thereby improving the user experience and saving costs.

[0075] Figure 3 Schematic diagram of the main process of querying inventory status data in the first cache in an optional embodiment of the present invention. In this example, the first cache includes: a first local cache unit and a distributed cache unit. The process of data query includes:

[0076] Step S301: receiving an inventory query request;

[0077] Step S302: querying the first local cache unit for inventory status data corresponding to the inventory query request;

[0078] Step S303: Determine whether a result is found in the first local cache unit. If so, jump to step S307; otherwise, jump to step S304;

[0079] Step S304: querying the distributed cache unit for inventory status data corresponding to the inventory query request;

[0080] Step S305: Determine whether the query result is found in the distributed cache unit. If so, jump to step S307; otherwise, jump to step S306;

[0081] Step S306: query the second cache for inventory status data corresponding to the inventory query request, and write the queried inventory status data to the first local cache unit;

[0082] Step S307: Use the queried inventory status data as result data.

[0083] The characteristics of the caller are that the number of interface calls is large, the coverage of items is large, and a certain delay in the availability of goods can be accepted. Due to the large scope of query objects and the space limitations of local memory, caching the inventory status data of all query objects locally often cannot meet the needs. In this example, the secondary cache mechanism of "local cache unit + distributed cache unit" is adopted to avoid the problem of affecting query performance due to the large number of distributed cache queries.

[0084] The first local cache unit can use Guava (a Java library based on open source), Caffeine (a rewritten version of Guava cache using Java8), etc., and the distributed cache unit can use Redis, memcached (a distributed memory object cache system), etc. The inventory status data in the first local cache unit and the distributed cache unit can be set to expire within a preset time. When no results are found in the first local cache unit, continue to query the distributed cache unit, and write back to the first local cache unit after obtaining the results from the distributed cache unit. When writing back, a synchronous write-back method can be used to improve the timeliness of cached data. Adding a layer of the first local cache unit is first to relieve the query pressure of the distributed cache unit, cache some data in a short time to prevent it from being penetrated to the distributed cache unit in the case of high concurrency and high traffic access, thereby avoiding the problem of excessive QPS (number of requests or queries per second) affecting query performance due to the large query volume of the distributed cache unit, and secondly to improve the query speed and trade space for time.

[0085] Optionally, the first local cache unit includes: a preset caller directory, and cache records of each query object corresponding to the preset caller; the cache records are stored in a key-value pair format, wherein the key is the query object identifier and the value is the inventory status data corresponding to the query object. Determining whether there is inventory status data corresponding to the inventory query request in the first local cache unit includes: determining whether there is a caller identifier of the inventory query request in the preset caller directory; if so, obtaining the inventory status data corresponding to the query object identifier of the inventory query request from the cache record; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the first local cache unit.

[0086] The query object can be selectively defined according to the actual situation, for example, each item is used as a query object, or items in a certain administrative area are used as query objects, or items in each channel are used as query objects, etc. The query object identifier is a unique representation of the query object. Optionally, the query object identifier includes: the item identifier of the inventory query request, the channel (such as item procurement channel, item sales channel, etc.) identifier, and the address identifier. Exemplarily, "Sku (Stock Keeping Unit, inventory unit) + channel + fourth-level address of administrative division" is used as the query object identifier.

[0087] By caching the preset caller directory, the inventory query request of the cached caller can be effectively intercepted. In this example, the preset caller directory can be used to add callers, delete callers, modify callers, etc. according to actual conditions, which is scalable.

[0088] Optionally, the third cache includes: a second local cache unit for storing hotspot item identifiers, and a third local cache unit for storing inventory status data of hotspot items. Determining whether the inventory status data corresponding to the inventory query request exists in the third cache includes: determining whether the item identifier of the inventory query request exists in the second local cache unit; if so, obtaining the inventory status data corresponding to the item identifier of the inventory query request from the third local cache unit; otherwise, determining that the inventory status data corresponding to the inventory query request does not exist in the hotspot cache.

[0089] Hotspot caching is caching the status results (i.e., inventory status data) for relatively hot items. The basis for determining hotspot items can be selectively set according to actual conditions. The item identifier of the query request refers to the unique representation of the item requested in the query request. For example, Figure 5 As shown, the second local cache unit is a layer of Guava, and the third local cache unit is a layer of Guava. Items that have existed in the second local cache unit for more than the preset time n (i.e., the time they have existed in the cache from the time they entered the cache to the time they were removed) and have not expired are considered hot items. The second layer is used to cache the status results of these hot items, for example, by SKU, channel, etc. The cached status results can be the inventory status of all regions.

[0090] Through local caching, some data can be cached for a short period of time to prevent the database from being penetrated in hot scenes with high concurrency and high traffic. The second cache is a local cache, such as Caffeine. The double-layer cache mechanism of "second local cache unit + third local cache unit" can improve the query efficiency of the third cache.

[0091] Optionally, the method of the embodiment of the present invention further includes: if the inventory status data corresponding to the item identifier of the inventory query request does not exist in the third local cache unit, writing a placeholder in the third local cache unit, and then using an asynchronous thread to load the inventory status data corresponding to the inventory query request from the database, so as to obtain the inventory status data as the result data and write the obtained inventory status data back to the third local cache unit.

[0092] By writing placeholders, it is possible to avoid querying the database separately for multiple query requests for the same query object, thus reducing system resource consumption. Figure 5 As shown, if the current hot Sku does not exist in the second layer of guava, a placeholder data is written before the Load method (a remote call method) is used to load the inventory status data, and then an asynchronous thread is initiated to call the inventory status interface of all regions. All regional interfaces refer to the inventory status interfaces under all addresses of a certain item. The Load method has a locking mechanism to ensure that only one concurrent request can enter the method call. Other calls to the same Sku get the placeholder data and find that it is an empty placeholder collection, and continue with subsequent calculations. Avoid consuming system resources by enabling asynchronous threads to call all regional interfaces at the same time due to concurrent requests for the same Sku.

[0093] Optionally, the method of the embodiment of the present invention further includes: monitoring hot items in the second local cache unit; for any hot item in the second local cache unit, when the hot item does not meet the hot condition, deleting the hot item.

[0094] Hotspot conditions can be selectively set according to actual conditions. For example, monitor the cache duration of each hot item in the second local cache unit; for any hot item in the second local cache unit, when the cache duration of any hot item is greater than a preset duration threshold, delete the hot item. For another example, monitor the total number of hot items in the second local cache unit; when the total number is greater than a preset number threshold, delete at least some of the hot items in the second local cache unit. For another example, monitor the number of requests for any item, and when the number of requests is greater than or equal to a preset number threshold, treat it as a hot item, and when the number of requests is less than the preset number threshold, delete it from the second local cache unit.

[0095] Exemplarily, the cache queue of hot items cached in the second local cache unit has a fixed expiration time for each cache data. When the inventory query request is for a certain SKU, the current time is recorded in the second local cache unit, and the length of time the SKU exists in the second local cache is calculated before the cache is invalidated and the SKU is removed from the cache. The cache expiration time ≥ the time the current SKU exists in the cache. When the SKU is not accessed within the expiration time, it is removed from the cache queue. In the case of high concurrency, due to the limited length of the cache queue, when there are many SKUs entering the cache queue, some SKUs in the cache queue will be removed.

[0096] By updating the hot items in the second local cache unit in real time, the query efficiency can be improved while reducing memory consumption.

[0097] Optionally, after obtaining the inventory status data corresponding to the inventory query request from the database, the method further includes: writing the obtained inventory status data into the first cache, such as Figure 3 As shown, when the acquired inventory status data indicates that the item requested by the inventory query is out of stock, the acquired inventory status data is written into the second cache.

[0098] The second cache is the local cache, such as Caffeine. The local cache can cache some data for a short period of time to prevent it from being penetrated to the database in hot scenes with high concurrency and high traffic. Figure 4 FIG. 1 is a schematic diagram of the main process of querying inventory status data in the second cache in an embodiment of the present invention. Figure 4 As shown, in step S401, an inventory query request is received, and in step S402, the inventory status data corresponding to the inventory query request is queried in the second cache. Step S403, determine whether a result is found; if so, jump to step S405, otherwise jump to step S404. Step S404, load the inventory status data from the database and write it into the second cache. Step S405, use the queried inventory status data as the result data.

[0099] When writing back, you can use synchronous write-back or asynchronous write-back. Synchronous and asynchronous focus on the message communication mechanism (synchronous communication / asynchronous communication). The so-called synchronization means that when a call is issued, the call will not return before the result is obtained. But once the call returns, the return value is obtained. In other words, the caller actively waits for the call result. Asynchronous is the opposite. The call returns directly after it is issued, so no result is returned. In other words, when an asynchronous process call is issued, the caller will not get the result immediately. Instead, after the call is issued, the callee notifies the caller through status and notification, or handles the call through a callback function. For example, synchronous write-back is used when writing data to the local cache, and asynchronous write-back is used when writing data to the distributed cache to reduce resource consumption.

[0100] After acquiring and storing data from the database, the acquired inventory status data is written into the first cache or the second cache, and subsequent identical inventory query requests can be responded to through the first cache or the second cache, thereby reducing system resource consumption.

[0101] According to a second aspect of an embodiment of the present invention, a device for implementing the above method is provided.

[0102] Figure 6 Schematic diagram of the main modules of the data query device based on multi-level cache according to an embodiment of the present invention. Figure 6 As shown, the data query device 600 based on multi-level cache includes:

[0103] A request receiving module 601 receives an inventory query request;

[0104] The cache query module 602 determines whether there is inventory status data corresponding to the inventory query request in the cache; the cache includes: a first cache, a second cache, and a third cache; the first cache is used to cache the inventory status data of the preset caller, the second cache is used to cache the inventory status data of out-of-stock items; the third cache is used to cache the inventory status data of hot items;

[0105] Result determination module 603, if yes, uses the inventory status data as result data; otherwise, obtains basic data corresponding to the inventory query request from the database, determines the inventory status data according to the basic data, and uses the inventory status data as result data.

[0106] Optionally, the cache query module determines whether there is inventory status data corresponding to the inventory query request in the cache, including:

[0107] Determine whether there is inventory status data corresponding to the inventory query request in the first cache; if so, use the inventory status data as the result data; otherwise, determine whether there is inventory status data corresponding to the inventory query request in the second cache;

[0108] If yes, use the inventory status data as the result data; otherwise, determine whether there is inventory status data corresponding to the inventory query request in the third cache;

[0109] If so, the inventory status data is used as the result data; otherwise, it is determined that there is no inventory status data corresponding to the inventory query request in the cache.

[0110] Optionally, the first cache includes: a first local cache unit and a distributed cache unit;

[0111] The cache query module determines whether there is inventory status data corresponding to the inventory query request in the first cache, including: determining whether there is inventory status data corresponding to the inventory query request in the first local cache unit; if so, using the inventory status data as the result data; otherwise, determining whether there is inventory status data corresponding to the inventory query request in the distributed cache unit; if so, writing the inventory status data into the first local cache unit, and using the inventory status data as the result data; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the first cache.

[0112] Optionally, the first local cache unit includes: a preset caller directory, and cache records of each query object corresponding to the preset caller; the cache records are stored in a key-value pair format, wherein the key is the query object identifier and the value is the inventory status data corresponding to the query object;

[0113] The cache query module determines whether there is inventory status data corresponding to the inventory query request in the first local cache unit, including: determining whether there is a caller identifier of the inventory query request in the preset caller directory; if so, obtaining the inventory status data corresponding to the query object identifier of the inventory query request from the cache record; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the first local cache unit.

[0114] Optionally, the third cache includes: a second local cache unit for storing hotspot item identifiers, and a third local cache unit for storing inventory status data of hotspot items;

[0115] The cache query module determines whether there is inventory status data corresponding to the inventory query request in the third cache, including: determining whether there is an item identifier of the inventory query request in the second local cache unit; if so, obtaining the inventory status data corresponding to the item identifier of the inventory query request from the third local cache unit; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the hotspot cache.

[0116] Optionally, the cache query module is also used to: if the inventory status data corresponding to the item identifier of the inventory query request does not exist in the third local cache unit, write a placeholder in the third local cache unit, and then use an asynchronous thread to load the inventory status data corresponding to the inventory query request from the database, so as to obtain the inventory status data as the result data and write the obtained inventory status data back to the third local cache unit.

[0117] Optionally, the cache query module is further used to: monitor the hot items in the second local cache unit; for any hot item in the second local cache unit, when the hot item does not meet the hot condition, delete the hot item.

[0118] Optionally, the result determination module is further used to: after obtaining inventory status data corresponding to the inventory query request from the database, write the obtained inventory status data into the first cache; and, when the obtained inventory status data indicates that the item requested by the inventory query is out of stock, write the obtained inventory status data into the second cache.

[0119] Optionally, the query object identifier includes: an item identifier, a channel identifier, and an address identifier of the inventory query request.

[0120] According to a third aspect of an embodiment of the present invention, there is provided an electronic device for querying data based on a multi-level cache, comprising:

[0121] one or more processors;

[0122] a storage device for storing one or more programs,

[0123] When the one or more programs are executed by the one or more processors, the one or more processors implement the method provided in the first aspect of the embodiment of the present invention.

[0124] According to a fourth aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored, and when the program is executed by a processor, the method provided by the first aspect of the embodiment of the present invention is implemented.

[0125] Figure 7An exemplary system architecture 700 is shown to which a data query method based on a multi-level cache or a data query device based on a multi-level cache according to an embodiment of the present invention can be applied.

[0126] like Figure 7 As shown, system architecture 700 may include terminal devices 701, 702, 703, network 704 and server 705. Network 704 is used to provide a medium for communication links between terminal devices 701, 702, 703 and server 705. Network 704 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.

[0127] Users can use terminal devices 701, 702, and 703 to interact with server 705 through network 704 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 701, 702, and 703, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only examples).

[0128] The terminal devices 701 , 702 , and 703 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.

[0129] The server 705 may be a server that provides various services, such as a backend management server (only for example) that supports shopping websites browsed by users using the terminal devices 701, 702, and 703. The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results (such as target push information, product information - only for example) to the terminal device.

[0130] It should be noted that the data query method based on multi-level cache provided in the embodiment of the present invention is generally executed by the server 705 , and accordingly, the data query device based on multi-level cache is generally arranged in the server 705 .

[0131] It should be understood that Figure 7 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.

[0132] Reference below Figure 8 , which shows a schematic diagram of the structure of a computer system 800 of a terminal device suitable for implementing an embodiment of the present invention. Figure 8 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0133] like Figure 8As shown, the computer system 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage part 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the system 800 are also stored. The CPU 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.

[0134] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed, so that a computer program read therefrom is installed into the storage section 808 as needed.

[0135] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the central processing unit (CPU) 801, the above-mentioned functions defined in the system of the present invention are executed.

[0136] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0137] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0138] The modules involved in the embodiments of the present invention can be implemented by software or hardware. The modules described can also be set in a processor, for example, it can be described as: a processor includes: a request receiving module, receiving an inventory query request; a cache query module, judging whether there is inventory status data corresponding to the inventory query request in the cache; the cache includes: a first cache, a second cache, and a third cache; the first cache is used to cache the inventory status data of the preset caller, the second cache is used to cache the inventory status data of out-of-stock items; the third cache is used to cache the inventory status data of hot items; a result determination module, if yes, the inventory status data is used as the result data; otherwise, the basic data corresponding to the inventory query request is obtained from the database, the inventory status data is determined according to the basic data, and the inventory status data is used as the result data. Among them, the names of these modules do not constitute a limitation on the module itself in some cases, for example, the request receiving module can also be described as "a module for judging whether there is inventory status data corresponding to the inventory query request in the cache".

[0139] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs. When the above one or more programs are executed by a device, the device includes: receiving an inventory query request; determining whether there is inventory status data corresponding to the inventory query request in the cache; the cache includes: a first cache, a second cache, and a third cache; the first cache is used to cache the inventory status data of the preset caller, the second cache is used to cache the inventory status data of out-of-stock items; the third cache is used to cache the inventory status data of hot items; if yes, the inventory status data is used as the result data; otherwise, the basic data corresponding to the inventory query request is obtained from the database, the inventory status data is determined according to the basic data, and the inventory status data is used as the result data.

[0140] According to the technical solution of the embodiment of the present invention, a multi-level cache mechanism including a first cache, a second cache and a third cache is adopted, which can improve the single-machine throughput and system concurrency capability under high concurrency and large-traffic access conditions, be stable and reliable, thereby improving user experience and saving costs.

[0141] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A data query method based on multi-level cache, characterized in that: include: Receive inventory query requests; Determining whether there is inventory status data corresponding to the inventory query request in the cache; The cache includes: a first cache, a second cache, and a third cache; the first cache is used to cache the inventory status data of the preset caller, the second cache is used to cache the inventory status data of out-of-stock items; the third cache is used to cache the inventory status data of hot items; If yes, use the inventory status data as result data; otherwise, obtain the inventory status data corresponding to the inventory query request from the database as result data, write the inventory status data into the first cache, and write the inventory status data into the second cache when the inventory status data indicates that the item requested by the inventory query is out of stock; The first cache includes: a first local cache unit and a distributed cache unit; the inventory status data in the first local cache unit and the distributed cache unit are set to be invalid within a preset time; when no result is found in the first local cache unit, continue to query the distributed cache unit, and write the result back to the first local cache unit after obtaining it from the distributed cache unit.

2. The method according to claim 1, characterized in that Determining whether there is inventory status data corresponding to the inventory query request in the cache includes: Determine whether there is inventory status data corresponding to the inventory query request in the first cache; if so, use the inventory status data as the result data; otherwise, determine whether there is inventory status data corresponding to the inventory query request in the second cache; If yes, use the inventory status data as the result data; otherwise, determine whether there is inventory status data corresponding to the inventory query request in the third cache; If so, the inventory status data is used as the result data; otherwise, it is determined that there is no inventory status data corresponding to the inventory query request in the cache.

3. The method according to claim 2, characterized in that Determining whether there is inventory status data corresponding to the inventory query request in the first cache includes: determining whether there is inventory status data corresponding to the inventory query request in the first local cache unit; if so, using the inventory status data as the result data; otherwise, determining whether there is inventory status data corresponding to the inventory query request in the distributed cache unit; if so, writing the inventory status data into the first local cache unit, and using the inventory status data as the result data; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the first cache.

4. The method according to claim 3, characterized in that The first local cache unit includes: a preset caller directory, and cache records of each query object corresponding to the preset caller; the cache records are stored in a key-value pair format, wherein the key is the query object identifier and the value is the inventory status data corresponding to the query object; Determining whether there is inventory status data corresponding to the inventory query request in the first local cache unit includes: determining whether there is a caller identifier of the inventory query request in the preset caller directory; if so, obtaining inventory status data corresponding to the query object identifier of the inventory query request from the cache record; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the first local cache unit.

5. The method according to claim 2, characterized in that The third cache includes: a second local cache unit for storing hotspot item identifiers, and a third local cache unit for storing inventory status data of hotspot items; Determining whether there is inventory status data corresponding to the inventory query request in the third cache includes: determining whether there is an item identifier of the inventory query request in the second local cache unit; if so, obtaining the inventory status data corresponding to the item identifier of the inventory query request from the third local cache unit; otherwise, determining that there is no inventory status data corresponding to the inventory query request in the third cache.

6. The method according to claim 5, characterized in that Also includes: If the inventory status data corresponding to the item identifier of the inventory query request does not exist in the third local cache unit, a placeholder is written in the third local cache unit, and then the inventory status data corresponding to the inventory query request is loaded from the database using an asynchronous thread, the obtained inventory status data is used as the result data and the obtained inventory status data is written back to the third local cache unit.

7. The method according to claim 5, characterized in that Also includes: Monitor the hot items in the second local cache unit; for any hot item in the second local cache unit, when the hot item does not meet the hot condition, delete the hot item.

8. The method according to claim 4, characterized in that The query object identifier includes: the item identifier, channel identifier, and address identifier of the inventory query request.

9. A data query device based on multi-level cache, characterized in that: include: A request receiving module receives inventory query requests; A cache query module, determining whether there is inventory status data corresponding to the inventory query request in the cache; The cache includes: a first cache, a second cache, and a third cache; the first cache is used to cache the inventory status data of the preset caller, the second cache is used to cache the inventory status data of out-of-stock items; the third cache is used to cache the inventory status data of hot items; a result determination module, if yes, taking the inventory status data as result data; otherwise, obtaining the inventory status data corresponding to the inventory query request from the database as result data, writing the inventory status data into a first cache, and, when the inventory status data indicates that the item requested by the inventory query is out of stock, writing the inventory status data into a second cache; The first cache includes: a first local cache unit and a distributed cache unit; the inventory status data in the first local cache unit and the distributed cache unit are set to be invalid within a preset time; when no result is found in the first local cache unit, continue to query the distributed cache unit, and write the result back to the first local cache unit after obtaining it from the distributed cache unit.

10. A data query electronic device based on multi-level cache, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 8.

11. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.

Citation Information

Patent Citations

  • Multi-level data caching method and device

    CN109977129A