CN117112583A - Method for realizing database and cache data synchronization and method for inquiring data - Google Patents

Method for realizing database and cache data synchronization and method for inquiring data Download PDF

Info

Publication number
CN117112583A
CN117112583A CN202311075870.0A CN202311075870A CN117112583A CN 117112583 A CN117112583 A CN 117112583A CN 202311075870 A CN202311075870 A CN 202311075870A CN 117112583 A CN117112583 A CN 117112583A
Authority
CN
China
Prior art keywords
data
identifier
identification
cache
database
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202311075870.0A
Other languages
Chinese (zh)
Inventor
马强
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Bank of China Ltd
Original Assignee
Bank of China Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Bank of China Ltd filed Critical Bank of China Ltd
Priority to CN202311075870.0A priority Critical patent/CN117112583A/en
Publication of CN117112583A publication Critical patent/CN117112583A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • G06F16/2365Ensuring data consistency and integrity
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2453Query optimisation
    • G06F16/24534Query rewriting; Transformation
    • G06F16/24539Query rewriting; Transformation using cached or materialised query results
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2455Query execution
    • G06F16/24552Database cache management
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Databases & Information Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Computational Linguistics (AREA)
  • Computer Security & Cryptography (AREA)
  • Computing Systems (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The application discloses a method for realizing database and cache data synchronization and a method for inquiring data, which can be applied to the financial field or other fields and comprises the following steps: and when the first data is written in the cache, inquiring whether the second data exists in the cache according to the identification of the first data. If so, adding a first writing identification to the second data, and acquiring third data in the database according to the identification. The second data and the third data are then updated according to the first data. And deleting the first writing identification after the updating is successful. If the first data does not exist, the first data is written into the database to serve as fourth data, the first data is written into the cache to serve as fifth data, and the fifth data comprises a second writing identification. And deleting the second writing identification in response to the fourth data and the fifth data being written successfully. Through the operation of adding the writing identification to the data in the cache, after the data in the cache and the database are synchronously updated, the writing identification is deleted, so that the consistency of the data in the cache and the database is ensured.

Description

Method for realizing database and cache data synchronization and method for inquiring data
Technical Field
The application relates to the technical field of computers, in particular to a method for realizing database and cache data synchronization and a method for inquiring data.
Background
Banks have huge flows of financial transactions, and often have the scene of concurrent checking (data inquiry) and entering and exiting (data addition/modification). In order to relieve the pressure of the database processing data, the most commonly used approach is to use some caching middleware, such as remote dictionary services (Remote Dictionary Server, dis). However, due to the fact that there is no consistency protocol between the database and the cache, a scene that the data is inconsistent frequently occurs in the database and the cache. For example, the data in the cache is successfully updated but the data in the database is not synchronously updated, or the data in the database is forgotten to be updated, etc.
Disclosure of Invention
In view of this, the present application provides a method for implementing database and cache data synchronization so as to ensure the consistency of the data in the database and the cache.
In a first aspect, the present application provides a method for implementing database and cache data synchronization, the method comprising:
responding to a request for writing first data, and inquiring whether second data exists in a cache according to the identification of the first data, wherein the second data and the first data have the same identification;
If so, adding a first writing identification to the second data; acquiring third data in a database according to the identification of the first data, and updating the second data and the third data based on the first data, wherein the third data and the first data have the same identification;
deleting the first writing identification in response to successful updating of the second data and the third data;
if not, writing the first data into the database as fourth data, and writing the first data into the cache as fifth data, wherein the fifth data comprises a second writing identification;
and deleting the second writing identification in response to the successful writing operation of the fourth data and the fifth data.
In one possible implementation, the updating the second data based on the first data includes:
and updating the second data according to the updated third data after updating the third data based on the first data.
In one possible implementation manner, the first data is in the form of a key value pair, the identifier of the first data is a key, and the true value of the first data is a value.
In one possible implementation manner, the writing the first data into the cache as fifth data, where the fifth data includes a second writing identifier, includes:
creating a key value pair in the cache, wherein a key word in the key value pair is an identification of the first data, and a value in the key value pair is null;
adding the second writing identification to the value in the key value pair;
and after the first data is written into the database as the fourth data, updating the value in the key value pair according to the fourth data.
In one possible implementation, the method further includes:
when a request for writing a plurality of data is received in a preset time period, taking the latest request for writing data as the request for writing first data, wherein the identification of the plurality of data is the same as that of the first data.
In one possible implementation, the first written identity is a universally unique identification code (Universally Unique Identifier, UUID).
In a second aspect, the present application provides a method for implementing database and cache data synchronization, the method comprising:
responding to a request for reading target data, and inquiring in a cache according to the identification of the target data;
When the identification of the target data is not queried in the cache, creating a data item in the cache and adding a reading identification for the data item, wherein the identification of the data item is the identification of the target data, and the true value of the data item is null;
inquiring in a database according to the identification of the target data, and returning the data corresponding to the inquired identification of the target data;
updating the true value of the data item according to the data corresponding to the identification of the target data in the database;
and deleting the reading identification in response to successful updating of the true value of the data item.
In a third aspect, the present application provides a method of querying data, the method comprising:
responding to a request for reading target data, and inquiring in a cache according to the identification of the target data;
when the identifier is not queried in the cache, querying in a database according to the identifier, and returning data corresponding to the queried identifier;
when the identifier is queried in the cache, judging whether the data corresponding to the identifier comprises a read identifier or a write identifier, and if the read identifier or the write identifier does not exist, returning the queried data corresponding to the identifier; if the read identifier or the write identifier exists, waiting for deleting the read identifier or the write identifier, and acquiring data corresponding to the identifier again;
Wherein the written identity is obtained according to the method according to any one of the implementation forms of the first aspect and the read identity is obtained according to the method according to the second aspect.
In one possible implementation, when the identifier is not queried in the cache, the method further includes:
creating a data item in the cache, and adding a reading identifier for the data item, wherein the identifier of the data item is the identifier of the target data, and the true value of the data item is null;
and updating the true value of the data item according to the data corresponding to the identification of the target data in the database, and deleting the reading identification.
In a fourth aspect, the present application provides an apparatus for implementing database and cache data synchronization, the apparatus comprising:
the first query unit is used for responding to a request for writing first data, and querying whether second data exist in a cache according to the identification of the first data, wherein the second data have the same identification with the first data;
a first processing unit for adding a first write identification to the second data, if any; acquiring third data in a database according to the identification of the first data, and updating the second data and the third data based on the first data, wherein the third data and the first data have the same identification;
A first deleting unit configured to delete the first writing identifier in response to an operation that the second data and the third data are updated successfully;
a second processing unit, configured to write the first data into the database as fourth data and write the first data into the cache as fifth data if the first data does not exist, where the fifth data includes a second writing identifier;
the first deleting unit is further configured to delete the second writing identifier in response to an operation that the fourth data and the fifth data are written successfully.
In a fifth aspect, the present application provides an apparatus for implementing database and cache data synchronization, the apparatus comprising:
the second query unit is used for responding to a request for reading target data and querying in a cache according to the identification of the target data;
the creating unit is used for creating a data item in the cache and adding a reading identifier for the data item when the identifier of the target data is not queried in the cache, wherein the identifier of the data item is the identifier of the target data, and the true value of the data item is null;
the second query unit is further configured to query in a database according to the identifier of the target data, and return data corresponding to the queried identifier of the target data;
The updating unit is used for updating the true value of the data item according to the data corresponding to the identification of the target data in the database;
and the second deleting unit is used for deleting the reading identification in response to the successful updating operation of the true value of the data item.
In a sixth aspect, the present application provides an apparatus for querying data, the apparatus comprising:
the third query unit is used for responding to a request for reading target data and querying in a cache according to the identification of the target data;
the third query unit is further configured to query in a database according to the identifier when the identifier is not queried in the cache, and return data corresponding to the queried identifier;
the acquisition unit is used for judging whether the data corresponding to the identifier comprises a read identifier or a write identifier when the identifier is inquired in the cache, and returning the inquired data corresponding to the identifier if the read identifier or the write identifier does not exist; and if the read identifier or the write identifier exists, waiting for deleting the read identifier or the write identifier, and acquiring the data corresponding to the identifier again.
In a seventh aspect, the present application provides an electronic device, the device comprising: a memory and a processor;
the memory is used for storing related program codes;
the processor is configured to invoke the program code to perform the method of the first aspect, the second aspect or the third aspect.
In an eighth aspect, the present application provides a computer readable storage medium for storing a computer program for executing the method according to the first, second or third aspect.
From this, the application has the following beneficial effects:
when the first data needs to be written in the cache, for example, the first data can be modified or new first data can be written, and whether second data with the same identification as the first data exists in the cache is inquired according to the identification of the first data. If so, indicating that the second data needs to be modified into the first data, adding a first writing identification to the second data, and acquiring third data in the database according to the identification of the first data, wherein the third data has the same identification as the first data, namely, the third data needs to be modified into the first data, and then updating the third data and the second data according to the first data. And deleting the first writing identification of the second data after the second data and the third data are updated successfully, wherein the first writing identification indicates that both the cache and the database are updated successfully. If the first data is not the newly written data, writing the first data into a database to serve as fourth data, and writing the first data into a cache to serve as fifth data, wherein the fifth data comprises a second writing identification. And deleting the second writing identification in response to the successful writing operation of the fourth data and the fifth data, wherein the second writing identification indicates that the first data is successfully written into the cache and the database. By adding the writing identification to the data in the cache, the writing identification can be deleted after the data in the cache and the database are synchronously updated, so that the consistency of the data in the cache and the database is ensured.
When a request for reading target data is received, firstly, inquiring is carried out in a cache according to the identification of the target data. If the identification of the target data is not queried in the cache, a data item can be created in the cache and a read identification can be added to the data item, wherein the identification of the data item is the identification of the target data, and the true value of the data item is null, which indicates that the target data does not exist in the cache at present. And then, inquiring in a database according to the identification of the target data, and returning the data corresponding to the inquired identification of the target data. When the data is queried, updating the true value of the data item according to the data. And deleting the read identification of the data item in response to successful updating of the true value of the data item. By adding the reading identifier to the data item in the cache, the data in the cache can be marked to be updated from the database, and the reading identifier can be deleted only after the cache is successfully updated, so that the consistency of the data in the database and the cache is realized.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for the description of the embodiments will be briefly described below, and it is apparent that the drawings in the following description are only some embodiments provided in the present application, and other drawings may be obtained according to these drawings for those of ordinary skill in the art.
FIG. 1 is a flowchart of a method for implementing database and cache data synchronization according to an embodiment of the present application;
FIG. 2 is a flowchart of another method for implementing database and cache data synchronization according to an embodiment of the present application;
FIG. 3 is a flowchart of a method for querying data according to an embodiment of the present application;
FIG. 4 is a schematic diagram of an apparatus for implementing database and cache data synchronization according to an embodiment of the present application;
FIG. 5 is a schematic diagram of another apparatus for implementing database and cache data synchronization according to an embodiment of the present application;
FIG. 6 is a schematic diagram of an apparatus for querying data according to an embodiment of the present application;
fig. 7 is a schematic diagram of an electronic device according to an embodiment of the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application, where the described embodiments are merely exemplary implementations, but not all implementations of the application. Those skilled in the art can combine embodiments of the application to obtain other embodiments without inventive faculty, and such embodiments are also within the scope of the application.
Banks have huge flows of financial transactions, and often have the scene of concurrent checking (data inquiry) and entering and exiting (data addition/modification). In order to relieve the pressure of the database processing data, the most commonly used approach is to use some caching middleware, such as remote dictionary services (Remote Dictionary Server, dis). However, due to the fact that there is no consistency protocol between the database and the cache, a scene that the data is inconsistent frequently occurs in the database and the cache. For example, the data in the cache is successfully updated but the data in the database is not synchronously updated, or the data in the database is forgotten to be updated, etc.
When data update is involved, the methods for realizing the consistency of the data in the database and the cache are mainly as follows:
(1) Firstly, writing the cache and then writing the database;
(2) Writing the database first and then writing the cache;
(3) Firstly writing a database, and then deleting the cache;
(4) The cache is deleted and the database is written.
For scheme (1), if the cache writing is successful and the database writing fails, data in the cache and the database are inconsistent, so that old data exists in the database. The new data in the cache will be replaced by the old data in the database at the next cache synchronization, and the data repair will be invalidated.
For scheme (2), in a high concurrency situation, the database successfully writes the latest data through synchronization of multiple threads, but the cache cannot synchronize according to the time sequence of the multiple threads due to network fluctuation, so that the latest data is updated into the cache first, and the old data is updated into the cache last, so that the data in the cache and the database are inconsistent.
For scheme (3), if the old data in the cache is deleted first, then the read request queries the cache to find that there is no data, and then directly queries the database. At this time, the operation of updating the data in the database may not be finished, and the queried old data is still queried, the old data is updated in the cache, and the update of the data is realized in the database.
For scheme (4), if the database is updated first, the database fails to be deleted, so that the data in the cache is old data, the database is the latest data, the problem of data consistency occurs, and whether the data searched during the cache is old data is queried.
In order to solve the problem that data are inconsistent between a database and a cache, the embodiment of the application provides a method for realizing the synchronization of the database and the cache data, so that the consistency of the data in the database and the cache can be ensured. In the embodiment of the present application, the data writing and reading scenarios may be divided, and the data writing scenarios are described below first.
Referring to fig. 1, fig. 1 is a flowchart of a method for implementing database and cache data synchronization according to an embodiment of the present application.
The method may be performed by a processing device that may process commands directed to a cache and a database to implement data processing operations in the cache and the database.
The method may comprise the steps of:
s101: and responding to the request for writing the first data, and inquiring whether second data exist in the cache according to the identification of the first data. If so, executing step S102; if not, the jump proceeds to step S105.
The first data may be in the form of a key value pair, the identifier of the first data may be a key of the key value pair, the true value of the first data may be a value of the key value pair, and the key is unique and unchanged.
When first data needs to be written in the cache, it can be determined whether to modify the original data or to write new data. For example, it may first be queried whether there is second data in the cache that has the same identification as the first data. If present, indicates that the actual value of the second data needs to be modified based on the actual value of the first data.
S102: the first write identification is added to the second data.
When the second data in the cache is queried, a first writing identification can be added to the second data, which indicates that the second data is the data needing to be updated. The first writing identifier and the second data are uniquely corresponding, and the embodiment of the application is not limited to a specific form of the first writing identifier. For example, a universally unique identification code (Universally Unique Identifier, UUID) may be randomly generated as the writing identification.
S103: and acquiring third data in the database according to the identification of the first data, and updating the third data and the second data based on the first data.
Since the data in the cache needs to be updated, similarly, the data in the database needs to be updated synchronously. Specifically, third data having the same identification as the first data in the database may be acquired according to the identification of the first data, and then the true value of the third data may be updated according to the true value of the first data.
When updating the second data in the cache according to the first data, the real value of the second data may be updated directly according to the real value of the first data when the first writing identification is added to the second data.
Optionally, when the first writing identifier is added to the second data, the second data may not be updated first, that is, the real value of the third data in the database may be updated first according to the real value of the first data. And after the third data is updated successfully, updating the second data in the cache according to the updated third data. That is, the true value of the second data is updated according to the updated true value of the third data.
S104: and deleting the first writing identification in response to successful updating of the second data and the third data.
And deleting the first writing identification in the second data after the second data in the cache and the third data in the database are updated successfully according to the first data, which indicates that the data in the cache and the database are updated synchronously.
In one possible implementation, when the data in the cache is subsequently read, if the second data in the cache is queried according to the identifier of the data to be read, and the second data includes the first writing identifier at the moment, that is, the second data is being updated at the moment, the request may be set to wait for a period of time, that is, the request is controlled not to read the data in the database, so as to relieve the pressure of querying the data by the database. After a period of time, if the first writing identification inquired in the cache is deleted, the second data is indicated to be updated successfully, and the data can be inquired in the cache again according to the identification of the inquired data.
By setting the writing identification, the synchronous updating of the data in the database and the cache can be realized, the consistency of the data is ensured, and the waiting time can be set when the data with the writing identification is inquired, so that the data can be prevented from being directly inquired in the database, the pressure of the database is relieved, and the cache breakdown is avoided.
S105: the first data is written into the database as fourth data and the first data is written into the cache as fifth data.
When the second data with the same identification as the first data does not exist in the cache, the first data is indicated to be the first data written into the cache and the database, and at the moment, the first data can be written into the database to be used as fourth data, and the first data can be written into the cache to be used as fifth data.
In one possible implementation, when the second data is not queried in the cache, a key pair may be established in the cache at this time for storing the first data. The key of the key value pair is the identification of the first data, and at this time, the value in the key value pair is null. A second write flag may also be added to the value of the key-value pair, indicating that the key-value pair is data being updated.
When the key-value pair is updated according to the first data, a value in the key-value pair may be updated according to the true value of the first data when the key-value pair is established. It is also possible to write the first data into the database as the fourth data without updating the value after adding the second writing identification. And after the fourth data is successfully written, updating the value in the key value pair according to the fourth data to obtain fifth data written into the cache.
S106: and deleting the second writing identification in response to the successful writing operation of the fourth data and the fifth data.
And deleting a second writing identifier in the fifth data after the fourth data and the fifth data are written successfully, which indicates that the data updating is realized by both the cache and the database, and the consistency of the data is maintained.
In one possible implementation manner, when a request for writing a plurality of data is received in a preset time period, the identifiers of the data written in the plurality of requests are the same as the identifiers of the first data, which indicates that a plurality of operations for modifying the true value of the first data are received in the preset time period. For example, a modification error may be found when modifying data for a user and then modified once more. At this time, in order to avoid resource waste caused by frequent updating of data, the latest request for writing data may be used as a request for writing the first data, that is, only the request for writing data last time in the preset period of time is processed. The preset time period is shorter and can be set according to actual requirements, which is not limited in the embodiment of the present application.
By adding the writing identification to the data in the cache, the writing identification can be deleted after the data in the cache and the database are synchronously updated, so that the consistency of the data in the cache and the database is ensured.
Based on the above method embodiments, a scenario of reading data will be described below.
Referring to fig. 2, fig. 2 is a flowchart of another method for implementing database and cache data synchronization according to an embodiment of the present application.
The method may comprise the steps of:
s201: and responding to a request for reading the target data, and inquiring in the cache according to the identification of the target data.
When it is desired to read the target data, an identification of the target data may be included in the request for querying the actual value of the target data based on the identification. Wherein the identification of the target data is uniquely corresponding, so that the true value of the target data can be queried according to the identification. When querying data, first a query is made in the cache.
S202: when the identity of the target data is not queried in the cache, then the data item is created in the cache and a read identity is added to the data item.
If the identity of the target data is not queried in the cache, it is indicated that the data is not synchronized from the database in the cache. At this time, in order to synchronize the target data to the cache, the data item may be first established in the cache for storing the data. Wherein the identity of the data item is the identity of the target data, and the true value of the data item is null at this time. And adding a reading identification for the data item, which indicates that the data corresponding to the data item is being queried from the database and updated into the cache.
S203: inquiring in the database according to the identification of the target data, and returning the data corresponding to the identification of the inquired target data.
When the identification of the target data is not queried in the cache, the database can be queried, and the data corresponding to the queried identification of the target data is returned.
S204: and updating the true value of the data item according to the data corresponding to the identification of the target data in the database.
After the data corresponding to the identification of the target data is queried, the true value of the data item in the cache can be updated according to the true value of the data, namely, the queried data is updated in the cache.
S205: and deleting the read identifier in response to successful updating of the true value of the data item.
And deleting the reading identification of the data item after the true value of the data item in the cache is updated successfully, which indicates that the data synchronization of the cache and the database is realized.
In one possible implementation, when a request for reading data is subsequently received, when the read data again queries the target data, the data item which is not updated successfully is queried from the cache, that is, the data item includes the read identifier, and the target data is being queried from the database. At this point, the read request may be set to wait for a period of time, i.e., the request is controlled not to go to the database for a further query. And waiting until the data queried from the database is updated in the cache, deleting the read identifier, and querying the updated data item in the cache again according to the read identifier of the read data.
By adding the reading identifier to the data item in the cache, the data in the cache can be marked to be updated from the database, and the reading identifier can be deleted only after the cache is successfully updated, so that the consistency of the data in the database and the cache is realized.
Based on the method embodiment, the embodiment of the application also provides a method for inquiring data.
Referring to fig. 3, fig. 3 is a flowchart of a method for querying data according to an embodiment of the present application.
The method may comprise the steps of:
s301: and responding to a request for reading the target data, and inquiring in the cache according to the identification of the target data.
S302: and when the identifier is not queried in the cache, querying in a database according to the identifier, and returning the data corresponding to the queried identifier.
S303: when the identifier is queried in the cache, judging whether the data corresponding to the identifier comprises a read identifier or a write identifier; if none of the identifiers exists, returning the data corresponding to the inquired identifiers; if the read identifier or the write identifier exists, waiting for deleting the read identifier or the write identifier, and re-acquiring the data corresponding to the identifier.
The data identifier and the writing identifier may be obtained according to the steps described in the above embodiments, and when the read identifier or the writing identifier exists in the queried data, the data in the cache is indicated to be updated. At this time, in order to relieve the data query pressure of the database, the request for querying the data can be controlled to wait for a period of time, i.e. the data is not directly queried in the database. When the data in the cache is updated, the read identifier or the write identifier is deleted, and at this time, the data corresponding to the identifier can be obtained again.
When the identity is not queried in the cache, it is indicated that the data is not synchronized from the database in the cache. At this time, in order to synchronize the target data to the cache, it is also possible to establish a data item in the cache and add a read identifier to the data item for storing the data. Wherein the identity of the data item is the identity of the target data, and the true value of the data item is null at this time. And updating the true value of the data item according to the data corresponding to the identification of the target data in the database, and deleting the read identification.
Based on the above method embodiment, the embodiment of the application provides a device for implementing database and cache data synchronization. Referring to fig. 4, fig. 4 is a schematic diagram of an apparatus for implementing database and cache data synchronization according to an embodiment of the present application.
The apparatus 400 includes:
a first querying unit 401, configured to query, in response to a request for writing first data, whether second data exists in a cache according to an identifier of the first data, where the second data has the same identifier as the first data;
a first processing unit 402, configured to add a first write identifier to the second data, if any; acquiring third data in a database according to the identification of the first data, and updating the second data and the third data based on the first data, wherein the third data and the first data have the same identification;
A first deleting unit 403, configured to delete the first writing identifier in response to an operation that the second data and the third data are updated successfully;
a second processing unit 404, configured to write the first data into the database as fourth data and write the first data into the cache as fifth data, if not present, where the fifth data includes a second write identifier;
the first deleting unit 403 is further configured to delete the second writing identifier in response to an operation that the fourth data and the fifth data are written successfully.
In a possible implementation manner, the first processing unit 402 is specifically configured to update the second data according to the updated third data after updating the third data based on the first data.
In one possible implementation manner, the first data is in the form of a key value pair, the identifier of the first data is a key, and the true value of the first data is a value.
In a possible implementation manner, the second processing unit 404 is specifically configured to create a key value pair in the cache, where a key of the key value pair is an identifier of the first data, and a value of the key value pair is null; adding the second writing identification to the value in the key value pair; and after the first data is written into the database as the fourth data, updating the value in the key value pair according to the fourth data.
In one possible implementation, the apparatus further includes: a receiving unit;
the receiving unit is used for taking the latest request for writing data as the request for writing the first data when receiving the request for writing a plurality of data in a preset time period, wherein the identification of the plurality of data is the same as the identification of the first data.
In one possible implementation, the first written identification is a universally unique identification code UUID.
Based on the method embodiment, the embodiment of the application also provides a device for realizing the synchronization of the database and the cache data. Referring to fig. 5, fig. 5 is a schematic diagram of another apparatus for implementing database and cache data synchronization according to an embodiment of the present application.
The apparatus 500 includes:
a second query unit 501, configured to respond to a request for reading target data, and perform a query in a cache according to an identifier of the target data;
a creating unit 502, configured to create a data item in the cache and add a read identifier to the data item when the identifier of the target data is not queried in the cache, where the identifier of the data item is the identifier of the target data, and the true value of the data item is null;
The second query unit 501 is further configured to query in a database according to the identifier of the target data, and return data corresponding to the queried identifier of the target data;
an updating unit 503, configured to update a true value of the data item according to data corresponding to the identifier of the target data in the database;
a second deleting unit 504, configured to delete the read identifier in response to an operation that the update of the real value of the data item is successful.
In addition, the embodiment of the application also provides a device for inquiring the data. Referring to fig. 6, fig. 6 is a schematic diagram of an apparatus for querying data according to an embodiment of the present application.
The apparatus 600 includes:
a third query unit 601, configured to respond to a request for reading target data, and perform a query in a cache according to an identifier of the target data;
the third query unit 601 is further configured to query in a database according to the identifier and return data corresponding to the queried identifier when the identifier is not queried in the cache;
an obtaining unit 602, configured to determine, when the identifier is queried in the cache, whether data corresponding to the identifier includes a read identifier or a write identifier, and if none of the data exists, return data corresponding to the queried identifier; and if the read identifier or the write identifier exists, waiting for deleting the read identifier or the write identifier, and acquiring the data corresponding to the identifier again.
Based on the method embodiment and the device embodiment, the embodiment of the application also provides electronic equipment. The following description will be made with reference to the accompanying drawings.
Referring to fig. 7, fig. 7 is a schematic diagram of an electronic device according to an embodiment of the present application.
The apparatus 700 includes: a memory 701 and a processor 702;
the memory 701 is used for storing related program codes;
the processor 702 is configured to invoke the program code to perform the method described in the method embodiments above.
Furthermore, the embodiment of the application also provides a computer readable storage medium for storing a computer program for executing the method described in the embodiment of the method.
It should be noted that the method for implementing database and cache data synchronization and the method for querying data provided by the application can be used in the financial field or other fields. Other fields are any field other than the financial field, for example, the computer technology field. The foregoing is merely an example, and is not intended to limit the application fields of the method for implementing database and cache data synchronization and the method for querying data provided by the present application.
It should be noted that, in the present description, each embodiment is described in a progressive manner, and each embodiment is mainly described in a different manner from other embodiments, and identical and similar parts between the embodiments are all enough to refer to each other. In particular, for system or apparatus embodiments, since they are substantially similar to method embodiments, the description is relatively simple, with relevant portions being referred to in the description of the method embodiments. The above-described apparatus embodiments are merely illustrative, in which units or modules illustrated as separate components may or may not be physically separate, and components shown as units or modules may or may not be physical modules, i.e. may be located in one place, or may be distributed over multiple network units, where some or all of the units or modules may be selected according to actual needs to achieve the purposes of the embodiment. Those of ordinary skill in the art will understand and implement the present application without undue burden.
It should be understood that in the present application, "at least one (item)" means one or more, and "a plurality" means two or more. "and/or" for describing the association relationship of the association object, the representation may have three relationships, for example, "a and/or B" may represent: only a, only B and both a and B are present, wherein a, B may be singular or plural. The character "/" generally indicates that the context-dependent object is an "or" relationship. "at least one of" or the like means any combination of these items, including any combination of single item(s) or plural items(s). For example, at least one (one) of a, b or c may represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", wherein a, b, c may be single or plural.
It is further noted that relational terms such as first and second, and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Moreover, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
The steps of a method or algorithm described in connection with the embodiments disclosed herein may be embodied directly in hardware, in a software module executed by a processor, or in a combination of the two. The software modules may be disposed in Random Access Memory (RAM), memory, read Only Memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
The previous description of the disclosed embodiments is provided to enable any person skilled in the art to make or use the present application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the application. Thus, the present application is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims (14)

1. A method for implementing database and cache data synchronization, the method comprising:
responding to a request for writing first data, and inquiring whether second data exists in a cache according to the identification of the first data, wherein the second data and the first data have the same identification;
If so, adding a first writing identification to the second data; acquiring third data in a database according to the identification of the first data, and updating the second data and the third data based on the first data, wherein the third data and the first data have the same identification;
deleting the first writing identification in response to successful updating of the second data and the third data;
if not, writing the first data into the database as fourth data, and writing the first data into the cache as fifth data, wherein the fifth data comprises a second writing identification;
and deleting the second writing identification in response to the successful writing operation of the fourth data and the fifth data.
2. The method of claim 1, wherein the updating the second data based on the first data comprises:
and updating the second data according to the updated third data after updating the third data based on the first data.
3. The method of claim 1, wherein the first data is in the form of key-value pairs, the identification of the first data is a key, and the true value of the first data is a value.
4. A method according to claim 3, wherein said writing said first data to said cache as fifth data, said fifth data comprising a second write identification, comprises:
creating a key value pair in the cache, wherein a key word in the key value pair is an identification of the first data, and a value in the key value pair is null;
adding the second writing identification to the value in the key value pair;
and after the first data is written into the database as the fourth data, updating the value in the key value pair according to the fourth data.
5. The method according to claim 1, wherein the method further comprises:
when a request for writing a plurality of data is received in a preset time period, taking the latest request for writing data as the request for writing first data, wherein the identification of the plurality of data is the same as that of the first data.
6. The method of any one of claims 1 to 5, wherein the first written identification is a universally unique identification code UUID.
7. A method for implementing database and cache data synchronization, the method comprising:
Responding to a request for reading target data, and inquiring in a cache according to the identification of the target data;
when the identification of the target data is not queried in the cache, creating a data item in the cache and adding a reading identification for the data item, wherein the identification of the data item is the identification of the target data, and the true value of the data item is null;
inquiring in a database according to the identification of the target data, and returning the data corresponding to the inquired identification of the target data;
updating the true value of the data item according to the data corresponding to the identification of the target data in the database;
and deleting the reading identification in response to successful updating of the true value of the data item.
8. A method of querying data, the method comprising:
responding to a request for reading target data, and inquiring in a cache according to the identification of the target data;
when the identifier is not queried in the cache, querying in a database according to the identifier, and returning data corresponding to the queried identifier;
when the identifier is queried in the cache, judging whether the data corresponding to the identifier comprises a read identifier or a write identifier, and if the read identifier or the write identifier does not exist, returning the queried data corresponding to the identifier; if the read identifier or the write identifier exists, waiting for deleting the read identifier or the write identifier, and acquiring data corresponding to the identifier again;
Wherein the written identity is obtained according to the method of any one of claims 1 to 6 and the read identity is obtained according to the method of claim 7.
9. The method of claim 8, wherein when the identity is not queried in the cache, the method further comprises:
creating a data item in the cache, and adding a reading identifier for the data item, wherein the identifier of the data item is the identifier of the target data, and the true value of the data item is null;
and updating the true value of the data item according to the data corresponding to the identification of the target data in the database, and deleting the reading identification.
10. An apparatus for implementing database and cache data synchronization, the apparatus comprising:
the first query unit is used for responding to a request for writing first data, and querying whether second data exist in a cache according to the identification of the first data, wherein the second data have the same identification with the first data;
a first processing unit for adding a first write identification to the second data, if any; acquiring third data in a database according to the identification of the first data, and updating the second data and the third data based on the first data, wherein the third data and the first data have the same identification;
A first deleting unit configured to delete the first writing identifier in response to an operation that the second data and the third data are updated successfully;
a second processing unit, configured to write the first data into the database as fourth data and write the first data into the cache as fifth data if the first data does not exist, where the fifth data includes a second writing identifier;
the first deleting unit is further configured to delete the second writing identifier in response to an operation that the fourth data and the fifth data are written successfully.
11. An apparatus for implementing database and cache data synchronization, the apparatus comprising:
the second query unit is used for responding to a request for reading target data and querying in a cache according to the identification of the target data;
the creating unit is used for creating a data item in the cache and adding a reading identifier for the data item when the identifier of the target data is not queried in the cache, wherein the identifier of the data item is the identifier of the target data, and the true value of the data item is null;
the second query unit is further configured to query in a database according to the identifier of the target data, and return data corresponding to the queried identifier of the target data;
The updating unit is used for updating the true value of the data item according to the data corresponding to the identification of the target data in the database;
and the second deleting unit is used for deleting the reading identification in response to the successful updating operation of the true value of the data item.
12. An apparatus for querying data, the apparatus comprising:
the third query unit is used for responding to a request for reading target data and querying in a cache according to the identification of the target data;
the third query unit is further configured to query in a database according to the identifier when the identifier is not queried in the cache, and return data corresponding to the queried identifier;
the acquisition unit is used for judging whether the data corresponding to the identifier comprises a read identifier or a write identifier when the identifier is inquired in the cache, and returning the inquired data corresponding to the identifier if the read identifier or the write identifier does not exist; and if the read identifier or the write identifier exists, waiting for deleting the read identifier or the write identifier, and acquiring the data corresponding to the identifier again.
13. An electronic device, the device comprising: a memory and a processor;
The memory is used for storing related program codes;
the processor is configured to invoke the program code to perform the method of any of claims 1 to 6, or claim 7, or claim 8 or 9.
14. A computer readable storage medium for storing a computer program for performing the method of any one of claims 1 to 6, or claim 7, or claim 8 or 9.
CN202311075870.0A 2023-08-24 2023-08-24 Method for realizing database and cache data synchronization and method for inquiring data Pending CN117112583A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311075870.0A CN117112583A (en) 2023-08-24 2023-08-24 Method for realizing database and cache data synchronization and method for inquiring data

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311075870.0A CN117112583A (en) 2023-08-24 2023-08-24 Method for realizing database and cache data synchronization and method for inquiring data

Publications (1)

Publication Number Publication Date
CN117112583A true CN117112583A (en) 2023-11-24

Family

ID=88812300

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311075870.0A Pending CN117112583A (en) 2023-08-24 2023-08-24 Method for realizing database and cache data synchronization and method for inquiring data

Country Status (1)

Country Link
CN (1) CN117112583A (en)

Similar Documents

Publication Publication Date Title
CN107273455B (en) Block chain data access method and device
CN109684307B (en) Data storage method, device, equipment and storage medium
CN105630863B (en) Transaction control block for multi-version concurrent commit status
US8924365B2 (en) System and method for range search over distributive storage systems
EP3117348B1 (en) Systems and methods to optimize multi-version support in indexes
US8713046B2 (en) Snapshot isolation support for distributed query processing in a shared disk database cluster
US8990177B2 (en) Lock-free transactional support for large-scale storage systems
CN112363979B (en) Distributed index method and system based on graph database
US9990391B1 (en) Transactional messages in journal-based storage systems
US11392567B2 (en) Just-in-time multi-indexed tables in a shared log
JP2005532615A (en) Providing usable versions of data items
CN107193827B (en) Idempotent control method and device for sub-base and sub-table
US10108658B1 (en) Deferred assignments in journal-based storage systems
CN111475519B (en) Data caching method and device
US11269829B2 (en) Row level locking for columnar data
US11151081B1 (en) Data tiering service with cold tier indexing
US20150074070A1 (en) System and method for reconciling transactional and non-transactional operations in key-value stores
US8380663B2 (en) Data integrity in a database environment through background synchronization
US20150134900A1 (en) Cache efficiency in a shared disk database cluster
CN113220669A (en) Service data processing method and device and electronic equipment
US20200034472A1 (en) Asynchronous cache coherency for mvcc based database systems
WO2021237190A1 (en) Distributed transaction execution in distributed databases
CN107025257B (en) Transaction processing method and device
CN109542872B (en) Data reading method and device, computer equipment and storage medium
CN111639087A (en) Data updating method and device in database and electronic equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination