Method and device for realizing consistency of cached data, equipment and storage medium

By synchronizing the data to be updated between the primary database, cache and slave database, and using the first write function to update the target update data in the cache, the problem of inconsistency between cached data and master database data is solved, and data consistency and system performance are improved.

CN119829590APending Publication Date: 2025-04-15KE COM (BEIJING) TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411812256.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-10
Publication Date
2025-04-15

AI Technical Summary

Technical Problem

In the prior art, the data in the cache is inconsistent with the data in the main database, resulting in reduced data security, interruption of business processes, impaired system performance, business decision-making errors, and degraded user experience.

Method used

By obtaining the data to be updated and writing it to the main database, cache and slave database; receiving a read request to read the data to be updated, and reading the slave database according to the read request, obtaining the database read results; converting the database read results to obtain the target update data; using the first write function to write the target update data to the cache to achieve consistency between the cached data and the main database data.

Benefits of technology

The cached data is consistent with the main database data, avoiding various negative effects caused by data inconsistency, and improving data security and system performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119829590A_ABST
    Figure CN119829590A_ABST
Patent Text Reader

Abstract

The invention provides a method and device for realizing consistency of cached data, equipment and a storage medium, and relates to the technical field of databases. The method comprises the following steps: acquiring to-be-updated data, and writing the to-be-updated data into a master database, a cache and a slave database; receiving a reading request for reading the to-be-updated data, and performing reading processing on the slave database according to the reading request to obtain a database reading result for reading the to-be-updated data; performing conversion processing on data corresponding to the database reading result to obtain target update data; and writing the target update data into the cache by utilizing the first write-in function so as to realize the consistency of the to-be-updated data in the cache and the to-be-updated data in the main database. The embodiment provided by the invention is used for solving the defect that the data in the cache is inconsistent with the data in the main database in the prior art, and realizing that the converted target update data is written into the cache by utilizing the first write-in function, so that the data in the cache is kept consistent with the data in the main database.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of database technology, and in particular to a method, device, equipment and storage medium for achieving cache data consistency. Background Art

[0002] The importance of cache in high-concurrency systems is self-evident. Cache is used extensively in projects and handles most of the query traffic, but there are also many points that need attention. The most typical example is the data consistency issue.

[0003] Regarding the issue of data consistency, in the master-slave mode of the database, the system writes to the master database and reads from the slave database. When the database data is synchronized from the master database to the slave database, there will be a delay between the master and slave databases. If the cache is not used, the read data will be delayed by at most one database master-slave synchronization cycle (generally no more than 1 second). If Redis cache is used, assuming the cache cycle is 15 minutes, if old data is read from the lagging slave database and put into Redis, the existence time of this old data will be extended to 15 minutes. In this 15 minutes, the data in the cache will be inconsistent with the data in the master database.

[0004] Inconsistency between the data in the cache and the data in the main database will have many serious impacts, including but not limited to reduced data security, interrupted business processes, impaired system performance, incorrect business decisions, and reduced user experience. Summary of the invention

[0005] The present invention provides a method, device, equipment and storage medium for achieving cache data consistency, which is used to solve the defect of inconsistency between data in the cache and data in the main database in the prior art, and realizes that the data in the cache is kept consistent with the data in the main database by writing the converted target update data into the cache through the use of a first write function.

[0006] In a first aspect, the present invention provides a method for achieving cache data consistency, comprising the following steps.

[0007] Obtaining data to be updated, and writing the data to be updated into a master database, a cache, and a slave database; receiving a read request for reading the data to be updated, and performing a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated; Performing conversion processing on the data corresponding to the database reading result to obtain target update data; The target update data is written into the cache using a first write function, so that the data to be updated in the cache is consistent with the data to be updated in the main database.

[0008] Preferably, according to the method for achieving cache data consistency provided by the present invention, the data corresponding to the database read result is converted to obtain target update data, including: Get the system ID of the database system; Adding prefix data and suffix data to the system identifier to generate target identifier data; The target identification data and the data corresponding to the database reading result are combined and processed to obtain the target update data.

