Data updating method and device, storage medium and electronic device
Patent Information
- Application Number
- CN202211203452.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-29
- Publication Date
- 2026-09-18
- Estimated Expiration
- 2042-09-29
AI Technical Summary
[0005]针对相关技术中,如何对缓存数据进行更新,以保证数据库中数据和缓存数据一致的技术问题,尚未提出有效的解决方案
[0017]In this embodiment, an update identifier corresponding to the log content of a first database is obtained, the update identifier indicating whether the log content of the first database has been updated; if it is determined that the update identifier indicates that the first data in the first database has changed to a second data with the same data identifier as the first data, the second data is obtained from the first database; the first data stored in the cache is determined, and the first data stored in the cache is updated to the second data, wherein the cache and the first database have been pre-synchronized with the first data; by adopting the above technical solution, the technical problem of how to update cached data to ensure consistency between data in the database and cached data is solved, thereby ensuring consistency between data in the database and cached data when updating cached data.
Smart Images

Figure CN115576966B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of smart home technology, and more specifically, to a data update method, apparatus, storage medium, and electronic device. Background Technology
[0002] Currently, data access typically involves users concurrently accessing data in a business system, with the system returning response information. However, in modern internet scenarios with high concurrency, the large number of data I / O operations generated by unpredictable high-concurrency access could directly cause database crashes and unavailability if these data access requests were routed directly to the database. This could lead to serious business failures or system anomalies.
[0003] Based on the above issues, we need to introduce a caching layer between the business system and the database system, so that high-concurrency user requests for I / O operations will first query the cache. However, the introduction of caching technology will also lead to data inconsistency issues, namely, the consistency between the updated database and the cached data.
[0004] Therefore, in related technologies, there is a technical problem of how to update cached data to ensure consistency between data in the database and cached data.
[0005] There is currently no effective solution to the technical problem of how to update cached data to ensure consistency between data in the database and cached data. Summary of the Invention
[0006] This application provides a data update method, apparatus, storage medium, and electronic device to at least solve the technical problem in the related art of how to update cached data to ensure consistency between data in the database and cached data.
[0007] According to one embodiment of this application, a data update method is provided, comprising: obtaining an update identifier corresponding to log content of a first database, the update identifier being used to indicate whether the log content of the first database has been updated; if it is determined that the update identifier indicates that first data in the first database has been changed to second data, obtaining the second data from the first database, wherein the first data and the second data correspond to the same data identifier; determining the first data stored in a cache, and updating the first data stored in the cache to the second data, wherein the cache and the first database have been pre-synchronized with the first data.
[0008] In an exemplary embodiment, after updating the first data stored in the cache to the second data, the method further includes: if an update confirmation ACK identifier generated when the cache updates the first data to the second data is obtained, determining a first query instruction based on the ACK identifier; querying third data from a second database based on the first query instruction, wherein the second database is a backup database of the first database, and the third data represents backup data of the second data; and if it is determined that the second data and the third data are consistent, sending a notification message to the terminal device to indicate that the update was successful.
[0009] In an exemplary embodiment, updating the first data stored in the cache to the second data includes: determining a backup time for backing up the second data of the first database to the second database; generating a delay queue for delaying cache deletion operations based on the backup time, wherein the delay queue corresponds to a delay execution time, the start time of the delay execution time is the time when the first data in the first database is changed to the second data, and the end time of the delay execution time is later than the end time of the backup time; and updating the first data stored in the cache to the second data based on the delay execution time corresponding to the delay queue.
[0010] In an exemplary embodiment, updating the first data stored in the cache to the second data according to the delay execution time corresponding to the delay queue includes: after the delay execution time has elapsed, deleting the first data in the cache based on the cache deletion operation; reading the third data from the second database and writing the third data into the cache.
[0011] In an exemplary embodiment, determining the backup time for backing up the second data from the first database to the second database includes: determining the historical backup time when backing up the second data from the first database to the second database, and determining the backup time based on the historical backup time, including one of the following: setting the historical backup time with the longest usage time as the backup time; setting the historical backup time with the highest usage frequency as the backup time; or setting the historical backup time with the most recent usage time as the backup time.
[0012] In one exemplary embodiment, determining the backup time required to back up second data from the first database to the second database includes: determining the backup time to back up the second data from the first database to the second database based on the type of the first database, the type of the second database, and the network status when backing up the second data from the first database to the second database, so as to determine the backup time to back up the second data from the first database to the second database.
[0013] In one exemplary embodiment, the method further includes: sending a notification message indicating successful update to a terminal device when it is determined that the first data cannot be found in the first database, the second database, and the cache; or sending a notification message indicating successful update to a terminal device when it is determined that the second data can be found in both the first database and the cache; or sending a notification message indicating successful update to a terminal device when it is determined that the third data can be found in both the second database and the cache.
[0014] According to another embodiment of the present application, a data update apparatus is also provided, comprising: a first update module, configured to obtain an update identifier corresponding to log content of a first database, the update identifier indicating whether the log content of the first database has been updated; an acquisition module, configured to obtain second data from the first database when it is determined that the update identifier indicates that first data in the first database has been changed to second data, wherein the first data and the second data correspond to the same data identifier; and a second update module, configured to determine the first data stored in a cache and update the first data stored in the cache to the second data, wherein the cache and the first database have been pre-synchronized with the first data.
[0015] According to another aspect of the embodiments of this application, a computer-readable storage medium is also provided, wherein a computer program is stored in the computer program, and the computer program is configured to execute the above-described data update method when running.
[0016] According to another aspect of the embodiments of this application, an electronic device is also provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the above-described data update method through the computer program.
[0017] In this embodiment, an update identifier corresponding to the log content of a first database is obtained, the update identifier indicating whether the log content of the first database has been updated; if it is determined that the update identifier indicates that the first data in the first database has changed to a second data with the same data identifier as the first data, the second data is obtained from the first database; the first data stored in the cache is determined, and the first data stored in the cache is updated to the second data, wherein the cache and the first database have been pre-synchronized with the first data; by adopting the above technical solution, the technical problem of how to update cached data to ensure consistency between data in the database and cached data is solved, thereby ensuring consistency between data in the database and cached data when updating cached data. Attached Figure Description
[0018] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0019] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0020] Figure 1 This is a schematic diagram of the hardware environment for a data update method according to an embodiment of this application;
[0021] Figure 2 This is a flowchart of a data update method according to an embodiment of this application;
[0022] Figure 3 This is a flowchart illustrating a data update method according to an embodiment of this application;
[0023] Figure 4 This is a schematic diagram of the framework of a data update method according to an embodiment of this application;
[0024] Figure 5 This is a schematic diagram of a cache-free data update method according to an embodiment of this application;
[0025] Figure 6 This is a flowchart (I) of a cached data update method according to an embodiment of this application;
[0026] Figure 7 This is a flowchart (II) of a cached data update method according to an embodiment of this application;
[0027] Figure 8This is a flowchart (III) illustrating a cached data update method according to an embodiment of this application;
[0028] Figure 9 This is a structural block diagram of a data update device according to an embodiment of this application. Detailed Implementation
[0029] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.
[0030] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0031] According to one aspect of the embodiments of this application, a data update method is provided. This data update method is widely applicable to whole-house intelligent digital control application scenarios such as smart homes, smart home ecosystems, and intelligencehouse ecosystems. Optionally, in this embodiment, the above-mentioned data update method can be applied to, for example... Figure 1 The hardware environment shown consists of terminal device 102 and server 104. For example... Figure 1 As shown, server 104 is connected to terminal device 102 via a network and can be used to provide services (such as application services) to the terminal or clients installed on the terminal. A database can be set up on the server or independently of the server to provide data storage services for server 104. Cloud computing and / or edge computing services can be configured on the server or independently of the server to provide data processing services for server 104.
[0032] The aforementioned network may include, but is not limited to, at least one of the following: wired network, wireless network. The aforementioned wired network may include, but is not limited to, at least one of the following: wide area network, metropolitan area network, local area network. The aforementioned wireless network may include, but is not limited to, at least one of the following: Wi-Fi (Wireless Fidelity), Bluetooth. The terminal device 102 may not be limited to PC, mobile phone, tablet computer, smart air conditioner, smart range hood, smart refrigerator, smart oven, smart stove, smart washing machine, smart water heater, smart washing equipment, smart dishwasher, smart projector, smart TV, smart clothes rack, smart curtains, smart audio-visual equipment, smart socket, smart speaker, smart speaker box, smart fresh air equipment, smart kitchen and bathroom equipment, smart bathroom equipment, smart robot vacuum cleaner, smart window cleaning robot, smart mopping robot, smart air purifier, smart steam oven, smart microwave oven, smart water heater, smart air purifier, smart water dispenser, smart door lock, etc.
[0033] This embodiment provides a data update method, applied to the aforementioned computer terminal. Figure 2 This is a flowchart of a data update method according to an embodiment of this application, which includes the following steps:
[0034] Step S202: Obtain the update identifier corresponding to the log content of the first database. The update identifier is used to indicate whether the log content of the first database has been updated.
[0035] It should be noted that the above update identifier can indicate whether the log content of the entire first database has been updated, or it can indicate whether the log content of a specific data within the first database has been updated. This application does not impose any restrictions on this.
[0036] Step S204: If it is determined that the update identifier indicates that the first data in the first database has been changed to the second data, the second data is obtained from the first database, wherein the first data and the second data correspond to the same data identifier;
[0037] Here, the aforementioned data identifier can be understood as a unique identifier for identifying data values. For example, in key-value pairs, the data identifier can be understood as the key, and the data corresponding to the data identifier can be understood as the value. Specifically, for example, taking the data identifier as key A, the first data as data A1, and the second data as A2, the first data corresponding to key A can be represented as {A, A1}. When the first data is updated to the second data, the second data corresponding to key A can be represented as {A, A2}.
[0038] Step S206: Determine the first data stored in the cache, and update the first data stored in the cache to the second data, wherein the cache and the first database have been pre-synchronized with the first data.
[0039] Through the above steps, an update identifier corresponding to the log content of the first database is obtained, which indicates whether the log content of the first database has been updated; if it is determined that the update identifier indicates that the first data in the first database has been changed to the second data, the second data is obtained from the first database, wherein the first data and the second data correspond to the same data identifier; the first data stored in the cache is determined, and the first data stored in the cache is updated to the second data, wherein the cache and the first database have been pre-synchronized with the first data, thus solving the technical problem in related technologies of how to update cached data to ensure consistency between data in the database and cached data, thereby ensuring consistency between data in the database and cached data when updating cached data.
[0040] In an exemplary embodiment, after updating the first data stored in the cache to the second data, the following scheme can be further implemented: if an update acknowledgment (ACK) identifier generated when the cache updates the first data to the second data is obtained, a first query instruction is determined according to the ACK identifier; based on the first query instruction, a third data is queried from a second database, where the second database is a backup database of the first database, and the third data represents backup data of the second data; if it is determined that the second data and the third data are consistent, a notification message indicating successful update is sent to the terminal device.
[0041] Through the above embodiments, it is possible to determine whether the update was successful based on whether the third data in the second database is consistent with the second data, thereby improving the accuracy of the data update results.
[0042] In one embodiment, if the update confirmation ACK flag generated when the cache updates the first data to the second data is not obtained, it can be determined that the cache has not been successfully updated. In this case, the cache is updated again based on the retry mechanism, and a retry message indicating the re-update is sent to the terminal device. The reasons for the update failure may include network outage, program error, program failure due to insufficient storage space, etc., and this application is not limited to these.
[0043] Alternatively, if the update confirmation ACK flag generated when the cache updates the first data to the second data is not obtained, the first data stored in the cache is directly updated to the third data.
[0044] In other embodiments, if it is determined that the second data and the third data are inconsistent, the second data in the cache is deleted, and the first data stored in the cache is updated to the third data.
[0045] In an exemplary embodiment, to better understand how the process of updating the first data stored in the cache to the second data is implemented in step S206 above, the following steps can be referred to: determining the backup time for backing up the second data of the first database to the second database; generating a delay queue for delayed cache deletion operation according to the backup time, wherein the delay queue corresponds to a delay execution time, the start time of the delay execution time is the time when the first data in the first database is changed to the second data, and the end time of the delay execution time is later than the end time of the backup time; updating the first data stored in the cache to the second data according to the delay execution time corresponding to the delay queue.
[0046] In an exemplary embodiment, a technical solution is proposed for the step of updating the first data stored in the cache to the second data according to the delayed execution time corresponding to the delayed queue. Specifically, the solution includes: after the delayed execution time has elapsed, deleting the first data in the cache based on the cache deletion operation; reading the third data from the second database and writing the third data into the cache.
[0047] In one exemplary embodiment, a technical solution is further proposed for determining the backup time for backing up the second data of the first database to the second database. The specific steps include: determining the historical backup time when backing up the second data of the first database to the second database, and determining the backup time based on the historical backup time, including one of the following: setting the historical backup time with the longest usage time as the backup time; setting the historical backup time with the highest usage frequency as the backup time; setting the historical backup time with the most recent usage time as the backup time.
[0048] The determination of the backup time based on the historical backup time may further include determining the backup time based on the average of multiple historical backup times.
[0049] In one exemplary embodiment, alternatively, other technical solutions for determining the backup time required to back up the second data in the first database to the second database are also proposed, specifically including: determining the backup time for backing up the second data in the first database to the second database based on the type of the first database, the type of the second database, and the network status when backing up the second data in the first database to the second database, so as to determine the backup time for backing up the second data in the first database to the second database.
[0050] In one exemplary embodiment, optionally, a scheme for sending an update success notification message to a terminal device is also proposed, specifically including: Scheme 1, when it is determined that the first data cannot be found in the first database, the second database, and the cache, sending a notification message indicating update success to the terminal device; Scheme 2, when it is determined that the second data can be found in both the first database and the cache, sending a notification message indicating update success to the terminal device; or, when it is determined that the third data can be found in both the second database and the cache, sending a notification message indicating update success to the terminal device.
[0051] To better understand the process of the above data update method, the implementation flow of the above data update method will be described below in conjunction with optional embodiments, but this is not intended to limit the technical solution of the embodiments of this application.
[0052] This embodiment provides a data update method. Figure 3 This is a flowchart illustrating the data update method according to an embodiment of this application, combined with... Figure 3 A technical solution is proposed, with the following specific steps:
[0053] Step S301: Perform the data writing operation through thread A;
[0054] Step S302: Perform a data update operation through the business system;
[0055] Step S303: Database update successful;
[0056] Step S304: Subscribe to the database logs via a message queue;
[0057] In one embodiment, after thread A successfully updates the database, a message queue is used to listen to the database Binlog log and write the log messages to the message queue.
[0058] Step S305: If it is determined from the message queue subscription messages that the database has been updated, update the cache;
[0059] Step S306: After the cache update is successful, send an ACK confirmation message to the message queue;
[0060] Step S307: If the cache update fails, update the cache again based on the retry mechanism;
[0061] In one embodiment, the caching module listens for log messages in a message queue. If it detects an updated log message in the queue, it immediately performs a consumption operation to update the cache. Upon confirming that the caching module has successfully consumed the updated log message, the cache update is successful, and an ACK flag is returned to the message queue to acknowledge successful consumption. If the caching module fails to consume the updated log message due to network issues, the cache update is unsuccessful, and no ACK flag is returned. If the message queue does not receive an ACK flag indicating successful cache consumption, it uses a retry mechanism to re-enable the caching module to perform the consumption operation until it receives an ACK flag from the caching module. This confirms that thread A has completed updating the cache.
[0062] Step S309: Perform the data reading operation through thread B;
[0063] Step S310: Thread B reads data from the cache;
[0064] Step S311: When the cache is empty, read data from the database;
[0065] Step S312: Update the cache based on the data read from the database;
[0066] Step S313: The cache sends feedback to the business system to confirm whether the update was successful.
[0067] Through the above embodiments, after thread A updates the database, it subscribes to the database's binlog logs and pushes messages related to the updated binlog logs to the message queue. This enables the use of the message queue to listen to the binlog logs and synchronously update the cache. This decoupling can be achieved without intruding on the system's business code. In this way, during the operation of the business system, the cache layer can also synchronize the updated data of the database in a timely manner, ensuring the consistency between the cache and the database data.
[0068] In one embodiment, Figure 4 This is a schematic diagram of the framework of a data update method according to an embodiment of this application, such as... Figure 4 As shown, the business system is used to write data to the database, the log subscription middleware (Canal) subscribes to the database to update the binlog logs, writes the binlog logs to the Message Queue, and the caching module is used to consume messages in the message queue to update the cache; the retry mechanism of the Message Queue ensures that the cache consumes messages successfully in the end.
[0069] In high-concurrency business systems, which typically involve more reads than writes, a master-slave database architecture can be employed. This involves setting the master database to be read-write enabled, while the slave databases are read-only. This read-write separation distributes database read and write operations across different nodes, slightly improving write performance while significantly improving read performance. Based on this, this embodiment uses high-concurrency threads to initiate read and write requests for the business system: writing to the master database and reading from the slave databases. Thread A performs the database write operation, writing to the master database; threads B and C perform the database read operation, reading from the slave databases. After thread A successfully writes to the master database, a new asynchronous thread is created to update the cache. When updating the cache, the timing of the update is determined by subscribing to the master database update log. To delay cache deletion using a delayed queue, the delay time for the delayed queue message needs to be set greater than the master-slave replication delay time. After the master and slave databases synchronize and update data, the cache is deleted based on the delayed queue message delay time, allowing threads B and C to read the updated data from the slave database and then write the data to the cache, ultimately maintaining consistency between the database and the cache.
[0070] In other embodiments, Figure 5 This is a schematic diagram of a cache-free data update method according to an embodiment of this application, as shown below. Figure 5 As shown, you can also directly send a request to the business system to query the database to implement a cache-free data update method. However, this method is prone to database crashes or unresponsiveness in high-concurrency scenarios.
[0071] Optionally, in one embodiment, to better understand the process of steps S309-S313 above, in conjunction with... Figure 6 This process will be explained. Figure 6 This is a flowchart (I) of a cached data update method according to an embodiment of this application. The specific steps are as follows:
[0072] Step S601: Initiate a data read request;
[0073] Step S602: Determine whether there is data in the cache; if yes, proceed to step S606; if no, proceed to step S603.
[0074] Step S603: Load the data to be read from the database;
[0075] Step S604: Determine whether the data read from the database is empty; if yes, proceed to step S606; if no, proceed to step S605.
[0076] Step S605: Write the data read from the database into the cache;
[0077] Step S606: Return the data read from the cache.
[0078] Based on the above embodiments, optionally, in combination with Figure 7 Explain the scenario where the cache is deleted first, and then the database is updated. For example... Figure 7 As shown, Figure 7 This is a flowchart (II) illustrating a cached data update method according to an embodiment of this application. When thread A updates the database, thread A first deletes the cache. Subsequently, thread B reads the cache, which is currently empty. If thread B queries the database, thread A has not yet successfully updated the database, and thread B still reads the old value. Then, thread A successfully updates the database. Thread A updates the cache with the new value. Thread B writes the old value it reads back into the cache, overwriting the new value previously written by thread A. The final result is that the cache contains thread B's old value, while the database contains the new value updated by thread A, leading to an inconsistency between the data and the cache.
[0079] In this embodiment, if the cache is deleted first before the database update is complete, the cache will not contain new data. Data needs to be read from the database and then written to the cache, resulting in old data in the cache. In this embodiment, data consistency can be achieved through delayed double-delete. To avoid other threads not being able to read data from the cache during updates, the system can sleep for a period after the database update and then delete the cache again. However, this method cannot accurately estimate the sleep time and may still result in data inconsistency.
[0080] In other embodiments, optionally, combined with Figure 8 Update the database first. Figure 8 This is a flowchart (III) illustrating a cached data update method according to an embodiment of this application. The case of deleting the cache will be explained. For example... Figure 8 As shown, after thread A successfully updates the database, it may fail to update the cache if issues arise, such as failure to delete the cache or the cache not being deleted in time. If thread B then reads the cache, the cached value will be the old value, resulting in a cache inconsistency.
[0081] In this embodiment, if the database update succeeds but the cache deletion fails, the database will contain the new value, while the cache will contain the old value. Subsequent read requests will retrieve the old data. Only after the cache expires can the correct value be stored in the database. In this embodiment, after a successful database update, a message can be sent to a message queue. The client consumes the message from the queue and deletes the cache, leveraging the queue's retry mechanism to achieve eventual consistency. However, introducing a message queue also introduces issues such as ensuring message loss, delay, and ordering, potentially leading to data inconsistency.
[0082] Compared to Figures 7-8 Data update method Figure 3 This implementation allows for real-time tracking of database operations by subscribing to the Binlog logs of database operation updates. It decouples from the publish-subscribe message middleware system, implementing functionality without intruding on the business system. Write operations are performed by thread A, using the log middleware (Canal) to write messages to MQ (Kafka, RacketMQ) in real time. The cache module subscribes to messages in the message queue to update the cache; if an update fails, a retry mechanism allows for repeated updates until the message is successfully updated. Using a retry mechanism increases the probability of cache updates, preventing a single failure from halting updates. Furthermore, leveraging the message ordering of the message queue avoids out-of-order updates, resolving issues related to multi-threaded updates and database ordering.
[0083] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods of the various embodiments of this application.
[0084] Figure 9 This is a structural block diagram of a data update device according to an embodiment of this application; as shown below. Figure 9 As shown, it includes:
[0085] The first update module 92 is used to obtain the update identifier corresponding to the log content of the first database, wherein the update identifier is used to indicate whether the log content of the first database has been updated;
[0086] It should be noted that the above update identifier can indicate whether the log content of the entire first database has been updated, or it can indicate whether the log content of a specific data within the first database has been updated. This application does not impose any restrictions on this.
[0087] The acquisition module 94 is configured to acquire the second data from the first database when it is determined that the update identifier indicates that the first data in the first database has been changed to the second data, wherein the first data and the second data correspond to the same data identifier;
[0088] Here, the aforementioned data identifier can be understood as a unique identifier for identifying data values. For example, in key-value pairs, the data identifier can be understood as the key, and the data corresponding to the data identifier can be understood as the value. Specifically, for example, taking the data identifier as key A, the first data as data A1, and the second data as A2, the first data corresponding to key A can be represented as {A, A1}. When the first data is updated to the second data, the second data corresponding to key A can be represented as {A, A2}.
[0089] The second update module 96 is used to determine the first data stored in the cache and update the first data stored in the cache to the second data, wherein the cache and the first database have been pre-synchronized with the first data.
[0090] Using the above-described apparatus, an update identifier corresponding to the log content of the first database is obtained, the update identifier indicating whether the log content of the first database has been updated; if it is determined that the update identifier indicates that the first data in the first database has been changed to the second data, the second data is obtained from the first database, wherein the first data and the second data correspond to the same data identifier; the first data stored in the cache is determined, and the first data stored in the cache is updated to the second data, wherein the cache and the first database have been pre-synchronized with the first data, thus solving the technical problem in related technologies of how to update cached data to ensure consistency between data in the database and cached data, thereby ensuring consistency between data in the database and cached data when updating cached data.
[0091] In an exemplary embodiment, the data update apparatus further includes: a determining module, configured to, upon obtaining an update confirmation ACK identifier generated when the cache updates the first data to the second data, determine a first query instruction based on the ACK identifier; query a third data from a second database based on the first query instruction, wherein the second database is a backup database of the first database, and the third data represents backup data of the second data; and, upon determining that the second data and the third data are consistent, send a notification message to a terminal device indicating successful update.
[0092] In one embodiment, if the update confirmation ACK flag generated when the cache updates the first data to the second data is not obtained, it can be determined that the cache has not been successfully updated. In this case, the cache is updated again based on the retry mechanism, and a retry message indicating the re-update is sent to the terminal device. The reasons for the update failure may include network outage, program error, program failure due to insufficient storage space, etc., and this application is not limited to these.
[0093] Alternatively, if the update confirmation ACK flag generated when the cache updates the first data to the second data is not obtained, the first data stored in the cache is directly updated to the third data.
[0094] In other embodiments, if it is determined that the second data and the third data are inconsistent, the second data in the cache is deleted, and the first data stored in the cache is updated to the third data.
[0095] In an exemplary embodiment, the second update module 96 is further configured to determine the backup time for backing up the second data of the first database to the second database; generate a delay queue for delayed cache deletion operation based on the backup time, wherein the delay queue corresponds to a delay execution time, the start time of the delay execution time is the time when the first data in the first database is changed to the second data, and the end time of the delay execution time is later than the end time of the backup time; and update the first data stored in the cache to the second data according to the delay execution time corresponding to the delay queue.
[0096] In an exemplary embodiment, the second update module 96 is further configured to, after the delay execution time, delete the first data in the cache based on the cache deletion operation; read the third data from the second database and write the third data into the cache.
[0097] In an exemplary embodiment, the second update module 96 is further configured to determine the historical backup time when backing up the second data in the first database to the second database, and to determine the backup time based on the historical backup time, including one of the following: setting the historical backup time with the longest usage time as the backup time; setting the historical backup time with the highest usage frequency as the backup time; setting the historical backup time with the most recent usage time as the backup time.
[0098] The determination of the backup time based on the historical backup time may further include determining the backup time based on the average of multiple historical backup times.
[0099] In an exemplary embodiment, the second update module 96 is further configured to set the standard backup time for backing up the second data in the first database to the second database as the backup time, wherein the standard backup time is determined based on the type of the first database, the type of the second database, and the network status when backing up the second data in the first database to the second database.
[0100] In one exemplary embodiment, the data update apparatus further includes: a sending module, configured to send a notification message indicating successful update to a terminal device when it is determined that the first data cannot be found in the first database, the second database, and the cache; or, when it is determined that the second data can be found in both the first database and the cache, send a notification message indicating successful update to the terminal device; or, when it is determined that the third data can be found in both the second database and the cache, send a notification message indicating successful update to the terminal device.
[0101] Embodiments of this application also provide a storage medium including a stored program, wherein the program executes any of the methods described above when it is run.
[0102] Optionally, in this embodiment, the storage medium may be configured to store program code for performing the following steps:
[0103] S1, obtain the update identifier corresponding to the log content of the first database, the update identifier being used to indicate whether the log content of the first database has been updated;
[0104] S2, if it is determined that the update identifier indicates that the first data in the first database has been changed to the second data, the second data is obtained from the first database, wherein the first data and the second data correspond to the same data identifier;
[0105] S3, determine the first data stored in the cache, and update the first data stored in the cache to the second data, wherein the cache and the first database have been pre-synchronized with the first data.
[0106] Embodiments of this application also provide an electronic device including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0107] Optionally, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0108] Optionally, in this embodiment, the processor can be configured to perform the following steps via a computer program:
[0109] S1, obtain the update identifier corresponding to the log content of the first database, the update identifier being used to indicate whether the log content of the first database has been updated;
[0110] S2, if it is determined that the update identifier indicates that the first data in the first database has been changed to the second data, the second data is obtained from the first database, wherein the first data and the second data correspond to the same data identifier;
[0111] S3, determine the first data stored in the cache, and update the first data stored in the cache to the second data, wherein the cache and the first database have been pre-synchronized with the first data.
[0112] Optionally, in this embodiment, the storage medium may include, but is not limited to, various media capable of storing program code, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.
[0113] Optionally, specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementations, and will not be repeated here.
[0114] Obviously, those skilled in the art should understand that the modules or steps of this application described above can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. Optionally, they can be implemented using computer-executable program code, thereby storing them in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those presented here, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, this application is not limited to any particular combination of hardware and software.
[0115] The above description is only a preferred embodiment of this application. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of this application, and these improvements and modifications should also be considered within the scope of protection of this application.
Claims
1. A data update method, characterized in that, include: Obtain the update identifier corresponding to the log content of the first database, wherein the update identifier is used to indicate whether the log content of the first database has been updated; If it is determined that the update identifier indicates that the first data in the first database has been changed to the second data, the second data is retrieved from the first database, wherein the first data and the second data correspond to the same data identifier; Determine the first data stored in the cache, and update the first data stored in the cache with the second data, wherein the cache and the first database have been pre-synchronized with the first data; The method further includes, after updating the first data stored in the cache to the second data, the following steps: if an update confirmation ACK flag generated when the cache updates the first data to the second data is obtained, determining a first query instruction based on the ACK flag; querying a third data from a second database based on the first query instruction, wherein the second database is a backup database of the first database and the third data represents backup data of the second data; if the second data and the third data are determined to be consistent, sending a notification message to the terminal device to indicate that the update was successful; if the ACK flag is not obtained, updating the cache again based on a retry mechanism, or directly querying the third data from the second database and updating the first data stored in the cache to the third data; if the second data and the third data are determined to be inconsistent, deleting the second data in the cache and updating the first data stored in the cache to the third data.
2. The data update method according to claim 1, characterized in that, Updating the first data stored in the cache to the second data includes: Determine the backup time to back up the second data from the first database to the second database; A delayed queue for delayed cache deletion operations is generated based on the backup time. The delayed queue corresponds to a delayed execution time. The start time of the delayed execution time is the time when the first data in the first database is changed to the second data. The end time of the delayed execution time is later than the end time of the backup time. The first data stored in the cache is updated to the second data according to the delay execution time corresponding to the delay queue.
3. The data update method according to claim 2, characterized in that, Update the first data stored in the cache with the second data according to the delay execution time corresponding to the delay queue, including: After the aforementioned delay period, the first data in the cache is deleted based on the cache deletion operation; Read the third data from the second database and write the third data into the cache.
4. The data update method according to claim 2, characterized in that, Determining the backup time for backing up the second data from the first database to the second database includes: Determine the historical backup time when backing up the second data from the first database to the second database, and determine the backup time based on the historical backup time, including one of the following: Set the longest historical backup time as the backup time; Set the most frequently used historical backup time as the backup time; Set the most recent historical backup time as the backup time.
5. The data update method according to claim 2, characterized in that, Determine the backup time required to back up the second data from the first database to the second database, including: The backup time for backing up the second data in the first database to the second database is determined based on the type of the first database, the type of the second database, and the network status when the second data in the first database is backed up to the second database, so as to determine the backup time for backing up the second data in the first database to the second database.
6. The data update method according to claim 1, characterized in that, The method further includes: If the first data cannot be found in the first database, the second database, and the cache, a notification message indicating successful update is sent to the terminal device. Alternatively, if it is determined that the second data is found in both the first database and the cache, a notification message indicating that the update was successful is sent to the terminal device. Alternatively, if it is determined that the third data is found in both the second database and the cache, a notification message indicating successful update is sent to the terminal device.
7. A data update device, characterized in that, include: The first update module is used to obtain the update identifier corresponding to the log content of the first database, wherein the update identifier is used to indicate whether the log content of the first database has been updated; The acquisition module is configured to acquire the second data from the first database when it is determined that the update identifier indicates that the first data in the first database has been changed to the second data, wherein the first data and the second data correspond to the same data identifier; The second update module is used to determine the first data stored in the cache and update the first data stored in the cache to the second data, wherein the cache and the first database have been pre-synchronized with the first data; The second update module is further configured to, upon obtaining an update confirmation ACK flag generated when the cache updates the first data to the second data, determine a first query instruction based on the ACK flag; query a third data from a second database based on the first query instruction, wherein the second database is a backup database of the first database and the third data represents backup data of the second data; if the second data and the third data are determined to be consistent, send a notification message to the terminal device indicating successful update; if the ACK flag is not obtained, update the cache again based on a retry mechanism, or directly query the third data from the second database and update the first data stored in the cache to the third data; if the second data and the third data are determined to be inconsistent, delete the second data in the cache and update the first data stored in the cache to the third data.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored program, wherein the program, when executed, performs the method described in any one of claims 1 to 6.
9. An electronic device comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to execute the method described in any one of claims 1 to 6 through the computer program.
Citation Information
Patent Citations
Cache updating method and system based on Canal
CN109241072A
Data processing method and device, equipment and storage medium
CN113094430A