Business processing method, device, electronic device and readable medium
By using the target change identifier to determine the database change situation, the problem of inconsistent query results in the cache and query results in the database is solved, ensuring the latest and accurate results.
Patent Information
- Application Number
- CN202311251514.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-26
- Publication Date
- 2025-04-04
AI Technical Summary
The results obtained by querying in the cache are inconsistent with the results obtained by querying in the database, resulting in unpredictable losses.
Determine the database change situation through the target change identification, so as to decide whether to obtain business results from the cache or from the database, and update the cache after obtaining the results in the database.
Ensure that the results obtained are the latest and accurate data, thereby solving the problem of inconsistent cache and database results.
Smart Images

Figure CN119759959A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of Internet technology, and in particular to a business processing method, device, electronic device and readable medium. Background Art
[0002] In order to quickly respond to the client's query request, the web system introduces a cache mechanism before querying the database. For example, the web system first queries the cache based on the query request. If no result is found, it queries the database.
[0003] As the project becomes larger and larger, the database needs to be constantly updated. In order to ensure data consistency between the cache and the database, the relevant technology updates the cache immediately after the database is updated. However, the cache update delay is high, and it is easy for the query results in the cache to be old data or erroneous data. Therefore, the query results in the cache are inconsistent with the results in the database. However, even if the query results are inconsistent, there will be no error, and it is difficult to be discovered, resulting in unpredictable losses. Summary of the invention
[0004] In order to solve or partially solve the problems existing in the related art, the present application provides a business processing method, device, electronic device and readable medium, which can solve the problem that the query results obtained in the cache are inconsistent with the query results obtained in the database.
[0005] A first aspect of the present application provides a service processing method, the method comprising:
[0006] When a business request is received, a target change identifier corresponding to the business request is obtained; wherein the target change identifier is an identifier configured for a database change situation;
[0007] According to the target change identifier, it is determined to obtain the business result corresponding to the business request from the cache or from the database, wherein obtaining the business result from the database also includes updating the cache with the business result.
[0008] In one implementation, determining, based on the change identifier, to obtain a service result corresponding to the service request from a cache or from a database includes:
[0009] According to the target change identifier, determining whether a business result corresponding to the business request in the database has changed;
[0010] If the business result corresponding to the business request in the database has not changed, determining to obtain the business result corresponding to the business request from the cache;
[0011] If the business result corresponding to the business request in the database changes, it is determined to obtain the business result corresponding to the business request from the database.
[0012] In one embodiment, the method further comprises:
[0013] When a change event occurs in the database, a change identifier is configured for the change event;
[0014] The change identification is stored in a configuration center.
[0015] In another embodiment, the configuration center includes a plurality of change identifiers, and the method further includes:
[0016] When a change event occurs in the database, determining a change identifier corresponding to the change event from the configuration center;
[0017] The value of the change identifier corresponding to the change event is updated to a first value.
[0018] In one implementation, the business request includes a business field; and judging whether a business result corresponding to the business request in the database has changed according to the target change identifier includes:
[0019] Concatenate the business field and the target change identifier to obtain a key field;
[0020] Check whether there is a business result matching the key field in the cache;
[0021] If there is a business result matching the key field in the cache, it is determined that the business result corresponding to the business request in the database has not changed;
[0022] If there is no business result matching the key field in the cache, it is determined that the business result corresponding to the business request in the database has changed.
[0023] In another embodiment, judging whether a business result corresponding to the business request in the database has changed according to the target change identifier includes:
[0024] Determining a value of the target change indicator;
[0025] If the value of the target change identifier is the second value, it is determined that the business result corresponding to the business request in the database has not changed;
[0026] If the value of the target change identifier is the first value, it is determined that the business result corresponding to the business request in the database has changed.
[0027] In one embodiment, after the cache is updated using the service result, the method further includes:
[0028] The value of the target change identifier is updated to a second value.
[0029] In one implementation, when a service request is received, obtaining a target change identifier corresponding to the service request includes:
[0030] When receiving a service request, extracting a service field from the service request;
[0031] According to the business field, obtaining a storage path of the target change identifier in the configuration center;
[0032] The target change identifier is obtained from the configuration center using the storage path.
[0033] A second aspect of the present application provides a service processing device, the device comprising:
[0034] A target change identifier acquisition module, used for acquiring a target change identifier corresponding to a business request when receiving the business request; wherein the target change identifier is an identifier configured for a database change situation;
[0035] The business processing module is used to determine whether to obtain the business result corresponding to the business request from the cache or from the database according to the target change identifier, wherein obtaining the business result from the database also includes updating the cache with the business result.
[0036] A third aspect of the present application provides an electronic device, including:
[0037] Processor; and
[0038] The memory stores executable codes thereon, and when the executable codes are executed by the processor, the processor is caused to execute the method as described above.
[0039] A fourth aspect of the present application provides a computer-readable storage medium having executable code stored thereon. When the executable code is executed by a processor of an electronic device, the processor is caused to execute the method as described above.
[0040] The technical solution provided by this application may have the following beneficial effects:
[0041] The solution provided by the present application, when receiving a business request, obtains the target change identifier corresponding to the business request; wherein the target change identifier is an identifier configured for the database change situation; according to the target change identifier, it is determined to obtain the business result corresponding to the business request from the cache or from the database, wherein obtaining the business result from the database also includes updating the cache with the business result. The present application can determine the database change situation through the target change identifier, so as to determine to obtain the result from the cache based on the database change situation, or determine to obtain the result from the database, and update the result from the database to the cache after obtaining the result, so that it can be guaranteed that the obtained result is the latest and accurate data, thereby solving the problem of inconsistency between the query results obtained in the cache and the query results obtained in the database.
[0042] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] The above and other objects, features and advantages of the present application will become more apparent by describing in more detail exemplary embodiments of the present application in conjunction with the accompanying drawings, wherein the same reference numerals generally represent the same components in the exemplary embodiments of the present application.
[0044] Figure 1 It is a flowchart of a business processing method shown in an embodiment of the present application;
[0045] Figure 2 is another flowchart of the business processing method shown in an embodiment of the present application;
[0046] Figure 3 It is a business processing flow chart of using the current timestamp to define a change identifier shown in an embodiment of the present application;
[0047] Figure 4 It is a business processing flow chart of a change identifier defined by a specific numerical value shown in an embodiment of the present application;
[0048] Figure 5 is a schematic diagram of the structure of a service processing device shown in an embodiment of the present application;
[0049] Figure 6 It is a schematic diagram of the structure of an electronic device shown in an embodiment of the present application. DETAILED DESCRIPTION
[0050] The embodiments of the present application will be described in more detail below with reference to the accompanying drawings. Although the embodiments of the present application are shown in the accompanying drawings, it should be understood that the present application can be implemented in various forms and should not be limited by the embodiments described herein. On the contrary, these embodiments are provided to make the present application more thorough and complete, and to fully convey the scope of the present application to those skilled in the art.
[0051] The terms used in this application are for the purpose of describing specific embodiments only and are not intended to limit this application. The singular forms of "a", "said" and "the" used in this application and the appended claims are also intended to include plural forms unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.
[0052] It should be understood that although the terms "first", "second", "third", etc. may be used in this application to describe various information, this information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of this application, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Thus, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features. In the description of this application, the meaning of "multiple" is two or more, unless otherwise clearly and specifically defined.
[0053] In the related art, the web system first queries the cache to see if there are any results that meet the conditions based on the fields of the query request. If no results are found, the database is queried to obtain the results. In order to ensure data consistency between the cache and the database, the cache is updated immediately after the database is updated in the related art. However, the cache update delay is high, and it is easy for the results obtained from the query in the cache to be old data or erroneous data. Therefore, the results obtained from the query in the cache are inconsistent with the results obtained from the query in the database. However, even if the query result is inconsistent, there will be no error, and it is difficult to be discovered, resulting in unpredictable losses.
[0054] In response to the above problems, an embodiment of the present application provides a business processing method, which can determine the database change status through a target change identifier, so as to determine whether to obtain results from the cache based on the database change status, or determine to obtain results from the database, and update the cache after obtaining the results from the database. Therefore, it can be ensured that the results obtained are the latest and accurate data, thereby solving the problem of inconsistency between the query results obtained in the cache and the query results obtained in the database.
[0055] The technical solution of the embodiments of the present application is described in detail below with reference to the accompanying drawings.
[0056] Figure 1 It is a flowchart of the business processing method shown in an embodiment of the present application.
[0057] See also Figure 1 , the business processing method of this application includes:
[0058] S110, when a business request is received, obtaining a target change identifier corresponding to the business request; wherein the target change identifier is an identifier configured for a database change situation.
[0059] The embodiments of the present application can be applied to a business system, which can be a web system. The business system, as a server, can communicate with a client. In actual applications, when the client detects a business field input by a user, it can send the business field as a business request to the business system, and the business system processes the business request.
[0060] Among them, the business request can be a query request, and the business field can be a query field feild_condition used as a query condition. For example, the query field can be id, product_category (product category), sku (stockkeeping unit), name (name), gender (gender), age (age), city (city) and other fields.
[0061] The business system may include a cache, a database, and a configuration center. Before querying the cache, the business system may first obtain the target change identifier corresponding to the business request from the configuration center. The target change identifier is an identifier configured for database changes, so the target change identifier can reflect the specific changes in the database.
[0062] S111, determining to obtain a business result corresponding to the business request from a cache or a database according to a target change identifier, wherein obtaining the business result from the database also includes updating the cache with the business result.
[0063] The business system can determine the database change status based on the target change identifier, and then determine whether to obtain the business result corresponding to the business request from the cache, or determine to obtain the business result corresponding to the business request from the database based on the database change status. After obtaining the business result from the database, the business result can be updated to the cache, so it can be guaranteed that the result obtained is the latest and accurate data, thereby solving the problem of inconsistency between the query results obtained in the cache and the query results obtained in the database.
[0064] After obtaining the business result from the cache or from the database, the business system can return the business result for the business request to the client.
[0065] As can be seen from this example, the solution provided by the present application obtains the target change identifier corresponding to the business request when a business request is received; wherein the target change identifier is an identifier configured for database changes; according to the target change identifier, it is determined to obtain the business result corresponding to the business request from the cache or from the database, wherein obtaining the business result from the database also includes updating the cache with the business result. The present application can determine the database change status through the target change identifier, so as to determine to obtain the result from the cache based on the database change status, or determine to obtain the result from the database, and update the cache after obtaining the result from the database, so that it can be guaranteed that the obtained result is the latest and accurate data, thereby solving the problem of inconsistency between the query results obtained in the cache and the query results obtained in the database.
[0066] Figure 2 It is another flowchart of the business processing method shown in an embodiment of the present application.
[0067] See also Figure 2 , the business processing method of this application includes:
[0068] S210, when a business request is received, obtaining a target change identifier corresponding to the business request; wherein the target change identifier is an identifier configured for a database change situation.
[0069] The embodiment of the present application can be applied to a business system, wherein the business system can be connected to a client for communication. In practical applications, when the client detects a business field input by a user, the client can send the business field as a business request to the business system, and the business system processes the business request. The business request can be a query request, and the business field can be a query field feild_condition used as a query condition.
[0070] The business system may include a cache, a database, and a configuration center. Before querying the cache, the business system may first obtain the target change identifier corresponding to the business request from the configuration center. The target change identifier is an identifier configured for database changes, so the target change identifier can reflect the specific changes in the database. For example, the target change identifier can reflect whether the business result corresponding to the business request in the database has changed.
[0071] Among them, the business results can be presented in the form of a data table. If a business result in the database changes, that is, a data table in the database changes, it means that the database performs a DML (Data Manipulation Language) operation on the row data of the data table, resulting in the change of the row data of the data table, and / or it means that the database performs a DDL (Data Definition Language) operation on the column data of the data table, resulting in the change of the column data of the data table.
[0072] The row data of the data table may be all information data of a certain object, and the column data of the data table may be certain information data of all objects. For example, the data table is a user list, and the user list contains user information of multiple users. The user information may include id, name, gender, age, and city fields. Then the user list may be as shown in Table 1 below:
[0073] id name gender age city 1 Little Red 2 18 City A 2 Little Orange 2 18 City A 3 Little Yellow 2 18 City A …… …… …… …… ……
[0074] Table 1
[0075] In the gender field, 1 indicates male and 2 indicates female.
[0076] After the database performs DML operations on the row data in Table 1, a row of data in Table 1 is changed. For example, the city where Xiaocheng is located is changed from City A to City B; or Xiaohong's user information is deleted from Table 1; or Xiaolu's user information is added to Table 1, and so on.
[0077] After the database performs DDL operations on the column data of Table 1, the data in a column of Table 1 is changed. For example, a new column "hobby" is added to Table 1, and the hobby information of Xiaohong, Xiaocheng and Xiaohuang can be reflected in Table 1; or, a column "age" is deleted from Table 1, and the age information of Xiaohong, Xiaocheng and Xiaohuang is deleted from Table 1, and so on.
[0078] In one embodiment, the method may further include:
[0079] When a change event occurs in the database, a change identifier is configured for the change event; and the change identifier is stored in a configuration center.
[0080] The business system can monitor binlog to detect whether a change event has occurred in the database. A change event can refer to a change in a data table in the database, such as a change in the city where Xiaocheng is located in Table 1 of the database. Among them, binlog is the binary log file of the database, which is used to record the change data of the database. When the binlog record is monitored to have change data, a change event in the database is detected, and the business system can configure a change flag for the change event.
[0081] In some embodiments, when a change event is detected in the database, the business system can obtain the current timestamp Snewest time and configure Snewest time as the change identifier of the change event. For example, at 10:30 on September 1, 2023, the business system detects that the city where Xiaocheng is located in Table 1 of the database has changed from City A to City B, then obtains the current timestamp Snewest time: "202309011030", and the business system can configure "202309011030" as the change identifier of Table 1, or as the change identifier of Xiaocheng's user information, or as the change identifier of the city where Xiaocheng is located.
[0082] After the configuration obtains the change identifier Snewest time, the business system can store the change identifier Snewest time in a public configuration center. For example, the configuration center can be nacos (dynamic naming and configuration service). Nacos is a dynamic service discovery, configuration management, and service management platform that is easy to build cloud native applications.
[0083] In another embodiment, the configuration center includes multiple change identifiers, and the method may further include:
[0084] When a change event occurs in the database, a change identifier corresponding to the change event is determined from the configuration center; and a value of the change identifier corresponding to the change event is updated to a first value.
[0085] The business system can pre-configure corresponding change identifiers for different change events, and then store these change identifiers in the configuration center (such as nacos), and at the same time establish a mapping relationship between change events and change identifiers. For example, assuming that the database contains data tables a, b, and c, the business system can pre-configure corresponding change identifiers for data tables a, b, and c. a , L b , L c , change the label L a , L b , L cStored in the configuration center, you can also create a data table a and a change identifier L a The mapping relationship between data table b and change identifier L b The mapping relationship between the data table c and the change identifier L c The mapping relationship.
[0086] When the binlog record is monitored to have change data, a change event in the database is detected. Then, based on the mapping relationship between the change event and the change identifier, the change identifier L corresponding to the current change event is determined from the configuration center, and then the value of the change identifier L is updated to a first value in the configuration center. The first value indicates that a change event has occurred in the database, that is, the first value can be used to characterize a change in a business result in the database.
[0087] Exemplarily, the business system detects that the city where Xiaocheng is located in Table 1 of the database has changed. Based on the mapping relationship between the change event and the change identifier, the change identifier corresponding to Table 1 is determined from the configuration center to be L1. At this time, the value of L1 is the second value, and the second value indicates that no change event has occurred in the database, that is, the second value can be used to characterize that a business result in the database has not changed. Therefore, the business system can update the value of the change identifier L1 from the second value to the first value in the configuration center. For example, if the first value is set to "0" and the second value is set to "1", the business system can update the value of the change identifier L1 from "1" to "0".
[0088] In one implementation, when a service request is received, obtaining a target change identifier corresponding to the service request may include:
[0089] When a business request is received, a business field is extracted from the business request; based on the business field, a storage path of the target change identifier in the configuration center is obtained; and the target change identifier is obtained from the configuration center using the storage path.
[0090] When storing the currently configured change identifier (Snewest time) in the configuration center, or when storing multiple pre-configured change identifiers L in the configuration center, the business system can obtain the storage path of the change identifier in the configuration center, and then establish a mapping relationship between the business field and the storage path of the change identifier in the code configuration, as shown in the following code segment:
[0091] config={
[0092] "city:A:gender2:age:18":" / config / cache / user / city:A:gender2:age:18",
[0093] "city:B:gender2:age:18":" / config / cache / user / city:B:gender2:age:18",
[0094] ...}.
[0095] When receiving a business request from a client, the business system may extract business fields from the business request, and then obtain the storage path of the target change identifier corresponding to the current business field in the configuration center from the code configuration based on the mapping relationship between the business field and the storage path of the change identifier, so as to obtain the target change identifier from the configuration center using the storage path.
[0096] In an example, assuming that the user wants to query "18-year-old girls in city A", the business field extracted by the business system from the business request can be "city:A:gender2:age:18". According to the mapping relationship between the business field and the storage path of the change identifier, the storage path of the target change identifier corresponding to "city:A:gender2:age:18" in the configuration center is obtained from the code configuration. The storage path is " / config / cache / user / city:A:gender2:age:18". Therefore, the storage path " / config / cache / user / city:A:gender2:age:18" is used to obtain the target change identifier corresponding to the business request "18-year-old girl in city A" from the configuration center.
[0097] S211, judging whether the business result corresponding to the business request in the database has changed according to the target change identifier.
[0098] The target change identifier is an identifier configured for database changes. Therefore, the business system can determine whether the business result corresponding to the current business request in the database has changed based on the target change identifier, so as to determine whether to obtain the business result corresponding to the current business request from the cache or from the database based on whether the business result corresponding to the current business request in the database has changed.
[0099] In one implementation, the business request includes a business field; judging whether a business result corresponding to the business request in the database has changed according to the target change identifier may include:
[0100] The business field and the target change identifier are concatenated to obtain the key field; whether there is a business result matching the key field in the cache; if there is a business result matching the key field in the cache, it is determined that the business result corresponding to the business request in the database has not changed; if there is no business result matching the key field in the cache, it is determined that the business result corresponding to the business request in the database has changed.
[0101] When the change identifier is defined by the current timestamp Snewest time, the business system can concatenate the business field feild_condition with the target change identifier Snewest time to obtain the key field key: "cache+feild_condition+Snewest time", where cache represents cache, feild_condition represents the business field, and Snewest time represents the target change identifier. The business system queries the cache based on the key field key to see if there is a business result that matches the key field key.
[0102] If there is a business result matching the key field key in the cache, it means that the business result corresponding to the business request in the database has not changed, that is, the business result obtained by querying in the cache is consistent with the business result obtained by querying in the database, so the business result corresponding to the business request can be directly queried in the cache. As an example, assuming that the business field is "city:A:gender2:age:18", the target change identifier Snewest time is "202301010000", the business system concatenates the business field and the target change identifier to obtain the key field key: "cache:city:A:gender2:age:18:202301010000", if it is found that there is a table 1 matching "cache:city:A:gender2:age:18:202301010000" in the cache, it can be determined that Table 1 in the database has not changed, that is, Table 1 obtained by querying in the cache is consistent with Table 1 obtained by querying in the database, so Table 1 can be directly queried in the cache.
[0103] If there is no business result matching the key field key in the cache, it means that the business result corresponding to the business request in the database has changed, that is, the business result obtained by querying in the cache is inconsistent with the business result obtained by querying in the database. Therefore, it is necessary to query in the database to obtain the business result corresponding to the business request. As an example, assuming that the business field is "city:A:gender2:age:18" and the target change identifier Snewest time is "202309011030", the business system concatenates the business field and the target change identifier to obtain the key field key: "cache:city:A:gender2:age:18:202309011030". If it is found that there is no business result matching "cache:city:A:gender2:age:18:202309011030" in the cache, it can be determined that Table 1 of the database has changed. For example, the city where Xiaocheng is located in Table 1 is changed from City A to City B, that is, Table 1 obtained by querying in the cache is inconsistent with Table 1 obtained by querying in the database. Therefore, it is necessary to query in the database to obtain the changed Table 1. For example, the changed Table 1 does not contain Xiaocheng's user information.
[0104] Compared with the related art in which the cache is updated immediately after the database is updated, due to the high cache update delay, it is easy to have the situation where the result obtained by querying in the cache is old data or wrong data. For example, in the related art, the web system queries the cache according to the field "city:A:gender2:age:18" of the query request, and the result obtained is Table 1. Table 1 still contains Xiaocheng's user information. However, the city where Xiaocheng is located in Table 1 of the database has changed, so the changed Table 1 in the database does not contain Xiaocheng's user information. Therefore, Table 1 obtained by querying in the cache is inconsistent with Table 1 obtained by querying in the database. However, in the related art, even if the query result is inconsistent, there will be no error, and it is difficult to be discovered, thus causing unpredictable losses.
[0105] In the embodiment of the present application, each time the database performs DDL / DML, the key field key that is equivalent to the query condition will become the latest. Therefore, when the cache is queried, there is no business result matching the key field key in the cache. Therefore, the business system needs to query the database, for example, query the database according to the business field to obtain the business result corresponding to the business request, thereby avoiding the situation of obtaining old data or erroneous data in the cache, and thus solving the problem of inconsistency between the query results obtained in the cache and the query results obtained in the database.
[0106] In another embodiment, judging whether a business result corresponding to a business request in a database has changed according to a target change identifier may include:
[0107] Determine the value of the target change identifier; if the value of the target change identifier is the second value, it is determined that the business result corresponding to the business request in the database has not changed; if the value of the target change identifier is the first value, it is determined that the business result corresponding to the business request in the database has changed.
[0108] When the change identifier is defined as a specific value, the business system may first determine the value of the target change identifier L, and then determine based on the value of the target change identifier L whether the business result corresponding to the business request in the database has changed.
[0109] If the value of the target change identifier L is the second value, it means that the business result corresponding to the business request in the database has not changed, so the business result obtained by querying in the cache is consistent with the business result obtained by querying in the database, so the business result corresponding to the business request can be directly queried in the cache according to the business field. As an example, assuming that the business field is "city:A:gender2:age:18", and the value of the target change identifier L is the second value 1, it can be determined that Table 1 in the database has not changed, that is, Table 1 obtained by querying in the cache is consistent with Table 1 obtained by querying in the database, so Table 1 can be directly queried in the cache according to the business field "city:A:gender2:age:18".
[0110] If the value of the target change identifier L is the first value, it means that the business result corresponding to the business request in the database has changed. Therefore, the business result obtained by querying in the cache is inconsistent with the business result obtained by querying in the database. Therefore, it is necessary to query in the database to obtain the business result corresponding to the business request. As an example, assuming that the business field is "city:A:gender2:age:18", and the value of the target change identifier L is the first value 0, it can be determined that Table 1 in the database has changed. For example, the city where Xiaocheng is located in Table 1 has changed from City A to City B, that is, Table 1 obtained by querying in the cache is inconsistent with Table 1 obtained by querying in the database. Therefore, it is necessary to query in the database to obtain the changed Table 1, and the changed Table 1 does not contain Xiaocheng's user information.
[0111] Compared with the related art in which the cache is updated immediately after the database is updated, due to the high cache update delay, it is easy to have the situation where the result obtained by querying in the cache is old data or wrong data. For example, in the related art, the web system queries the cache according to the field "city:A:gender2:age:18" of the query request, and the result obtained is Table 1. Table 1 still contains Xiaocheng's user information. However, the city where Xiaocheng is located in Table 1 of the database has changed, so the changed Table 1 in the database does not contain Xiaocheng's user information. Therefore, Table 1 obtained by querying in the cache is inconsistent with Table 1 obtained by querying in the database. However, in the related art, even if the query result is inconsistent, there will be no error, and it is difficult to be discovered, thus causing unpredictable losses.
[0112] In the embodiment of the present application, each time the database performs DDL / DML, the value of the corresponding change identifier is updated to the first value, and the first value is used to represent that a business result in the database has changed. Therefore, when it is determined that the value of the target change identifier is the first value, the business system can directly query the database, for example, directly query the database according to the business field, and obtain the business result corresponding to the business request, thereby avoiding the situation of obtaining old data or erroneous data in the cache, and thus solving the problem of inconsistency between the query results obtained in the cache and the query results obtained in the database.
[0113] It can be seen that, whether the embodiment of the present application adopts the current timestamp (Snewest time) to define the change identifier, or adopts a specific value (first value / second value) to define the change identifier, it can achieve the purpose of avoiding the situation of obtaining old data or erroneous data in the cache, and solving the problem that the query results obtained in the cache are inconsistent with the query results obtained in the database. In addition, the embodiment of the present application adopts a specific value to define the change identifier, which can also reduce the number of configuration times of the change identifier. It only needs to update the value of the change identifier, and there is no need to configure the latest change identifier Snewest time for the change event every time the database performs DDL / DML. Moreover, when it is determined that the value of the target change identifier is the first value, it is sufficient to directly query the database without querying the cache. Therefore, before returning the business result to the client, one IO operation can be saved, thereby improving business processing efficiency.
[0114] S212: If the business result corresponding to the business request in the database has not changed, determine to obtain the business result corresponding to the business request from the cache.
[0115] If a business result matching the key field key exists in the cache, or the value of the target change identifier is the second value, it means that the database has not performed a DML operation on the row data of the data table corresponding to the business request, and that the database has not performed a DDL operation on the column data of the data table corresponding to the business request. Therefore, it can be determined that the business result corresponding to the business request in the database has not changed. The business system can then directly obtain the business result corresponding to the business request from the cache without querying the database again, thereby achieving the purpose of improving business processing performance.
[0116] After obtaining the business result from the cache, the business system can return the business result for the business request to the client.
[0117] S213: If the business result corresponding to the business request in the database changes, determine to obtain the business result corresponding to the business request from the database, and use the business result to update the cache.
[0118] If there is no business result matching the key field key in the cache, or the value of the target change identifier is the first value, it means that the database has performed a DML operation on the row data of the data table corresponding to the business request, and / or, it means that the database has performed a DDL operation on the column data of the data table corresponding to the business request. Therefore, it can be determined that the business result corresponding to the business request in the database has changed. Then the business system needs to obtain the business result corresponding to the business request from the database, thereby avoiding the situation of obtaining old data or erroneous data in the cache and solving the problem of inconsistency between the query results obtained in the cache and the query results obtained in the database.
[0119] After obtaining the business results from the database, the business system can return the business results for the business request to the client.
[0120] Since the business result corresponding to the business request in the database has changed, the business result in the database is the latest and accurate data, while the business result in the current cache is old and erroneous data. For example, the changed Table 1 in the database does not contain Xiaocheng's user information, while Table 1 in the current cache still contains Xiaocheng's user information. Therefore, the business system needs to use the business results obtained from the database to update the cache. For example, the business results obtained from the database can be written into the cache to ensure the eventual consistency of the cache and the database.
[0121] In addition, in the related technology, after the database is updated, the old cached data can be deleted first, and then the new data can be written to the cache when the database is queried next time. It can be seen that each time the cache is updated in the related technology, two-step update operations are required. However, as the project becomes larger and larger, the new data will contain more and more index fields. Therefore, where the database involves updates, a large number of cache updates are required, which makes the maintenance cost of the project increasingly large.
[0122] The embodiment of the present application can determine the database change status through the target change identifier. When it is determined that the result of the client request in the database has changed, the new data obtained from the database can directly overwrite the old data in the update cache. Therefore, each time the cache is updated, only one update operation is required, thereby reducing the number of cache update operations. This not only avoids the disadvantages of requiring a large number of cache updates when the result of the client request in the database has changed, but also ensures the eventual consistency of the cache and the database.
[0123] In one implementation, after the cache is updated using the business result, the following steps may also be included:
[0124] Update the value of the target change flag to the second value.
[0125] After the business result is written to the cache, the business result in the cache is consistent with the business result in the database, and both are the latest and accurate data. Therefore, the value of the target change identifier can be updated to the second value, which indicates that the business result has been queried. In this way, the next time the query is made, the value of the target change identifier is the second value. It can be determined that the business result corresponding to the business request in the database has not changed, so the business result can be directly queried in the cache.
[0126] It can be seen from this example that the solution provided by the present application, when a business request is received, obtains the target change identifier corresponding to the business request; wherein the target change identifier is an identifier configured for the database change situation; based on the target change identifier, determines whether the business result corresponding to the business request in the database has changed; if the business result corresponding to the business request in the database has not changed, determines to obtain the business result corresponding to the business request from the cache; if the business result corresponding to the business request in the database has changed, determines to obtain the business result corresponding to the business request from the database, and uses the business result to update the cache. The present application can determine whether the result of the client request in the database has changed through the target change identifier, so that when it is determined that no change has occurred, the result of the client request can be directly obtained from the cache, thereby improving business processing performance; further, when it is determined that a change has occurred, the result of the client request is obtained from the database, thereby avoiding the situation of obtaining old data or erroneous data in the cache, and solving the problem of inconsistency between the query results obtained in the cache and the query results obtained in the database; further, the present application will directly overwrite the old data in the update cache with the new data obtained from the database, so each time the cache is updated, only one update operation is required, thereby reducing the number of cache update operations, which not only avoids the disadvantages caused by the need to update the cache in large quantities when the result of the client request in the database changes, but also ensures the ultimate consistency of the cache and the database.
[0127] In order to enable those skilled in the art to better understand the embodiments of the present application, the embodiments of the present application are described below through the following two examples.
[0128] Figure 3 This is a business process flow chart of using the current timestamp to define a change identifier as shown in an embodiment of the present application. The business process flow is as follows:
[0129] S1: The business system detects a database change event.
[0130] S2: The business system obtains the current timestamp Snewest time and configures Snewest time as the change identifier of the current change event.
[0131] S3, stores the change identifier Snewest time in the configuration center.
[0132] S301, when receiving a service request from a client, the service system extracts a service field feild_condition from the service request.
[0133] S302, obtaining the target change identifier Snewest time corresponding to the business request from the configuration center, for example, searching the storage path associated with the business field feild_condition in the code configuration, the storage path being the path where the target change identifier Snewest time is stored in the configuration center, so as to adopt the storage path to obtain the target change identifier Snewest time from the configuration center.
[0134] S303, the business system concatenates the business field feild_condition and the target change identifier Snewest time to obtain the key field key: "cache+feild_condition+Snewest time".
[0135] S304, query whether there is a business result matching the key field key in the cache; if so, jump to step S305a; if not, jump to step S305b.
[0136] S305a, if there is a business result matching the key field key in the cache, it means that the business result corresponding to the business request in the database has not changed, then the business system directly obtains the business result corresponding to the business request from the cache and proceeds to step S306a.
[0137] S306a, the business system returns the business result to the client, and the process ends.
[0138] S305b, if there is no business result matching the key field key in the cache, it means that the business result corresponding to the business request in the database has changed, then the business system needs to obtain the business result corresponding to the business request from the database and proceed to step S306b.
[0139] S306b, the business system returns the business result to the client and writes the business result into the cache, and the process ends.
[0140] Figure 4 This is a business process flow chart of a change identifier defined by a specific value shown in an embodiment of the present application. The business process flow is as follows:
[0141] S1', the business system detects a database change event.
[0142] S2', the configuration center includes multiple change identifiers L. For example, the business system pre-configures corresponding change identifiers L for different change events, and then stores multiple change identifiers L in the configuration center, so the business system can directly determine the change identifier L corresponding to the current change event in the configuration center.
[0143] S3', the business system updates the value of the change identifier L to the first value in the configuration center.
[0144] S401, when receiving a service request from a client, extracting a service field feild_condition from the service request.
[0145] S402, obtaining the target change identifier L corresponding to the business request from the configuration center, for example, searching the storage path associated with the business field feild_condition in the code configuration, the storage path being the path where the target change identifier L is stored in the configuration center, so as to adopt the storage path to obtain the target change identifier L from the configuration center.
[0146] S403, determining the value of the target change flag L.
[0147] S404a, if the value of the target change identifier L is the second value, it means that the business result corresponding to the business request in the database has not changed, then the business system directly obtains the business result corresponding to the business request from the cache and proceeds to step S405a.
[0148] S405a, the business system returns the business result to the client, and the process ends.
[0149] S404b, if the value of the target change identifier L is the first value, it means that the business result corresponding to the business request in the database has changed, then the business system needs to obtain the business result corresponding to the business request from the database, and flow to step S405b.
[0150] S405b, the business system returns the business result to the client, writes the business result into the cache, and updates the value of the target change identifier L to the second value, and the process ends.
[0151] Corresponding to the aforementioned application function implementation method embodiment, the present application also provides a business processing device, an electronic device, a computer-readable storage medium and corresponding embodiments.
[0152] Figure 5 It is a structural diagram of a business processing device shown in an embodiment of the present application.
[0153] See also Figure 5 , the application provides a service processing device, which may include:
[0154] The target change identifier acquisition module 501 is used to acquire the target change identifier corresponding to the business request when receiving the business request; wherein the target change identifier is an identifier configured for the database change situation;
[0155] The business processing module 502 is used to determine whether to obtain the business result corresponding to the business request from the cache or from the database according to the target change identifier, wherein obtaining the business result from the database also includes updating the cache with the business result.
[0156] In one implementation, the service processing module 502 may include:
[0157] The change judgment submodule is used to judge whether the business result corresponding to the business request in the database has changed according to the target change identifier;
[0158] A first business processing submodule, configured to determine to obtain the business result corresponding to the business request from the cache if the business result corresponding to the business request in the database has not changed;
[0159] The second business processing submodule is used to determine to obtain the business result corresponding to the business request from the database if the business result corresponding to the business request in the database changes.
[0160] In one embodiment, the device may further include:
[0161] A change identification configuration module is used to configure a change identification for a change event when a change event occurs in the database;
[0162] The change identification storage module is used to store the change identification in the configuration center.
[0163] In another embodiment, the configuration center includes a plurality of change identifiers, and the device may further include:
[0164] A change identification determination module is used to determine the change identification corresponding to the change event from the configuration center when a change event is detected in the database;
[0165] The first value updating module is used to update the value of the change identifier corresponding to the change event to a first value.
[0166] In one implementation, the service request includes a service field; the change determination submodule may include:
[0167] A splicing unit, used for splicing the business field and the target change identifier to obtain a key field;
[0168] A cache query unit is used to query whether there is a business result matching the key field in the cache;
[0169] A first unchanged determination unit, configured to determine that the business result corresponding to the business request in the database has not changed if there is a business result matching the key field in the cache;
[0170] The first change determination unit is configured to determine that a business result corresponding to the business request in the database has changed if there is no business result matching the key field in the cache.
[0171] In another embodiment, the change determination submodule may include:
[0172] A value determination unit, used to determine the value of the target change indicator;
[0173] A second unchanged determination unit, configured to determine that the business result corresponding to the business request in the database has not changed if the value of the target change identifier is a second value;
[0174] The second change determination unit is configured to determine that a business result corresponding to the business request in the database has changed if the value of the target change identifier is a first value.
[0175] In one embodiment, after updating the cache with the service result, the device may further include:
[0176] The second value updating module is used to update the value of the target change identifier to a second value.
[0177] In one implementation, the target change identification acquisition module 501 may include:
[0178] A business field extraction submodule is used to extract business fields from a business request when a business request is received;
[0179] The storage path acquisition submodule is used to obtain the storage path of the target change identifier in the configuration center according to the business field;
[0180] The target change identifier acquisition submodule is used to obtain the target change identifier from the configuration center using the storage path.
[0181] It can be seen from this example that the solution provided by the present application, when a business request is received, obtains the target change identifier corresponding to the business request; wherein the target change identifier is an identifier configured for the database change situation; based on the target change identifier, determines whether the business result corresponding to the business request in the database has changed; if the business result corresponding to the business request in the database has not changed, determines to obtain the business result corresponding to the business request from the cache; if the business result corresponding to the business request in the database has changed, determines to obtain the business result corresponding to the business request from the database, and uses the business result to update the cache. The present application can determine whether the result of the client request in the database has changed through the target change identifier, so that when it is determined that no change has occurred, the result of the client request can be directly obtained from the cache, thereby improving business processing performance; further, when it is determined that a change has occurred, the result of the client request is obtained from the database, thereby avoiding the situation of obtaining old data or erroneous data in the cache, and solving the problem of inconsistency between the query results obtained in the cache and the query results obtained in the database; further, the present application will directly overwrite the old data in the update cache with the new data obtained from the database, so each time the cache is updated, only one update operation is required, thereby reducing the number of cache update operations, which not only avoids the disadvantages caused by the need to update the cache in large quantities when the result of the client request in the database changes, but also ensures the ultimate consistency of the cache and the database.
[0182] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated again here.
[0183] Figure 6 It is a schematic diagram of the structure of an electronic device shown in an embodiment of the present application.
[0184] See also Figure 6 , the electronic device 600 includes a memory 610 and a processor 620 .
[0185] The processor 620 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc.
[0186] The memory 610 may include various types of storage units, such as system memory, read-only memory (ROM), and permanent storage devices. Among them, ROM can store static data or instructions required by the processor 620 or other modules of the computer. The permanent storage device may be a readable and writable storage device. The permanent storage device may be a non-volatile storage device that does not lose the stored instructions and data even after the computer is powered off. In some embodiments, the permanent storage device uses a large-capacity storage device (such as a magnetic or optical disk, flash memory) as a permanent storage device. In some other embodiments, the permanent storage device may be a removable storage device (such as a floppy disk, optical drive). The system memory may be a readable and writable storage device or a volatile readable and writable storage device, such as a dynamic random access memory. The system memory may store some or all instructions and data required by the processor at run time. In addition, the memory 610 may include any combination of computer-readable storage media, including various types of semiconductor storage chips (such as DRAM, SRAM, SDRAM, flash memory, programmable read-only memory), and disks and / or optical disks may also be used. In some embodiments, the memory 610 may include a readable and / or writable removable storage device, such as a laser disc (CD), a read-only digital versatile disc (such as a DVD-ROM, a double-layer DVD-ROM), a read-only Blu-ray disc, an ultra-density optical disc, a flash memory card (such as an SD card, a mini SD card, a Micro-SD card, etc.), a magnetic floppy disk, etc. The computer-readable storage medium does not include carrier waves and transient electronic signals transmitted wirelessly or wired.
[0187] The memory 610 stores executable codes, and when the executable codes are processed by the processor 620 , the processor 620 can execute part or all of the above-mentioned methods.
[0188] In addition, the method according to the present application may also be implemented as a computer program or a computer program product, which includes computer program code instructions for executing some or all of the steps in the above method of the present application.
[0189] Alternatively, the present application can also be implemented as a computer-readable storage medium (or non-transitory machine-readable storage medium or machine-readable storage medium) on which executable code (or computer program or computer instruction code) is stored. When the executable code (or computer program or computer instruction code) is executed by a processor of an electronic device (or server, etc.), the processor executes part or all of the steps of the above-mentioned method according to the present application.
[0190] The embodiments of the present application have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and changes will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or improvements to the technology in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.
Claims
1. A business processing method, characterized in that: The method comprises: When a business request is received, a target change identifier corresponding to the business request is obtained; wherein the target change identifier is an identifier configured for a database change situation; According to the target change identifier, it is determined to obtain the business result corresponding to the business request from the cache or from the database, wherein obtaining the business result from the database also includes updating the cache with the business result.
2. The method according to claim 1, characterized in that The determining, according to the change identifier, to obtain the service result corresponding to the service request from a cache or from a database includes: According to the target change identifier, determining whether a business result corresponding to the business request in the database has changed; If the business result corresponding to the business request in the database has not changed, determining to obtain the business result corresponding to the business request from the cache; If the business result corresponding to the business request in the database changes, it is determined to obtain the business result corresponding to the business request from the database.
3. The method according to claim 2, characterized in that The method further comprises: When a change event occurs in the database, a change identifier is configured for the change event; The change identification is stored in a configuration center.
4. The method according to claim 2, characterized in that: The configuration center includes a plurality of change identifiers, and the method further includes: When a change event occurs in the database, determining a change identifier corresponding to the change event from the configuration center; The value of the change identifier corresponding to the change event is updated to a first value.
5. The method according to claim 2, characterized in that: The business request includes a business field; and judging whether a business result corresponding to the business request in the database has changed according to the target change identifier includes: Concatenate the business field and the target change identifier to obtain a key field; Check whether there is a business result matching the key field in the cache; If there is a business result matching the key field in the cache, it is determined that the business result corresponding to the business request in the database has not changed; If there is no business result matching the key field in the cache, it is determined that the business result corresponding to the business request in the database has changed.
6. The method according to claim 4, characterized in that The determining, according to the target change identifier, whether a business result corresponding to the business request in the database has changed includes: Determining a value of the target change indicator; If the value of the target change identifier is the second value, it is determined that the business result corresponding to the business request in the database has not changed; If the value of the target change identifier is the first value, it is determined that the business result corresponding to the business request in the database has changed.
7. The method according to claim 6, characterized in that After the cache is updated using the business result, the method further includes: The value of the target change identifier is updated to a second value.
8. The method according to claim 3 or 4, characterized in that: When receiving a service request, obtaining a target change identifier corresponding to the service request includes: When receiving a service request, extracting a service field from the service request; According to the business field, obtaining a storage path of the target change identifier in the configuration center; The target change identifier is obtained from the configuration center using the storage path.
9. A service processing device, characterized in that: The device comprises: A target change identifier acquisition module, used for acquiring a target change identifier corresponding to a business request when receiving the business request; wherein the target change identifier is an identifier configured for a database change situation; The business processing module is used to determine whether to obtain the business result corresponding to the business request from the cache or from the database according to the target change identifier, wherein obtaining the business result from the database also includes updating the cache with the business result.
10. An electronic device, characterized in that: include: processor; as well as A memory having executable codes stored thereon, which, when executed by the processor, causes the processor to execute the method according to any one of claims 1 to 8.
11. A computer-readable storage medium having executable codes stored thereon, and when the executable codes are executed by a processor of an electronic device, the processor is caused to execute the method according to any one of claims 1 to 8.