[0009] Preferably, according to the method for achieving cache data consistency provided by the present invention, before the step of performing a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated, the method includes: Performing a read process on the cache according to the read request to obtain a cache read result; When the cache reading result indicates that the data to be updated exists in the cache, generating prompt information that the data to be updated in the cache is consistent with the data to be updated in the main database; When the cache reading result indicates that the read value of the to-be-updated data read from the cache is a null value, the step of performing the reading process on the slave database according to the read request to obtain the database reading result of reading the to-be-updated data is performed.

[0010] Preferably, according to the method for achieving cache data consistency provided by the present invention, the writing rules corresponding to the first writing function include: When the cache reading result indicates that the to-be-updated data exists in the cache, the step of updating the to-be-updated data by using the target update data is not performed; When the cache read result indicates that the read value of the to-be-updated data read from the cache is a null value, the to-be-updated data whose read value is a null value is updated by using the target update data.

[0011] Preferably, according to the method for achieving cache data consistency provided by the present invention, before the step of writing the data to be updated into the master database, the cache and the slave database, the method comprises: Acquire a competing lock according to the system identifier; Locking the first write operation and the second write operation according to the competing lock; The first write operation is an operation of writing the data to be updated into the primary database, and the second write operation is an operation of writing the data to be updated into the cache.

[0012] Preferably, according to the method for achieving cache data consistency provided by the present invention, the step of writing the data to be updated into the slave database includes: Obtaining a binary log for writing the data to be updated into the primary database; By copying the binary log, the data to be updated is written into the slave database.

[0013] In a second aspect, the present invention further provides a device for achieving cache data consistency, comprising the following modules: An acquisition module is used to acquire the data to be updated and write the data to be updated into a master database, a cache and a slave database; A reading module, used for receiving a reading request for reading the data to be updated, and performing a reading process on the slave database according to the reading request to obtain a database reading result of reading the data to be updated; A conversion module, used for converting the data corresponding to the database reading result to obtain target update data; A consistency implementation module is used to write the target update data into the cache using a first write function, so as to achieve consistency between the data to be updated in the cache and the data to be updated in the main database.

[0014] In a third aspect, the present invention further provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method for achieving cache data consistency as described in any one of the above is implemented.

[0015] In a fourth aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements any of the methods for achieving cache data consistency as described above.

[0016] In a fifth aspect, the present invention further provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the method for achieving cache data consistency as described in any one of the above is implemented.

[0017] The present invention provides a method, device, equipment and storage medium for achieving consistency of cache data, which obtains data to be updated and writes the data to be updated into a main database, a cache and a slave database; receives a read request for reading the data to be updated, and performs a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated; performs a conversion process on the data corresponding to the database read result to obtain target update data; and uses a first write function to write the target update data into the cache to achieve consistency between the data to be updated in the cache and the data to be updated in the main database. The method is used to solve the defect of inconsistency between data in the cache and data in the main database in the prior art, and achieves consistency between data in the cache and data in the main database by writing the converted target update data into the cache using the first write function. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0019] Figure 1 This is one of the flow charts of the method for achieving cache data consistency provided by the present invention.

[0020] Figure 2 This is the second flow chart of the method for achieving cache data consistency provided by the present invention.

[0021] Figure 3 It is a structural schematic diagram of a device for achieving cache data consistency provided by the present invention.

[0022] Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0023] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0024] First, several nouns involved in the present invention are analyzed: The Master Database (MD) is a central database that stores and manages data and is responsible for processing all write operations and data updates.

[0025] A slave database (SD) is a copy of the master database and is mainly used for read operations to reduce the load on the master database.

[0026] Redis cache, Redis is an open source, memory-based data structure storage system used as a database, cache, and message broker. Redis was originally designed as an in-memory database, but it also provides a persistence mechanism that can periodically save data to disk to prevent data loss.

[0027] A contention lock is a mechanism used to control multiple threads' access to shared resources in a multi-threaded environment. It prevents multiple threads from modifying the same resource at the same time, avoiding data contention and inconsistency. "Key" is a very important concept, which is used to uniquely identify and access stored data.

[0028] Primary key: Each table usually has a primary key, which is used to uniquely identify each row of data in the table.

[0029] ucid is a data type in Shared UUI (Universally Unique Identifier) ​​and is used to uniquely identify a call in a customer's movement.

[0030] In related technologies, inconsistency between data in the cache and data in the main database will bring many serious impacts, including but not limited to reduced data security, interrupted business processes, impaired system performance, wrong business decisions, and reduced user experience.

[0031] Combine the following Figure 1-Figure 4 The present invention describes a method, device, equipment and storage medium for achieving cache data consistency, which is used to solve the defect of inconsistency between data in the cache and data in the main database in the prior art, and achieves consistency between the data in the cache and the data in the main database by writing the converted target update data into the cache through the use of a first write function.

[0032] Figure 1 This is one of the flow charts of realizing cache data consistency provided by the present invention, such as Figure 1 As shown, the method may include but is not limited to steps S100 to S400: S100, obtaining data to be updated, and writing the data to be updated into a master database, a cache, and a slave database; S200, receiving a read request for reading the data to be updated, and performing a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated; S300, converting the data corresponding to the database reading result to obtain target update data; S400, receiving a control instruction for controlling the virtual scene sent by the client through the second channel, and controlling the real scene to perform a corresponding operation based on the control instruction.

[0033] In step S100 of some embodiments, the data to be updated is obtained, and the data to be updated is written into the master database, the cache, and the slave database.

[0034] It is understandable that the database system obtains the data to be updated, and the data to be updated is, for example, "1000000000000001, name: Wang Er".

[0035] When a write request is received, the data to be updated is first obtained. After obtaining the data to be updated, the next execution step is to write the data to be updated into the main database and cache.

[0036] After the data to be updated is written into the master database, the data to be updated is synchronously written into the slave database.

[0037] It should be noted that the master database is usually used to manage, store and write data, while the slave database is usually used to read and query data.

[0038] It should be further explained that before writing the data to be updated into the master database, cache and slave database, a contention lock is first acquired according to the system identifier, and then the first write operation and the second write operation are locked according to the contention lock to avoid multiple concurrent write operations resulting in inconsistencies between the data in the master database and the data in the cache.

[0039] The first write operation is an operation of writing the data to be updated into the main database, and the second write operation is an operation of writing the data to be updated into the cache.

[0040] In some embodiments of the present invention, it is assumed that one of the data in the main database, the name corresponding to the ucid 1000000000000001 is Wang Yi.

[0041] Write operation 1 (data to be updated 1): Change the name of 1000000000000001 to: Wang Er.

[0042] Write operation 2 (data to be updated 2): Change the name of 1000000000000001 to: Wang San.

[0043] Write operation 1 first writes to the main database, changes the name to Wang Er, and is about to write the name Wang Er into the Redis cache, but the writing is slow. At this time, write operation 2 changes the name in the main database to Wang San, and writes the name Wang San's data into the Redis cache as Wang San.

[0044] At this time, the write operation 1 writes to Redis cache and writes the name to Wang Er. Now the name in the main database is Wang San, but the name in the Redis cache is Wang Er, which will cause the data in the main database to be inconsistent with the data in the Redis cache, which will have a serious impact.

[0045] By acquiring a contention lock according to a system identifier and then locking the first write operation and the second write operation according to the contention lock, it is possible to avoid inconsistency between data in the main database and data in the cache caused by multiple concurrent write operations.

[0046] That is, after write operation 1 writes the name Wang Er into the main database, write operation 2 cannot be executed. You must wait until write operation 1 writes the name Wang Er into the main database, and then writes the name Wang Er into the Redis cache before you can execute write operation 2, write the data of Wang San in write operation 2 into the main database, and then write the data of Wang San in write operation 2 into the Redis cache. This can avoid the inconsistency problem between the data in the main database and the data in the cache.

