Data updating method, device, equipment and storage medium
By using the database and cache version identifiers in the cache in the Internet system with high concurrency and large traffic, the problem of inconsistency between cache and database data in the existing technology is solved, and the strong consistency update of data is achieved to ensure the latest and correctness of data.
Patent Information
- Application Number
- CN202011245395.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-10
- Publication Date
- 2025-05-16
- Estimated Expiration
- 2040-11-10
AI Technical Summary
The prior art cannot ensure strong consistency in cache and database data in high concurrency and high traffic Internet systems, resulting in the data read from cache being not the latest correct data.
By storing the target data and the first version identifier in the database, storing the first data and the second data in the cache, and using the second version identifier in the first data and the third version identifier in the second data, strong consistency updates between the cache and the database data are achieved. Specific steps include receiving update requests, querying version identification, updating version identification, updating database and cache data.
It realizes that the data in the cache and database are logically consistent when data is updated, so that the latest and correct data can be read when accessing data.
Smart Images

Figure CN113779052B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the field of Internet technology, and in particular to a data updating method, device, equipment and storage medium. Background Art
[0002] In high-concurrency, high-traffic Internet systems, if the system relies on a database for storage, a large database cluster is usually required to support high-traffic access. This requires a large amount of database resources and may not be able to withstand peak traffic. Therefore, a solution to use a cache with a faster reading speed than the database to cope with high-traffic access has emerged, that is, storing the same data as in the database in the cache for user access.
[0003] In order to keep the data in the cache and the database consistent in the prior art, the following three data update strategies are usually adopted: method one, updating the database first and then updating the cache; method two, updating the cache first and then updating the database; method three, using a passive update method, updating the database first, not updating the cache, but passively filling the data in the database back into the cache when reading data.
[0004] In Internet application scenarios where both the reading and writing processes are highly concurrent, the data update strategies of the prior art are usually unable to ensure strong consistency between the data in the cache and the database, which easily leads to the data read from the cache not being the latest and correct data. Summary of the invention
[0005] The embodiments of the present invention provide a data updating method, device, equipment and storage medium, which are used to ensure strong consistency of data in a cache and a database when data is updated.
[0006] In a first aspect, an embodiment of the present invention provides a data updating method, wherein target data and a corresponding first version identifier are stored in a database, first data and second data are stored in a cache, the first data includes the second version identifier of the target data in the cache, and the second data includes the target data and a third version identifier of the target data in the cache; the method includes:
[0007] receiving an update request for target data, and querying a first version identifier of the target data in the database and a second version identifier in the first data according to the update request;
[0008] According to the first version identifier and the second version identifier, updating the second version identifier to a target version identifier;
[0009] According to the target version identifier, updating the target data and the first version identifier in the database;
[0010] After the database is updated, the target data and the third version identifier in the second data are updated according to the updated target data and the updated first version identifier in the database.
[0011] In a second aspect, an embodiment of the present invention provides a data updating device, wherein target data and a corresponding first version identifier are stored in a database, first data and second data are stored in a cache, the first data includes the second version identifier of the target data in the cache, and the second data includes the target data and a third version identifier of the target data in the cache; the device includes:
[0012] A receiving module, used for receiving an update request for target data;
[0013] A query module, configured to query a first version identifier of the target data in the database and a second version identifier of the first data according to the update request;
[0014] a cache update module, configured to update the second version identifier to a target version identifier according to the first version identifier and the second version identifier;
[0015] A database updating module, used for updating the target data and the first version identifier in the database according to the target version identifier;
[0016] The cache update module is further used to update the target data and the third version identifier in the second data according to the updated target data and the updated first version identifier in the database after the database is updated.
[0017] In a third aspect, an embodiment of the present invention provides an electronic device, comprising: a database; a cache; at least one processor; and a memory for storing instructions;
[0018] The database is used to store the target data and the corresponding first version identifier;
[0019] The cache is used to store first data and second data, the first data includes a second version identifier of the target data in the cache, and the second data includes the target data and a third version identifier of the target data in the cache;
[0020] The memory is used to store computer-executable instructions;
[0021] The at least one processor is configured to execute the computer-executable instructions stored in the memory, so that the at least one processor performs the method according to the first aspect.
[0022] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions. When a processor executes the computer-executable instructions, the method described in the first aspect is implemented.
[0023] The data updating method, device, equipment and storage medium provided by the embodiment of the present invention query the first version identifier of the target data in the database and the second version identifier in the cached first data according to the update request after receiving the update request for the target data; update the second version identifier to the target version identifier according to the first version identifier and the second version identifier; update the target data and the first version identifier in the database according to the target version identifier; after the database is updated, update the target data and the third version identifier in the cached second data according to the updated target data in the database and the updated first version identifier. In the embodiment of the present invention, the updated version identifier in the cached first data is used to update the target data and the first version identifier in the database. After the target data and the first version identifier in the database are updated, the target data and the third version identifier in the cached second data are updated, so that the target data in the cache and the database are logically guaranteed to be strongly consistent after the update, so that the latest and correct data can be read when accessing the data. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0025] Figure 1 A schematic diagram of an application scenario of the data updating method provided by an embodiment of the present invention;
[0026] Figure 2 A flow chart of a data updating method provided by an embodiment of the present invention;
[0027] Figure 3 A flow chart of a data updating method provided by an embodiment of the present invention;
[0028] Figure 4 A flow chart of a data updating method provided by an embodiment of the present invention;
[0029] Figure 5 A flow chart of a data updating method provided by an embodiment of the present invention;
[0030] Figure 6 A block diagram of a data updating device provided by an embodiment of the present invention;
[0031] Figure 7 A block diagram of an electronic device for executing a data updating method provided by an embodiment of the present invention.
[0032] The above drawings show clear embodiments of the present disclosure, which will be described in more detail below. These drawings and text descriptions are not intended to limit the scope of the present disclosure in any way, but to illustrate the concepts of the present disclosure to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0033] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0034] In high-concurrency, high-traffic Internet systems, in order to support high-traffic access, a cache with a faster reading speed than the database is usually deployed on the basis of the database, and the same data as in the database is stored in the cache for user access. In order to keep the data in the cache consistent with the data in the database, the following three data update strategies are usually adopted in the prior art:
[0035] Method 1: first update the target data in the database, and then update the target data in the cache; Method 2: first update the target data in the cache, and then update the target data in the database; Method 3: adopt a passive update method, first update the target data in the database, do not update the target data in the cache, and passively fill the target data in the database back to the cache when reading data.
[0036] Since the reading and writing processes in Internet application scenarios are highly concurrent and the returned data is required to be the latest and correct data, it is usually necessary to ensure strong consistency between the data in the cache and the database. However, the above existing data update strategies have some shortcomings:
[0037] Method 1: If the database is updated successfully first and then the cache is updated failed, the cache data will be older than the database data. At this time, when an access request is received, dirty data (older than the database data) will be returned from the cache.
[0038] Method 2: If the cache is updated successfully first and the database is updated again, the cache data is newer than the database data, and the cache deletion also fails. At this time, when an access request is received, dirty data (newer than the database data) will be returned from the cache.
[0039] Method 3: The database is updated. If the cache has not expired, a request to read the cache will also return dirty data (older than the database).
[0040] In summary, in the case of Internet application scenarios, the data update strategy of the prior art usually cannot ensure strong consistency between the data in the cache and the database, which easily leads to the data read from the cache not being the latest and correct data.
[0041] In order to solve the above technical problems, in an embodiment of the present invention, target data and a corresponding first version identifier are stored in a database, and first data and second data are stored in a cache, wherein the first data includes a second version identifier of the target data in the cache, and the second data includes the target data and a third version identifier of the target data in the cache; under normal circumstances, if the target data in the cache and the database are consistent, the first version identifier, the second version identifier, and the third version identifier should remain the same. Of course, optionally, the version identifier can also be a part of the target data, for example, the version identifier can be a field in the target data.
[0042] When data update is required, that is, when an update request for target data is received, the first version identifier of the target data in the database and the second version identifier in the first data can be queried first, and then whether the update condition is met based on the first version identifier and the second version identifier. If the update condition is met, the second version identifier is updated to the target version identifier; and then the target data and the first version identifier in the database are updated according to the target version identifier, that is, the target data in the database is updated to the data corresponding to the target version identifier, and the first version identifier is updated to the target version identifier; after the database is updated, the target data and the third version identifier in the second data are updated according to the updated target data in the database and the updated first version identifier.
[0043] In the embodiment of the present invention, since the second version identifier in the first data in the cache is first updated to the target version identifier before the target data in the database is updated, the target data and the first version identifier in the database are updated after the second version identifier is successfully updated. After the database is updated, the target data and the third version identifier in the second data in the cache are updated according to the updated target data in the database and the updated first version identifier. The updated version identifier in the first data in the cache is used to update the target data and the first version identifier in the database. After the target data and the first version identifier in the database are updated, the update of the target data and the third version identifier in the second data in the cache is promoted. Therefore, the present invention makes the update of the target data in the database and the cache an interlocking process. Through this process, the target data in the cache and the database after the update can be logically guaranteed to have strong consistency. In particular, the second version identifier in the first data and the third version identifier in the second data can be used to verify whether the target data in the cache and the database are consistent, so that the latest and correct data can be obtained when the target data is read from the cache.
[0044] The data updating method of the embodiment of the present invention can be applied to Figure 1 The application scenario shown includes a client 101 and a server 102. The client 101 may be any electronic device including but not limited to a mobile phone or a computer, and the server may include a database 1022 and a cache 1023. The target data and the corresponding first version identifier are stored in the database 1022, and the first data and the second data are stored in the cache 1023, wherein the first data includes the second version identifier of the target data in the cache 1023, and the second data includes the target data and the third version identifier of the target data in the cache 1023. Under normal circumstances, if the target data in the cache 1023 is consistent with the target data in the database 1022, the first version identifier, the second version identifier and the third version identifier should remain the same.
[0045] The client 101 may send an update request for the target data to the server 102, or the server 102 may trigger an update request for the target data when detecting that the target data needs to be updated, and then the server 102 may query the first version identifier of the target data in the database 1022 and the second version identifier in the first data according to the update request; update the second version identifier to the target version identifier according to the first version identifier and the second version identifier; update the target data and the first version identifier in the database 1022 according to the target version identifier; after the database 1022 is updated, update the target data and the third version identifier in the second data according to the updated target data and the updated first version identifier in the database 1022.
[0046] It should be noted that, in the embodiment of the present invention, the server 102 can be a server, that is, the database and the cache are configured on the same server, and the execution subject of the data update method in the embodiment of the present invention is the server 102; of course, in the embodiment of the present invention, the server 102 can also be a cluster composed of multiple servers, for example, the database 1022 is configured on some servers, and the cache 1023 is configured on other servers, then the execution subject of the data update method in the embodiment of the present invention can be the central server of the server cluster, and the central server controls the update of the target data. Of course, the execution subject can also be any server.
[0047] The following specific embodiments are used to describe in detail the technical solutions of the present invention and how the technical solutions of the present application solve the above-mentioned technical problems. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present invention will be described below in conjunction with the accompanying drawings.
[0048] Figure 2A flow chart of a data updating method provided by an embodiment of the present invention. This embodiment provides a data updating method, in which target data and a corresponding first version identifier are stored in a database, and first data and second data are stored in a cache, wherein the first data includes a second version identifier of the target data in the cache, and the second data includes the target data and a third version identifier of the target data in the cache.
[0049] As an example, each piece of data in the database contains a version identification field, which can be auto-incremented starting from 0. The specific format of the data can be shown in the following table:
[0050] id (primary key) Other fields version 12345 …… 0
[0051] In the cache, the data in the database is represented by two pieces of data, each of which includes a version identifier. The specific format is as follows:
[0052] First data: key: 12345_v value: version: 0
[0053] Second data: key: 12345_i value: {"id": 12345,"other fields": "...","version": 0}
[0054] The cache stores the first data and the second data based on key-value, wherein the keyword key: 12345 in the first data and the second data corresponds to the id (primary key) in the database, which can identify the corresponding data in the cache and the database, and the corresponding values of the first data and the second data can be queried through value = get (key), the value in the first data is the version of the data corresponding to the id in the cache, and the value of the second data is the full data of the data corresponding to the id in the cache, including the id, other fields, version identification fields, etc. Optionally, the value of the second data is expressed in json format, and the json content is the full content of the data in the database. Of course, in this embodiment, the data in the database, the first data and the second data in the cache are not limited to the above format. Under normal circumstances, for a certain target data, if the target data in the cache and the database are consistent, the version identifier of the target data in the database, the version identifier in the first data corresponding to the target data in the cache, and the version identifier of the second data should remain the same.
[0055] Based on the above database and cache storage methods, such as Figure 2 As shown, the specific steps of the data updating method in this embodiment are as follows:
[0056] S201. Receive an update request for target data, and query a first version identifier of the target data in the database and a second version identifier in the first data according to the update request.
[0057] In this embodiment, when the target data needs to be updated, an update request for the target data can be sent, or when it is detected that the target data needs to be updated, an update request for the target data will be triggered, and then the first version identifier of the target data in the database and the second version identifier of the target data in the cached first data can be queried based on the update request. Optionally, the update request for the target data may include the id of the target data, and then the first version identifier of the target data is queried from the database based on the id of the target data, and the second version identifier of the first data corresponding to the id is queried from the cache.
[0058] It should be noted that, with respect to the first version identifier of the query target data in the database and the second version identifier of the query target data in the first data, the order of the two query processes is not limited in this embodiment, and of course the two query processes can also be executed simultaneously.
[0059] S202: Update the second version identifier to a target version identifier according to the first version identifier and the second version identifier.
[0060] In this embodiment, after querying the first version identifier of the target data in the database and the second version identifier of the target data in the cached first data, it can be determined whether the second version identifier can be updated based on the first version identifier and the second version identifier. If the second version identifier can be updated, the second version identifier is updated to the target version identifier.
[0061] Optionally, the target version identifier of the target data may be included in the update request; or optionally, if the version identifier is incremented sequentially when the target data is updated, the target version identifier of the target data may not be included in the update request, and the target version identifier may be the current version identifier + 1; if the version identifier is not incremented sequentially when the target data is updated, the target version identifier needs to be specified.
[0062] Further, when updating the second version identifier to the target version identifier according to the first version identifier and the second version identifier, it may specifically include:
[0063] If the second version identifier is equal to the first version identifier, then the second version identifier is updated to the target version identifier; or,
[0064] If the second version identifier is not equal to the first version identifier, and the second version identifier is equal to the target version identifier, it is directly determined that the second version identifier is updated successfully; or,
[0065] If the second version identifier is not equal to the first version identifier, and the second version identifier is not equal to the target version identifier, it is determined that the update of the second version identifier has failed, and the subsequent data update process ends.
[0066] In this embodiment, updating the second version identifier in the first data to the target version identifier serves as the starting point of the data update behavior. If the second version identifier is updated to the target version identifier, it means that the update of the target data has been initiated. If the second version identifier is not updated to the target version identifier, it means that the update of the target data has not been initiated. At this time, the second version identifier remains equal to the first version identifier. Therefore, in this embodiment, if the second version identifier is equal to the first version identifier and is not equal to the target version identifier, it means that the target data in the cache and the database has not been updated by the current thread and other threads. At this time, the second version identifier is updated to the target version identifier, that is, the data update behavior is started; if the second version identifier is not equal to the first version identifier, and the second version identifier is equal to the target version identifier, it means that other threads may have started the data update behavior at this time. Since the update of the data content of the target data in the cache and the database is not involved at this time, it is directly determined that the second version identifier is updated successfully, and the subsequent update of the data content of the target data in the cache and the database can continue; if the second version identifier is not equal to the first version identifier, and the second version identifier is not equal to the target version identifier, it is determined that the second version identifier update failed, the subsequent data update process is terminated, and an update failure message can also be returned.
[0067] S203: Update the target data and the first version identifier in the database according to the target version identifier.
[0068] In this embodiment, after the second version identifier is updated to the target version identifier, the target data in the database and the first version identifier of the target data in the database are updated, that is, the target data in the database is updated to the data corresponding to the target version identifier, and the first version identifier is updated to the target version identifier.
[0069] Optionally, when updating the target data in the database and the first version identifier, an optimistic locking mechanism can be set for the database, wherein the optimistic locking mechanism is in the data update scenario, assuming that the data will not cause conflicts under normal circumstances, so when the data is submitted for update, the conflict of the data will be formally detected. If a conflict is found, the information of the user's error detection will be returned. CAS (Compare and Swap) is an optimistic lock implementation method, and the version comparison in the where condition in SQL update is such an implementation. Specifically, in the process of updating the target data in the database, when submitting the new version of the target data, the current version identifier is compared with the first version identifier of the target data in the database initially found. If the current version identifier is equal to the first version identifier of the target data in the database initially found, the target data in the database is agreed to be updated, otherwise it is considered to be expired data, and it is determined that the update of the target data in the database fails, then the subsequent data update process ends.
[0070] S204: After the database is updated, the target data and the third version identifier in the second data are updated according to the updated target data and the updated first version identifier in the database.
[0071] In this embodiment, after the database completes the update of the target data and the first version identifier, the target data and the third version identifier in the second data are updated according to the updated target data and the updated first version identifier in the database, so that the target data and the third version identifier in the second data are guaranteed to be consistent with the updated target data and the updated first version identifier in the database. The above process can ensure strong logical consistency of the target data in the cache and the database after the update, that is, if the above process can be successfully executed, the target data in the cache and the database can ensure strong consistency; of course, there may be a situation where the update of the second data fails, and the target data and the third version identifier in the second data can be re-updated through the following embodiment, so that the target data in the cache and the database can be guaranteed to be strongly consistent after the re-update.
[0072] The data update method provided in this embodiment is as follows: after receiving an update request for target data, query the first version identifier of the target data in the database and the second version identifier in the cached first data according to the update request; update the second version identifier to the target version identifier according to the first version identifier and the second version identifier; update the target data and the first version identifier in the database according to the target version identifier; after the database is updated, update the target data and the third version identifier in the cached second data according to the updated target data in the database and the updated first version identifier. In this embodiment, the updated version identifier in the cached first data is used to update the target data and the first version identifier in the database. After the target data and the first version identifier in the database are updated, the target data and the third version identifier in the cached second data are updated, so that the updated target data in the cache and the database are logically guaranteed to be strongly consistent, so that the latest and correct data can be read when accessing the data.
[0073] On the basis of any of the above embodiments, when the target data in the database and the first version identifier are updated in S203, an update log file of the database may be generated, wherein the update log file may record the specific content updated by the database, and may optionally include the target data updated by the database and the first version identifier. As an example, in this embodiment, the database may adopt a MySQL database, and the update log file may be a MySQL binlog file, which is a log file that records modification records of the MySQL database in binary format.
[0074] Further, in this embodiment, the update log file can be subscribed in a publish / subscribe mode, such as Figure 3 As shown, the method also includes:
[0075] S301, when the update log file publishes a message, comparing the current third version identifier with the second version identifier according to the update log file;
[0076] S302: If the current third version identifier is older than the second version identifier, update the target data in the second data and the third version identifier according to the update log file.
[0077] In this embodiment, when updating the target data and the third version identifier in the second data, there may be some update failures, such as equipment failure, network failure, etc., which may cause the target data and the third version identifier in the second data to not be updated successfully. The second version identifier in the cached first data and the first version identifier of the target data in the database may have been updated to the target version identifier, while the third version identifier in the second data may not have been updated to the target version identifier. Therefore, in order to avoid the inconsistency of the target data in the cache and the database caused by the above situation, this embodiment adds a compensation mechanism. When the update log file publishes a message, the current third version identifier and the current second version identifier can be compared when the message is consumed. If the current third version identifier is older than the current second version identifier, the target data and the third version identifier in the second data are updated based on the update log file.
[0078] It should be noted that the update log file release message is parallel to S204. In most cases, S204 is earlier than the update log file release message (that is, in most cases, S204 is earlier than the database updating the update log file). After the update log file release message, the message is consumed. If the current third version identifier is older than the second version identifier, it proves that S204 has failed to update the second data. At this time, according to the update log file, the target data and the third version identifier in the second data are re-updated to ensure strong logical consistency of the target data. In addition, it should be noted that if S204 fails to update the second data, the target data and the third version identifier in the second data are not updated based on the target data and the first version identifier in the database. This is because the target data in the database may be in the next update cycle during the update process, and there may be a situation where the second data update fails again.
[0079] However, in extreme cases, the update log file may publish a message earlier than S204. In this case, the target data in the second data and the third version identifier may be updated first according to the update log file, and S204 is executed later. During execution, it is determined that the update has failed, but the logical consistency of the target data can still be guaranteed.
[0080] In another optional embodiment, if the target data in the second data and the third version identifier fail to be updated, or if it is determined that the third version identifier is older than the second version identifier when querying the target data from the cache, the target data in the second data and the third version identifier are re-updated according to the update log file of the database.
[0081] In this embodiment, the publish / subscribe mode may not be used to subscribe to the update log file, that is, it is not necessary to determine whether the update of the second data fails after the update log file publishes a message. Instead, if it is detected that S204 fails to update the second data under any circumstances, or if it is determined that the third version identifier is older than the second version identifier when querying the target data from the cache, the target data in the second data and the third version identifier may be re-updated according to the update log file of the database.
[0082] Based on any of the above embodiments, Figure 4 As shown, the method also includes:
[0083] S401, receiving a query request for the target data, and querying in the cache according to the query request whether a second version identifier of the target data in the first data is equal to a third version identifier in the second data;
[0084] If the second version identifier is equal to the third version identifier, execute S402; otherwise, execute S403.
[0085] S402: If the second version identifier is equal to the third version identifier, obtaining the target data from the second data; or
[0086] S403: If the second version identifier is not equal to the third version identifier, obtaining the target data from the database.
[0087] In this embodiment, when a query request for target data is received, it indicates that the target data needs to be accessed. At this time, the cache may be queried to see whether the second version identifier of the target data in the first data is equal to the third version identifier in the second data. If they are equal, it indicates that the cache is strongly consistent with the target data in the database, and the target data is directly obtained from the cached second data; if they are not equal, it indicates that the cache is inconsistent with the target data in the database, and the target data is directly obtained from the database. It should be noted that in this embodiment, the cached second data may not be backfilled based on the database, but the target data and the third version identifier in the second data may be re-updated through the update log file based on the database in the above embodiment. Alternatively, the target data and the third version identifier in the second data may be updated through S204 in the above embodiment, so that the cache and the database ensure strong consistency.
[0088] Based on any of the above embodiments, the cache in this embodiment can use distributed cache to solve the database performance bottleneck problem in a large number of access scenarios, and introduce memory-based application services, such as Redis and Memcached, which are widely used in Internet companies, to ensure high throughput of the cache. In addition, the above method flow can be implemented based on the Lua scripting language to ensure the integrity of the operation transaction.
[0089] Figure 5 A flow chart of a data updating method provided by an embodiment of the present invention. Based on the above embodiment, this embodiment provides a data updating method. The specific steps of the data updating method in this embodiment are as follows:
[0090] S501, receiving a request to update target data;
[0091] S502, querying the first version identifier of the target data in the database and the second version identifier in the first data;
[0092] S503: judging, according to the first version identifier and the second version identifier, whether the second version identifier is equal to the first version identifier, and whether the second version identifier is equal to the target version identifier;
[0093] S504: if the second version identifier is equal to the first version identifier, update the second version identifier to the target version identifier; or if the second version identifier is not equal to the first version identifier and the second version identifier is equal to the target version identifier, determine that the second version identifier is updated successfully;
[0094] S505: If the second version identifier is not equal to the first version identifier, and the second version identifier is not equal to the target version identifier, it is determined that the update fails, and an update failure message is returned;
[0095] After the second version identifier is successfully updated in S504, the following steps are also included:
[0096] S506: Update the target data and the first version identifier in the database according to the target version identifier;
[0097] S507: The database is updated successfully. According to the updated target data and the updated first version identifier in the database, the target data and the third version identifier in the second data are updated;
[0098] S508: Database update fails, and an update failure message is returned.
[0099] The specific implementation method and technical effect of the data updating method provided by the embodiment of the present invention can be found in the above embodiment and will not be described in detail here.
[0100] Figure 6 The data updating device provided in this embodiment can execute the processing flow provided in the method embodiment, wherein the target data and the corresponding first version identifier are stored in the database, and the first data and the second data are stored in the cache, wherein the first data includes the second version identifier of the target data in the cache, and the second data includes the target data and the third version identifier of the target data in the cache; Figure 6 As shown, the data updating device 600 includes a receiving module 601 , a query module 602 , a cache updating module 603 , and a database updating module 604 .
[0101] The receiving module 601 is used to receive a request for updating target data;
[0102] A query module 602, configured to query the first version identifier of the target data in the database and the second version identifier of the first data according to the update request;
[0103] A cache update module 603, configured to update the second version identifier to a target version identifier according to the first version identifier and the second version identifier;
[0104] A database updating module 604, configured to update the target data and the first version identifier in the database according to the target version identifier;
[0105] The cache update module 603 is further configured to update the target data and the third version identifier in the second data according to the updated target data and the updated first version identifier in the database after the database is updated.
[0106] Based on the above embodiments,
[0107] When the cache update module 603 updates the second version identifier to a target version identifier according to the first version identifier and the second version identifier, it is configured to:
[0108] If the second version identifier is equal to the first version identifier, updating the second version identifier to the target version identifier; or
[0109] If the second version identifier is not equal to the first version identifier, and the second version identifier is equal to the target version identifier, directly determining that the second version identifier is updated successfully; or
[0110] If the second version identifier is not equal to the first version identifier, and the second version identifier is not equal to the target version identifier, it is determined that the update of the second version identifier has failed, and the subsequent data update process ends.
[0111] Based on any of the above embodiments, when updating the target data in the database and the first version identifier according to the target version identifier, the database updating module 604 is used to:
[0112] The database optimistic lock mechanism is adopted to update the target data in the database to the data corresponding to the target version identifier, and the first version identifier is updated to the target version identifier.
[0113] Based on any of the above embodiments, the database updating module 604 is further used for:
[0114] If the update of the target data in the database using the database optimistic lock mechanism fails, the subsequent data update process is terminated.
[0115] Based on any of the above embodiments, the database update module 604 is further used to generate an update log file of the database when updating the target data and the first version identifier in the database;
[0116] The cache update module 603 is further used to subscribe to the update log file in a publish / subscribe mode, and compare the current third version identifier with the second version identifier according to the update log file when the update log file publishes a message;
[0117] If the current third version identifier is older than the second version identifier, the target data in the second data and the third version identifier are updated according to the update log file.
[0118] Based on any of the above embodiments, the database update module 604 is further used to generate an update log file of the database when updating the target data and the first version identifier in the database;
[0119] The cache update module 603 is also used to re-update the target data and the third version identifier in the second data according to the update log file of the database if the update of the target data and the third version identifier in the second data fails, or if it is determined that the third version identifier is older than the second version identifier when querying the target data from the cache.
[0120] Based on any of the above embodiments, the device further includes a data access module, which is used to:
[0121] receiving a query request for the target data, and querying in the cache according to the query request whether a second version identifier of the target data in the first data is equal to a third version identifier in the second data;
[0122] If the second version identifier is equal to the third version identifier, obtaining the target data from the second data; or
[0123] If the second version identifier is not equal to the third version identifier, the target data is obtained from the database.
[0124] The data updating device provided in the embodiment of the present invention can be specifically used to execute the above Figure 2-5 The specific functions of the provided method embodiment will not be repeated here.
[0125] The data updating device provided by the embodiment of the present invention queries the first version identifier of the target data in the database and the second version identifier in the cached first data according to the update request after receiving the update request for the target data; updates the second version identifier to the target version identifier according to the first version identifier and the second version identifier; updates the target data and the first version identifier in the database according to the target version identifier; and updates the target data and the third version identifier in the cached second data according to the updated target data in the database and the updated first version identifier after the database is updated. In this embodiment, the updated version identifier in the cached first data is used to update the target data and the first version identifier in the database. After the target data and the first version identifier in the database are updated, the target data and the third version identifier in the cached second data are updated, so that the target data in the cache and the database are logically guaranteed to be strongly consistent after the update, so that the latest and correct data can be read when accessing the data.
[0126] Figure 7 The electronic device provided by the embodiment of the present invention can execute the processing flow provided by the data updating method embodiment, such as Figure 7As shown, the electronic device 70 includes a database 71, a cache 72, a memory 73 for storing instructions, and at least one processor 74; wherein the database 71 is used to store target data and a corresponding first version identifier; the cache 72 is used to store first data and second data, the first data including the second version identifier of the target data in the cache 72, and the second data including the target data and the third version identifier of the target data in the cache 72; the computer program (instructions) is stored in the memory 73, and is configured to be executed by the processor 74 for executing the data update method described in the above embodiment. In addition, the electronic device 70 may also have a communication interface 75 for receiving control instructions. It should be noted that the memory 73 may also be integrated with the database 71 or the cache 72.
[0127] Figure 7 The electronic device of the illustrated embodiment can be used to execute the technical solution of the above-mentioned data updating method embodiment, and its implementation principle and technical effect are similar, which will not be repeated here.
[0128] In addition, this embodiment also provides a computer-readable storage medium on which a computer program is stored, and the computer program is executed by a processor to implement the block processing method of the blockchain described in the above embodiment.
[0129] In the several embodiments provided in the embodiments of the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0130] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0131] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0132] The above-mentioned integrated unit implemented in the form of a software functional unit can be stored in a computer-readable storage medium. The above-mentioned software functional unit is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform some steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program codes.
[0133] Those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example for illustration. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. The specific working process of the device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0134] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the embodiments of the present invention, rather than to limit them. Although the embodiments of the present invention have been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A data updating method, characterized in that: The database stores target data and a corresponding first version identifier, the cache stores first data and second data, the first data includes the second version identifier of the target data in the cache, and the second data includes the target data and a third version identifier of the target data in the cache; the method includes: receiving an update request for target data, and querying a first version identifier of the target data in the database and a second version identifier in the first data according to the update request; According to the first version identifier and the second version identifier, updating the second version identifier to a target version identifier; According to the target version identifier, updating the target data and the first version identifier in the database; After the database is updated, the target data and the third version identifier in the second data are updated according to the updated target data and the updated first version identifier in the database; The updating the second version identifier to a target version identifier according to the first version identifier and the second version identifier includes: If the second version identifier is equal to the first version identifier, updating the second version identifier to the target version identifier; or If the second version identifier is not equal to the first version identifier, and the second version identifier is equal to the target version identifier, directly determining that the second version identifier is updated successfully; or If the second version identifier is not equal to the first version identifier, and the second version identifier is not equal to the target version identifier, it is determined that the update of the second version identifier has failed, and the subsequent data update process ends.
2. The method according to claim 1, characterized in that The updating of the target data and the first version identifier in the database according to the target version identifier includes: The database optimistic lock mechanism is adopted to update the target data in the database to the data corresponding to the target version identifier, and the first version identifier is updated to the target version identifier.
3. The method according to claim 2, characterized in that The method further comprises: If the update of the target data in the database using the database optimistic lock mechanism fails, the subsequent data update process is terminated.
4. The method according to any one of claims 1 to 3, characterized in that: Also includes: When updating the target data in the database and the first version identifier, generating an update log file of the database; Adopting a publish / subscribe mode, subscribing to the update log file, and when the update log file publishes a message, comparing the current third version identifier with the second version identifier according to the update log file; If the current third version identifier is older than the second version identifier, the target data in the second data and the third version identifier are updated according to the update log file.
5. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: When updating the target data in the database and the first version identifier, generating an update log file of the database; If the target data in the second data and the third version identifier fail to be updated, or if it is determined that the third version identifier is older than the second version identifier when querying the target data from the cache, the target data in the second data and the third version identifier are re-updated according to the update log file of the database.
6. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: receiving a query request for the target data, and querying in the cache according to the query request whether a second version identifier of the target data in the first data is equal to a third version identifier in the second data; If the second version identifier is equal to the third version identifier, obtaining the target data from the second data; or If the second version identifier is not equal to the third version identifier, the target data is obtained from the database.
7. A data updating device, characterized in that: The database stores target data and a corresponding first version identifier, the cache stores first data and second data, the first data includes the second version identifier of the target data in the cache, and the second data includes the target data and a third version identifier of the target data in the cache; the device includes: A receiving module, used for receiving an update request for target data; A query module, configured to query a first version identifier of the target data in the database and a second version identifier of the first data according to the update request; a cache update module, configured to update the second version identifier to a target version identifier according to the first version identifier and the second version identifier; A database updating module, used for updating the target data and the first version identifier in the database according to the target version identifier; The cache update module is also used to update the target data and the third version identifier in the second data according to the updated target data and the updated first version identifier in the database after the database is updated; When the cache update module updates the second version identifier to a target version identifier according to the first version identifier and the second version identifier, it is configured to: If the second version identifier is equal to the first version identifier, updating the second version identifier to the target version identifier; or If the second version identifier is not equal to the first version identifier, and the second version identifier is equal to the target version identifier, directly determining that the second version identifier is updated successfully; or If the second version identifier is not equal to the first version identifier, and the second version identifier is not equal to the target version identifier, it is determined that the update of the second version identifier has failed, and the subsequent data update process ends.
8. The device according to claim 7, characterized in that When the database updating module updates the target data in the database and the first version identifier according to the target version identifier, it is used to: The database optimistic lock mechanism is adopted to update the target data in the database to the data corresponding to the target version identifier, and the first version identifier is updated to the target version identifier.
9. The device according to claim 8, characterized in that The database update module is also used for: If the update of the target data in the database using the database optimistic lock mechanism fails, the subsequent data update process is terminated.
10. The device according to any one of claims 7 to 9, characterized in that: The database update module is further used to generate an update log file of the database when updating the target data and the first version identifier in the database; The cache update module is also used to subscribe to the update log file in a publish / subscribe mode, and compare the current third version identifier with the second version identifier according to the update log file when the update log file publishes a message; If the current third version identifier is older than the second version identifier, the target data in the second data and the third version identifier are updated according to the update log file.
11. The device according to any one of claims 7 to 9, characterized in that: The database update module is further used to generate an update log file of the database when updating the target data and the first version identifier in the database; The cache update module is also used to re-update the target data and the third version identifier in the second data according to the update log file of the database if the update of the target data and the third version identifier in the second data fails, or if it is determined that the third version identifier is older than the second version identifier when querying the target data from the cache.
12. The device according to any one of claims 7 to 9, characterized in that: The device also includes a data access module, which is used to: receiving a query request for the target data, and querying in the cache according to the query request whether a second version identifier of the target data in the first data is equal to a third version identifier in the second data; If the second version identifier is equal to the third version identifier, acquiring the target data from the second data; or If the second version identifier is not equal to the third version identifier, the target data is obtained from the database.
13. An electronic device, characterized in that: include: database; cache; at least one processor; and a memory for storing instructions; The database is used to store target data and the corresponding first version identifier; The cache is used to store first data and second data, the first data includes a second version identifier of the target data in the cache, and the second data includes the target data and a third version identifier of the target data in the cache; The memory is used to store computer-executable instructions; The at least one processor is configured to execute the computer-executable instructions stored in the memory, so that the at least one processor executes the method according to any one of claims 1 to 6.
14. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the processor executes the computer-executable instructions, the method according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Cache updating method and device
CN107122410A
Data synchronization method, device and equipment
CN108959341A