Detailed Description
In order to make the technical solutions of the present invention better understood, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
It should be noted that the terms "first," "second," and the like in the description and claims of the present invention and in the drawings described above are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used is interchangeable under appropriate circumstances such that the embodiments of the invention described herein are capable of operation in sequences other than those illustrated or described herein. Furthermore, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
The method provided by the embodiment of the application can be executed in a computer terminal, a computer terminal or a similar operation device. Taking the example of running on a computer terminal, fig. 1 is a hardware structure block diagram of a computer terminal of an updating method of cache data according to an embodiment of the present invention. As shown in fig. 1, the computer terminal may include one or more (only one shown in fig. 1) processors 102 (the processors 102 may include, but are not limited to, a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data, and in an exemplary embodiment, may also include a transmission device 106 for communication functions and an input-output device 108. It will be understood by those skilled in the art that the structure shown in fig. 1 is only an illustration and is not intended to limit the structure of the computer terminal. For example, the computer terminal may also include more or fewer components than shown in FIG. 1, or have a different configuration with equivalent functionality to that shown in FIG. 1 or with more functionality than that shown in FIG. 1.
The memory 104 may be used to store computer programs, for example, software programs and modules of application software, such as computer programs corresponding to the method for updating the cache data in the embodiment of the present invention, and the processor 102 executes various functional applications and data processing by running the computer programs stored in the memory 104, so as to implement the above-mentioned method. The memory 104 may include high speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include memory located remotely from the processor 102, which may be connected to a computer terminal over a network. Examples of such networks include, but are not limited to, the internet, intranets, local area networks, mobile communication networks, and combinations thereof.
The transmission device 106 is used for receiving or transmitting data via a network. Specific examples of the network described above may include a wireless network provided by a communication provider of the computer terminal. In one example, the transmission device 106 includes a Network adapter (NIC), which can be connected to other Network devices through a base station so as to communicate with the internet. In one example, the transmission device 106 may be a Radio Frequency (RF) module, which is used for communicating with the internet in a wireless manner.
In this embodiment, a method for updating cache data is provided, which is applied to the computer terminal described above, and fig. 2 is a flowchart of a method for updating cache data according to an embodiment of the present invention, where the flowchart includes the following steps:
step S202, obtaining an operation log corresponding to a first database, wherein the operation log is used for indicating updated first cache data and a first identifier corresponding to the first cache data;
step S204, determining a first identifier corresponding to the first cache data according to the operation log, and determining whether a second identifier consistent with the first identifier exists in a second database;
step S206, if it is determined that a second identifier consistent with the first identifier exists in the second database, updating the first cache data with second cache data corresponding to the second identifier in the second database.
Through the steps, an operation log corresponding to the first database is obtained, wherein the operation log is used for indicating the updated first cache data and a first identifier corresponding to the first cache data; determining a first identifier corresponding to the first cache data according to the operation log, and determining whether second cache data corresponding to the first identifier exists in a second database; updating the first cache data with second cache data corresponding to the second identifier in the second database in case of determining that the second identifier consistent with the first identifier exists in the second database, by acquiring an operation log corresponding to the first database after the cache data is updated, acquiring a first identifier corresponding to the first cache data through the operation log, and updating the first cache data with second cache data corresponding to a second identifier in the second database, which is consistent with the first identifier, in the process of updating the data of the second database according to the data of the first database after the first database successfully updates the data, if improper operation can cause the problem that the data of the first database is inconsistent with the data of the second database, and the like, the problem that the data of the first database is inconsistent with the data of the second database can be avoided by adopting a subscription operation log mode.
In one exemplary embodiment, the operation log includes: binlog log, the first database comprising: the MYSQL database is a binary file, and the Binlog is called as binary log, and mainly records the operation log of the MYSQL database in the DML data operation language process.
In an exemplary embodiment, in a case that the first database receives an operation of updating third cache data, the first database is instructed to generate the operation log, wherein the third cache data is data of the first cache data before the update operation is performed; and acquiring the operation log under the condition that the first database successfully generates the operation log.
That is to say, when the target object updates the third cached data in the first database to the first cached data, the first database generates an operation log according to an operation of the target object to update the third cached data in the first database, where the operation log is used to indicate the first cached data and a first identifier corresponding to the first cached data; under the condition that the target object is determined to successfully update the third cache data in the first database into the first cache data, obtaining an operation log; and in the case that the target object is determined to successfully update the third cache data in the first database into the first cache data, the operation log is prohibited from being acquired.
In an exemplary embodiment, after determining that a second identifier consistent with the first identifier exists in the second database, updating second cache data corresponding to the second identifier in the second database with the first cache data, determining whether the second database successfully updates the second cache data to the first cache data; and under the condition that the second database does not successfully update the second cache data into the first cache data, saving consumption information to a message queue corresponding to the second database, wherein the consumption information is used for indicating that the second cache data in the second database is updated into the first cache data.
In other words, after the first cache data is updated by the second cache data corresponding to the second identifier in the second database, determining whether the second database successfully updates the first cache data by the second cache data, and returning an update success identifier to the moving display interface when it is determined that the second database successfully updates the first cache data by the second cache data; and under the condition that the second database is determined not to successfully update the first cache data with the second cache data, generating consumption information, and placing the consumption information in a message list, wherein the message list can send the consumption information to the second database at regular time, namely, compensation can be achieved after the second cache data in the second database fails to be updated.
In an exemplary embodiment, in the case that the second database does not successfully update the second cache data to the first cache data, after saving the consumption information to a message queue, instructing the message queue to send the consumption message to the second database in a preset manner; and under the condition of receiving the consumption message sent by the message queue, updating the second cache data in the second database into the first cache data according to the consumption message.
Specifically, the message list may send consumption information to the second database at regular time, the message queue sends consumption information to the second database according to a preset time interval, and the second cache data in the second database is updated to the first cache data when the consumption information is received.
Further, under a preset condition, the message queue stops sending the consumption information, wherein the preset condition at least comprises one of the following conditions: determining that second cache data in the second database is successfully updated to the first cache data; reaching the preset sending times.
In an exemplary embodiment, after determining whether the second database updates the second cached data to the first cached data, in case that it is determined that the second database successfully updates the second cached data to the first cached data, generating an update success identifier, wherein the update success identifier is used for indicating that the second database successfully updates the second cached data to the first cached data; and sending the update success identification to a page corresponding to the first database.
That is to say, under the condition that it is determined that the second database successfully updates the second cache data to the first cache data, an update success identifier is generated, and the update success identifier is sent to a page corresponding to the first database for display, so that the target object determines that the second database successfully updates the second cache data to the first cache data according to the update success identifier.
In an exemplary embodiment, determining, according to the operation log, a first identifier corresponding to the first cached data, and determining whether a second identifier consistent with the first identifier exists in a second database includes: and under the condition that it is determined that a second identifier which is consistent with the first identifier does not exist in the second database, deleting the operation log, and sending prompt information to a page corresponding to the first database, wherein the prompt information is used for indicating that the second identifier which is consistent with the first identifier does not exist in the second database.
In other words, when the third cached data in the first database is updated to the first cached data, it may be determined whether a second identifier consistent with the first identifier exists in the second database, or it may be determined whether a second cached data consistent with the third cached data exists in the second database, and when the second cached data consistent with the third cached data does not exist in the second database, the second database does not perform the update operation, deletes the operation log, and sends the prompt information to the page corresponding to the first database, so that the target object determines that the second identifier consistent with the first identifier does not exist in the second database according to the prompt information, and the second database may not perform the update operation.
In order to better understand the process of the above-mentioned cache data updating method, the following describes a flow of the above-mentioned cache data updating implementation method with reference to an optional embodiment, but the technical solution of the embodiment of the present invention is not limited thereto.
In this embodiment, a method for updating cache data is provided, and fig. 3 is a timing diagram of a method for updating cache data according to an embodiment of the present invention, as shown in fig. 3, the following steps are specifically provided:
step S301: target object login H5 page (equivalent to the page in the above embodiment);
step S302: modifying first data (corresponding to third cache data in the above embodiment) in a MYSQL database (corresponding to the first database in the above embodiment) on the H5 page, and sending modified second data (corresponding to the first cache data in the above embodiment) to a server;
step S303: updating the MYSQL database according to the modified second data;
step S304: after the MYSQL database is updated, a corresponding binlog log (equivalent to the operation log in the embodiment) is recorded;
step S305: sending the binlog log to a messaging service;
step S306: the message service updates third data (equivalent to the second cache data in the above embodiment) in redis (equivalent to the second database in the above embodiment) according to the binlog log;
step S307: the redis returns a message of successful update to the message service;
step S308: the message service returns a message of successful update to the server;
step S309: returning an updating success identifier to the server;
step S310: and displaying the page with the successful update to the target object.
Fig. 4 is a schematic diagram of an updating method of cache data according to an embodiment of the present invention, as shown in fig. 4, the following details are provided:
step S401: the business service updates the MYSQL database;
step S402: the MYSQL database records binlog logs;
step S403: the message service subscribes to the binlog log;
step S404: the first service extracts first data and a first identifier of the first data from a binlog log;
step S405: the second service sends an update indication to the redis, wherein the update indication is used for indicating that second data corresponding to a second identifier consistent with the first identifier in the redis is updated to be first data, and the update is finished under the condition that the redis is updated successfully; in the case where the update of the redis is not successful, step S406 is performed;
step S406: sending a consumption message to a RockettMQ (corresponding to a message queue in the above embodiment), wherein the consumption message is used for indicating that second data corresponding to a second identifier in the redis, which is consistent with the first identifier, is updated to the first data;
it should be noted that this scheme is to compensate for the failure of the redis cache update by the retry mechanism of the rockmq.
Step S407: sending consumption information to the second service;
step S408: and sending consumption information to the redis to instruct the redis to renew the second data into the first data.
By the embodiment, the operation log corresponding to the MYSQL database is obtained, wherein the operation log is used for indicating the updated first data and the first identifier corresponding to the first data; determining a first identifier corresponding to the first data according to the operation log, and determining whether second data corresponding to the first identifier exists in Redis; under the condition that a second identifier consistent with the first identifier exists in Redis, updating first data of second data corresponding to the second identifier in Redis, namely, acquiring an operation log corresponding to a MYSQL database after cache data is updated, acquiring a first identifier corresponding to the first data through the operation log, and updating the first data of second data corresponding to the second identifier consistent with the first identifier in Redis.
Through the embodiment, a power consumption request sent by a terminal is received, wherein the power consumption request is used for acquiring the total power consumption of target equipment in a preset time period; responding to the power consumption request, and acquiring first total power consumption of the target equipment in a first state and second total power consumption of the target equipment in a second state; and taking the sum of the first total electricity consumption and the second total electricity consumption as the total electricity consumption, namely determining the total electricity consumption according to the first total electricity consumption of the target equipment in the first state and the second total electricity consumption of the target equipment in the second state. By adopting the technical scheme, the problems that in the related technology, the cost of the hardware module is higher, the whole cost of the intelligent equipment can be increased by the intelligent equipment through increasing the power consumption of the hardware module sensing and calculating the intelligent equipment, and the like are solved, and the whole cost of the intelligent equipment is reduced.
Through the above description of the embodiments, those skilled in the art can clearly understand that the method according to the above embodiments can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware, but the former is a better implementation mode in many cases. Based on such understanding, the technical solutions of the present invention may be embodied in the form of a software product, which is stored in a storage medium (such as ROM/RAM, magnetic disk, optical disk) and includes instructions for enabling a terminal device (such as a mobile phone, a computer, a server, or a network device) to execute the method according to the embodiments of the present invention.
Fig. 5 is a block diagram of a structure of an updating apparatus for cache data according to an embodiment of the present invention; as shown in fig. 5, includes:
an obtaining module 52, configured to obtain an operation log corresponding to a first database, where the operation log is used to indicate the updated first cache data and a first identifier corresponding to the first cache data;
a determining module 54, configured to determine, according to the operation log, a first identifier corresponding to the first cache data, and determine whether a second identifier consistent with the first identifier exists in a second database;
an updating module 56, configured to update the first cache data with second cache data in the second database corresponding to the second identifier if it is determined that a second identifier that is consistent with the first identifier exists in the second database.
By the device, an operation log corresponding to the first database is obtained, wherein the operation log is used for indicating the updated first cache data and a first identifier corresponding to the first cache data; determining a first identifier corresponding to the first cache data according to the operation log, and determining whether second cache data corresponding to the first identifier exists in a second database; updating the first cache data with second cache data corresponding to the second identifier in the second database in case of determining that the second identifier consistent with the first identifier exists in the second database, by acquiring an operation log corresponding to the first database after the cache data is updated, acquiring a first identifier corresponding to the first cache data through the operation log, and updating the first cache data with second cache data corresponding to a second identifier in the second database, which is consistent with the first identifier, in the process of updating the data of the second database according to the data of the first database after the first database successfully updates the data, if improper operation can cause the problem that the data of the first database is inconsistent with the data of the second database, and the like, the problem that the data of the first database is inconsistent with the data of the second database can be avoided by adopting a subscription operation log mode.
In one exemplary embodiment, the operation log includes: binlog log, the first database comprising: the MYSQL database is a binary file, and the Binlog is called as binary log, and mainly records the operation log of the MYSQL database in the DML data operation language process.
In an exemplary embodiment, the determining module is further configured to instruct the first database to generate the operation log when the first database receives an operation of updating third cached data, where the third cached data is data of the first cached data before the updating operation is performed; and acquiring the operation log under the condition that the first database successfully generates the operation log.
That is to say, when the target object updates the third cached data in the first database to the first cached data, the first database generates an operation log according to an operation of the target object to update the third cached data in the first database, where the operation log is used to indicate the first cached data and a first identifier corresponding to the first cached data; under the condition that the target object is determined to successfully update the third cache data in the first database into the first cache data, obtaining an operation log; and in the case that the target object is determined to successfully update the third cache data in the first database into the first cache data, the operation log is prohibited from being acquired.
In an exemplary embodiment, after determining that there is a second identifier in the second database that is consistent with the first identifier, updating the first cached data with second cached data in the second database that corresponds to the second identifier, the determining module is further configured to determine whether the second database successfully updates the second cached data with the first cached data; and under the condition that the second database does not successfully update the second cache data into the first cache data, saving consumption information to a message queue corresponding to the second database, wherein the consumption information is used for indicating that the second cache data in the second database is updated into the first cache data.
In other words, after the first cache data is updated by the second cache data corresponding to the second identifier in the second database, determining whether the second database successfully updates the first cache data by the second cache data, and returning an update success identifier to the moving display interface when it is determined that the second database successfully updates the first cache data by the second cache data; and under the condition that the second database is determined not to successfully update the first cache data with the second cache data, generating consumption information, and placing the consumption information in a message list, wherein the message list can send the consumption information to the second database at regular time, namely, compensation can be achieved after the second cache data in the second database fails to be updated.
In an exemplary embodiment, after saving the consumption information to the message queue in a case that the second database does not successfully update the second cache data to the first cache data, the apparatus further includes: the receiving module is used for indicating the message queue to send the consumption message to the second database according to a preset mode; and under the condition of receiving the consumption message sent by the message queue, updating the second cache data in the second database into the first cache data according to the consumption message.
Specifically, the message list may send consumption information to the second database at regular time, the message queue sends consumption information to the second database according to a preset time interval, and the second cache data in the second database is updated to the first cache data when the consumption information is received.
Further, under a preset condition, the message queue stops sending the consumption information, wherein the preset condition at least comprises one of the following conditions: determining that second cache data in the second database is successfully updated to the first cache data; reaching the preset sending times.
In an exemplary embodiment, the determining module is further configured to generate an update success identifier if it is determined that the second database successfully updates the second cached data to the first cached data, where the update success identifier is used to indicate that the second database successfully updates the second cached data to the first cached data; and sending the update success identification to a page corresponding to the first database.
That is to say, under the condition that it is determined that the second database successfully updates the second cache data to the first cache data, an update success identifier is generated, and the update success identifier is sent to a page corresponding to the first database for display, so that the target object determines that the second database successfully updates the second cache data to the first cache data according to the update success identifier.
In an exemplary embodiment, the determining module is further configured to, in a case that it is determined that a second identifier that is consistent with the first identifier does not exist in the second database, delete the operation log, and send prompt information to a page corresponding to the first database, where the prompt information is used to indicate that a second identifier that is consistent with the first identifier does not exist in the second database.
In other words, when the third cached data in the first database is updated to the first cached data, it may be determined whether a second identifier consistent with the first identifier exists in the second database, or it may be determined whether a second cached data consistent with the third cached data exists in the second database, and when the second cached data consistent with the third cached data does not exist in the second database, the second database does not perform the update operation, deletes the operation log, and sends the prompt information to the page corresponding to the first database, so that the target object determines that the second identifier consistent with the first identifier does not exist in the second database according to the prompt information, and the second database may not perform the update operation.
An embodiment of the present invention further provides a storage medium including a stored program, wherein the program executes any one of the methods described above.
Alternatively, in the present embodiment, the storage medium may be configured to store program codes for performing the following steps:
s1, obtaining an operation log corresponding to the first database, wherein the operation log is used for indicating the updated first cache data and a first identifier corresponding to the first cache data;
s2, determining a first identifier corresponding to the first cache data according to the operation log, and determining whether a second identifier consistent with the first identifier exists in a second database;
s3, if it is determined that the second database has a second identifier that is consistent with the first identifier, updating the first cache data with second cache data in the second database that corresponds to the second identifier.
Embodiments of the present invention also provide an electronic device comprising a memory having a computer program stored therein and a processor arranged to run the computer program to perform the steps of any of the above method embodiments.
Optionally, the electronic apparatus may further include a transmission device and an input/output device, wherein the transmission device is connected to the processor, and the input/output device is connected to the processor.
Optionally, in this embodiment, the processor may be configured to execute the following steps by a computer program:
s1, obtaining an operation log corresponding to the first database, wherein the operation log is used for indicating the updated first cache data and a first identifier corresponding to the first cache data;
s2, determining a first identifier corresponding to the first cache data according to the operation log, and determining whether a second identifier consistent with the first identifier exists in a second database;
s3, if it is determined that the second database has a second identifier that is consistent with the first identifier, updating the first cache data with second cache data in the second database that corresponds to the second identifier.
Optionally, in this embodiment, the storage medium may include, but is not limited to: various media capable of storing program codes, such as a usb disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a removable hard disk, a magnetic disk, or an optical disk.
Optionally, the specific examples in this embodiment may refer to the examples described in the above embodiments and optional implementation manners, and this embodiment is not described herein again.
It will be apparent to those skilled in the art that the modules or steps of the present invention described above may be implemented by a general purpose computing device, they may be centralized on a single computing device or distributed across a network of multiple computing devices, and alternatively, they may be implemented by program code executable by a computing device, such that they may be stored in a storage device and executed by a computing device, and in some cases, the steps shown or described may be performed in an order different than that described herein, or they may be separately fabricated into individual integrated circuit modules, or multiple ones of them may be fabricated into a single integrated circuit module. Thus, the present invention is not limited to any specific combination of hardware and software.
The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention, and various modifications and changes may be made by those skilled in the art. Any modification, equivalent replacement, or improvement made within the principle of the present invention should be included in the protection scope of the present invention.