[0047] In some embodiments of the present invention, the step of writing the to-be-updated data into the slave database includes: Obtaining a binary log for writing the data to be updated into the primary database; By copying the binary log, the data to be updated is written into the slave database.

[0048] It should be noted that the master database records all data changes through binary logs (binlog) so that data can be replicated in the slave database. It is understandable that when the data to be updated is written into the master database, a corresponding binary log is generated, and then the data to be updated is written into the slave database by copying the binary log.

[0049] Furthermore, the slave database can read the binary log of the master database through the I / O thread and SQL thread and apply the changes to the local database.

[0050] A one-master-multiple-slave architecture can be implemented to further improve the fault tolerance and performance of the database system.

[0051] In step S200 of some embodiments, a read request for reading the data to be updated is received, and a read process is performed on the slave database according to the read request to obtain a database read result for reading the data to be updated.

[0052] It is understandable that after executing step S100, the specific execution steps may be: the server receives a read request for reading the data to be updated, and performs read processing on the slave database according to the read request to obtain a database read result for reading the data to be updated.

[0053] It should be noted that after receiving a read request for reading the data to be updated, the cache is first read according to the read request to obtain a cache read result.

[0054] When the cache reading result indicates that the data to be updated exists in the cache, a prompt message is generated that the data to be updated in the cache is consistent with the data to be updated in the main database, and the prompt message indicates that the data to be updated in the Redis cache is consistent with the data to be updated in the main database.

[0055] When the cache reading result indicates that the read value of the to-be-updated data read from the cache is a null value, the step of performing the reading process on the slave database according to the read request to obtain the database reading result of reading the to-be-updated data is performed.

[0056] It can be understood that when the cache read result indicates that the read value of the data to be updated read from the Redis cache is a null value, it means that the data to be updated is still performing a write operation to the Redis cache and has not been written yet, so the read value is a null value. At this time, it is necessary to perform a read process on the slave database according to the read request to determine whether the write operation speed of the slave database is faster. Therefore, the data to be updated is read from the slave database to obtain the database read result.

[0057] Then the database reading results have the following results. The first result is that the data to be updated read from the database is a null value, which means that the write operation has not been completed in the database.

[0058] The second result is that the data to be updated read from the database is the original data to be updated. For example, the name of the original data to be updated is Wang Yi, and the name of the newly written data to be updated is Wang Er. However, since the operation of writing Wang Er has not been completed, the original data to be updated whose name is Wang Yi is read.

[0059] The third result is that the data to be updated read from the database is new data to be updated, that is, the data to be updated whose name is Wang Er is read.

[0060] In step S300 of some embodiments, the data corresponding to the database reading result is converted to obtain target update data.

[0061] It can be understood that after executing step S200, the specific execution steps may be: obtaining the system identifier of the database system, adding prefix data and suffix data to the system identifier, generating target identification data, combining the target identification data and the data corresponding to the database reading result to obtain the target update data.

[0062] The system identifier can be ucid, which is the unique identifier of the entire system. Assuming the prefix is ​​prefix_ and the suffix is ​​_suffix, the complete key name, that is, the target identification data is prefix_ucid_suffix.

[0063] Then, the target update data is generated based on the target identification data and the read data result. The target update data can be a combination of a null value and the target identification data, a combination of the original data to be updated and the target identification data, or a combination of the newly written data to be updated and the target identification data.

[0064] In step S400 of some embodiments, the target update data is written into the cache using a first write function, so that the data to be updated in the cache is consistent with the data to be updated in the main database.

[0065] It can be understood that after executing step S300, the specific execution steps may be: writing the target update data into the cache using the first write function.

[0066] The writing rules corresponding to the first writing function include: When the cache reading result indicates that the to-be-updated data exists in the cache, the step of updating the to-be-updated data by using the target update data is not performed; When the cache read result indicates that the read value of the to-be-updated data read from the cache is a null value, the to-be-updated data whose read value is a null value is updated by using the target update data.

[0067] It can be understood that in the Redis cache, the first write function is setnx().

[0068] The setnx (Set if Not eXists) command is used to set a key-value pair, but only if the key does not exist. This ensures that existing cache data is not overwritten.

[0069] Assume you have a key ucid and you want to add prefix and suffix to it and then write the target update data using setnx() method.

[0070] If the key does not exist, the setting succeeds and returns True; if the key already exists, no operation is performed and False is returned.

[0071] Based on the return value, you can determine whether the data is newly inserted or the key already exists. In this way, you can ensure that the existing data will not be overwritten when writing Redis cache data.

[0072] More specifically, when the cache reading result indicates that the data to be updated exists in the cache, the step of updating the data to be updated using the target update data is not executed, no operation is performed and False is returned. At this time, repeated updates that cause the data in the cache to be inconsistent with the data in the main database can be avoided.

[0073] In the case where the cache read result indicates that the read value of the data to be updated read from the cache is a null value, the target update data is used to update the data to be updated whose read value is a null value. Updating the target update data to the data to be updated whose read value is a null value will not cause inconsistency between the data in the cache and the data in the main database, even if the target update data is the original data to be updated. Because after the target update data is used to update the data to be updated whose read value is a null value, the write operation is still writing to the cache after writing to the main database. Therefore, even if the original data to be updated exists in the cache, the write operation can quickly write the data to be updated into the cache to overwrite the original data to be updated, so that the original data to be updated does not exist in the cache throughout the data life cycle, making the data in the cache inconsistent with the data in the main database.

[0074] like Figure 2 The figure shows the second flow chart of the method for achieving cache data consistency provided by the present invention. In some embodiments of the present invention, the method for achieving cache data consistency mainly includes two parts, namely, a write operation and a read operation.

[0075] For write operations: In the operation of writing to the main database, the writing to the database and the writing to the cache are first regarded as an atomic operation, and a competing lock is applied for according to the ucid (the unique ID of the entire system). After the competing lock is obtained, the writing operation can be performed. If the competing lock is not obtained, the writing operation cannot be performed and the wait for the lock continues.

[0076] After obtaining the lock, the data to be updated is first written into the main database, and the set() method is used to write the data to be updated into the Redis cache.

[0077] For read operations: First read the Redis cache, and if the data is read, return directly. If the data cannot be read, read the query from the database. After the query is completed, regardless of whether the value of the data to be updated is found, write a piece of data to the Redis cache, that is, the key is ucid plus the prefix and suffix, and the value is empty or the found data. Then use the (first write function) setnx() method to write, which can ensure that the existing cache data is not overwritten.

[0078] The set() method is used to set a key-value pair. If the key already exists, its value is overwritten; if the key does not exist, a new key-value pair is created.

[0079] If the key does not exist, the setting succeeds and returns True; if the key already exists, no operation is performed and False is returned.

[0080] Based on the return value, you can determine whether the data is newly inserted or the key already exists. In this way, you can ensure that the existing data will not be overwritten when writing Redis cache data.

[0081] In some embodiments of the present invention, the problem of old data overwriting new data due to concurrent reading and writing can be solved: Suppose there is a data 1000000000000001. The name corresponding to this ucid is Wang Yi. Write operation a: Change the name of 1000000000000001 to: Wang Er At this time, write operation a has changed the name in both the database master and Redis to "Wang Er". At this time, the data in the database is being synchronized to the slave database. Before the synchronization is completed, a read operation b comes: read the name 10000000000000001.

[0082] At this time, the name that operation b will read is "Wang Yi" before the modification. At this time, if operation b uses set() to write data to the cache, the new data "Wang Er" in the cache will be overwritten, and the old data will exist for 15 minutes.

[0083] The method for achieving cache data consistency provided by the present invention can solve this problem well: setnx() is used instead of set() in the solution to ensure that newer data in the cache will not be overwritten, thereby achieving consistency between the data in the cache and the data in the main database.

[0084] The present invention provides a method, device, equipment and storage medium for achieving consistency of cache data, which obtains data to be updated and writes the data to be updated into a main database, a cache and a slave database; receives a read request for reading the data to be updated, and performs a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated; performs a conversion process on the data corresponding to the database read result to obtain target update data; and uses a first write function to write the target update data into the cache to achieve consistency between the data to be updated in the cache and the data to be updated in the main database. The method is used to solve the defect of inconsistency between data in the cache and data in the main database in the prior art, and achieves consistency between data in the cache and data in the main database by writing the converted target update data into the cache using the first write function.

[0085] The following is a description of an apparatus for implementing cache data consistency provided by the present invention. The apparatus for implementing cache data consistency described below and the method for implementing cache data consistency described above can be referenced to each other.

[0086] like Figure 3 FIG. 1 is a schematic diagram of a structure of a device for implementing cache data consistency provided by the present invention, wherein the device for implementing cache data consistency comprises: An acquisition module 310 is used to acquire data to be updated and write the data to be updated into a master database, a cache and a slave database; The reading module 320 is used to receive a reading request for reading the data to be updated, and perform a reading process on the slave database according to the reading request to obtain a database reading result of reading the data to be updated; The conversion module 330 is used to convert the data corresponding to the database reading result to obtain the target update data; The consistency achieving module 340 is used to write the target update data into the cache using a first write function, so as to achieve consistency between the data to be updated in the cache and the data to be updated in the main database.

[0087] Preferably, the device for achieving cache data consistency provided by the present invention, the conversion module 330, is further specifically used to obtain a system identifier of the database system; Adding prefix data and suffix data to the system identifier to generate target identifier data; The target identification data and the data corresponding to the database reading result are combined and processed to obtain the target update data.

[0088] Preferably, the device for achieving cache data consistency provided by the present invention is further used to perform a read process on the cache according to the read request to obtain a cache read result; When the cache reading result indicates that the data to be updated exists in the cache, generating prompt information that the data to be updated in the cache is consistent with the data to be updated in the main database; When the cache reading result indicates that the read value of the to-be-updated data read from the cache is a null value, the step of performing the reading process on the slave database according to the read request to obtain the database reading result of reading the to-be-updated data is performed.

[0089] Preferably, in the device for achieving cache data consistency provided by the present invention, the write rule corresponding to the first write function is further used for not executing the step of updating the data to be updated by using the target update data when the cache read result indicates that the data to be updated exists in the cache; When the cache read result indicates that the read value of the to-be-updated data read from the cache is a null value, the to-be-updated data whose read value is a null value is updated by using the target update data.

[0090] Preferably, the device for achieving cache data consistency provided by the present invention is further used to obtain a contention lock according to the system identifier; Locking the first write operation and the second write operation according to the competing lock; The first write operation is an operation of writing the data to be updated into the primary database, and the second write operation is an operation of writing the data to be updated into the cache.

[0091] Preferably, the device for achieving cache data consistency provided by the present invention is further used for writing the to-be-updated data into a slave database, comprising: Obtaining a binary log for writing the data to be updated into the primary database; By copying the binary log, the data to be updated is written into the slave database.

[0092] The present invention provides a method, device, equipment and storage medium for achieving consistency of cache data, which obtains data to be updated and writes the data to be updated into a main database, a cache and a slave database; receives a read request for reading the data to be updated, and performs a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated; performs a conversion process on the data corresponding to the database read result to obtain target update data; and uses a first write function to write the target update data into the cache to achieve consistency between the data to be updated in the cache and the data to be updated in the main database. The method is used to solve the defect of inconsistency between data in the cache and data in the main database in the prior art, and achieves consistency between data in the cache and data in the main database by writing the converted target update data into the cache using the first write function.

[0093] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4 As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute a method for achieving cache data consistency, the method comprising: obtaining the data to be updated, and writing the data to be updated into the main database, the cache and the slave database; receiving a read request for reading the data to be updated, and performing a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated; performing a conversion process on the data corresponding to the database read result to obtain the target update data; and using a first write function to write the target update data into the cache to achieve consistency between the data to be updated in the cache and the data to be updated in the main database.

[0094] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product 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.) to perform all or part of the 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, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0095] On the other hand, the present invention also provides a computer program product, which includes a computer program. The computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the method provided by the above-mentioned methods to achieve cache data consistency, which method includes: obtaining the data to be updated and writing the data to be updated into a main database, a cache and a slave database; receiving a read request to read the data to be updated, and performing a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated; performing a conversion process on the data corresponding to the database read result to obtain the target update data; and using a first write function to write the target update data into the cache to achieve consistency between the data to be updated in the cache and the data to be updated in the main database.

[0096] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, it is implemented to execute the method for achieving cache data consistency provided by the above-mentioned methods, the method comprising: obtaining the data to be updated, and writing the data to be updated into a main database, a cache and a slave database; receiving a read request for reading the data to be updated, and performing a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated; performing a conversion process on the data corresponding to the database read result to obtain target update data; and using a first write function to write the target update data into the cache to achieve consistency between the data to be updated in the cache and the data to be updated in the main database.

[0097] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0098] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0099] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has 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 make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for achieving cache data consistency, characterized in that: include: Obtaining data to be updated, and writing the data to be updated into a master database, a cache, and a slave database; receiving a read request for reading the data to be updated, and performing a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated; Performing conversion processing on the data corresponding to the database reading result to obtain target update data; The target update data is written into the cache using a first write function, so that the data to be updated in the cache is consistent with the data to be updated in the main database.

2. The method for achieving cache data consistency according to claim 1, characterized in that: The converting and processing the data corresponding to the database reading result to obtain the target update data includes: Get the system ID of the database system; Adding prefix data and suffix data to the system identifier to generate target identifier data; The target identification data and the data corresponding to the database reading result are combined and processed to obtain the target update data.

3. The method for achieving cache data consistency according to claim 2, characterized in that: Before the step of performing a read process on the slave database according to the read request to obtain a database read result of reading the data to be updated, the method includes: Performing a read process on the cache according to the read request to obtain a cache read result; When the cache reading result indicates that the data to be updated exists in the cache, generating prompt information that the data to be updated in the cache is consistent with the data to be updated in the main database; When the cache reading result indicates that the read value of the to-be-updated data read from the cache is a null value, the step of performing the reading process on the slave database according to the read request to obtain the database reading result of reading the to-be-updated data is performed.

4. The method for achieving cache data consistency according to claim 3, characterized in that: The writing rules corresponding to the first writing function include: When the cache reading result indicates that the to-be-updated data exists in the cache, the step of updating the to-be-updated data by using the target update data is not performed; When the cache read result indicates that the read value of the to-be-updated data read from the cache is a null value, the to-be-updated data whose read value is a null value is updated by using the target update data.

5. The method for achieving cache data consistency according to any one of claims 2 to 4, characterized in that: Before the step of writing the data to be updated into the master database, the cache and the slave database, the method includes: Acquire a competing lock according to the system identifier; Locking the first write operation and the second write operation according to the competing lock; The first write operation is an operation of writing the data to be updated into the primary database, and the second write operation is an operation of writing the data to be updated into the cache.

6. The method for achieving cache data consistency according to any one of claims 2 to 4, characterized in that: The step of writing the to-be-updated data into the slave database comprises: Obtaining a binary log for writing the data to be updated into the primary database; By copying the binary log, the data to be updated is written into the slave database.

7. A device for achieving cache data consistency, characterized in that: include: An acquisition module is used to acquire the data to be updated and write the data to be updated into a master database, a cache and a slave database; A reading module, used for receiving a reading request for reading the data to be updated, and performing a reading process on the slave database according to the reading request to obtain a database reading result of reading the data to be updated; A conversion module, used for converting the data corresponding to the database reading result to obtain target update data; A consistency implementation module is used to write the target update data into the cache using a first write function, so as to achieve consistency between the data to be updated in the cache and the data to be updated in the main database.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the method for achieving cache data consistency as described in any one of claims 1 to 6 is implemented.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method for achieving cache data consistency as claimed in any one of claims 1 to 6 is implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method for achieving cache data consistency as claimed in any one of claims 1 to 6 is implemented.