A method and apparatus for synchronizing data
By storing the changed business data in the cache in the sliding window and synchronizing it to the database, the problem of database resource waste and data loss is solved, efficient and flexible data synchronization and switching is achieved, and costs are reduced.
Patent Information
- Application Number
- CN201811442486.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2018-11-29
- Publication Date
- 2024-09-20
- Estimated Expiration
- 2038-11-29
AI Technical Summary
In the existing technology, there are problems such as high waste of database resources and high cost, and when using redis to cache data snapshots, if an exception occurs, data may be lost.
The changed business data in the cache is stored in the current sliding window, and a new sliding window is created to synchronize the business data in the original sliding window to the database, realizing flexible switching between the data cache and the database, and reducing This reduces the amount of data synchronized each time and improves application performance.
It overcomes the problems of high usage cost and data loss, improves application performance, realizes flexible data switching, and ensures data security and efficient synchronization.
Smart Images

Figure CN111241189B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for synchronizing data. Background Art
[0002] Currently, it is often necessary to monitor and count a large amount of real-time data and generate real-time reports, such as real-time statistics of the number of shipments from a sorting center in a certain period of time. If the relevant records in the database are updated every time an order is issued, the sorting center operates very frequently. In order to count a large number of indicators, the relevant database tables will be frequently updated, which will put a lot of pressure on the database, resulting in problems such as excessive CPU usage and large master-slave delay.
[0003] There are currently two solutions to the above problems: first, divide the database into different databases, that is, use multiple databases to store data to reduce the pressure on a single database; second, store database snapshots in redis. Each time the data is updated, directly update the data snapshot in redis, and then synchronize the data snapshot stored in redis to the database through a scheduled task.
[0004] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:
[0005] 1) Using sharding for data storage will waste a lot of database resources and make the cost too high. In addition, some real-time reports have a small amount of data, but the data is updated very frequently, so using sharding is not suitable.
[0006] 2) Using redis to cache data snapshots can reduce the use of database resources and reduce costs, but this method is not flexible. If redis or scheduled tasks are abnormal, they cannot quickly switch to the database, resulting in data loss. Summary of the invention
[0007] In view of this, an embodiment of the present invention provides a method and device for synchronizing data, which can solve the problems of high use cost and data loss.
[0008] To achieve the above object, according to one aspect of an embodiment of the present invention, a method for synchronizing data is provided, comprising:
[0009] Store the business data to be updated in the cache;
[0010] Storing the changed business data in the cache into the current sliding window;
[0011] Create a new sliding window, use it as the current sliding window, and synchronize the business data in the original sliding window to the database.
[0012] Optionally, storing the business data to be updated in the cache includes:
[0013] Get the identifier of the current sliding window pointed to by the pointer, assemble the business primary key of the business data and the identifier of the sliding window into a string, use the string as the key, and use the attributes and attribute values of the business data as the value, and store them in the cache.
[0014] Optionally, storing the changed business data in the cache into the current sliding window includes:
[0015] The key corresponding to the business data whose attribute value has changed and is stored in the cache is stored in the current sliding window pointed to by the pointer.
[0016] Optionally, a new sliding window is created and used as the current sliding window, and the business data in the original sliding window is synchronized to the database, including:
[0017] Query the identifier of the unsynchronized sliding window from the task list;
[0018] Create a new sliding window, generate an identifier of the new sliding window, point a pointer to the sliding window, and store the identifier of the sliding window in the task list;
[0019] Lock the unsynchronized sliding window found out by the query, and synchronize the business data in the sliding window to the database;
[0020] The sliding window is deleted from the cache and the status of the sliding window in the task list is set to invalid.
[0021] Optionally, synchronizing the business data in the sliding window to a database includes:
[0022] Take the key from the sliding window, convert the business data corresponding to the key into a Java object, and synchronize it to the database;
[0023] Delete the key in the sliding window.
[0024] Optionally, the method further comprises:
[0025] Receive real-time data streams and assemble them into business data to be updated;
[0026] The persistence interface is called, and the business data to be updated is persisted in the database or stored in the cache according to the cache annotation on the persistence interface.
[0027] Optionally, determining, according to a cache annotation on the persistence interface, whether to persist the business data to be updated in a database or store it in a cache includes:
[0028] Call the persistence method and perform annotation parsing;
[0029] If the annotation parsing is successful, the business data to be updated is stored in the cache;
[0030] If annotation parsing fails, the business data to be updated will be persisted in the database.
[0031] In addition, according to another aspect of an embodiment of the present invention, there is provided a device for synchronizing data, including:
[0032] A storage module, used to store the business data to be updated in a cache;
[0033] A sliding window module, used for storing the changed business data in the cache into the current sliding window;
[0034] The synchronization module is used to create a new sliding window, use it as the current sliding window, and synchronize the business data in the original sliding window to the database.
[0035] Optionally, the storage module is used for:
[0036] Get the identifier of the current sliding window pointed to by the pointer, assemble the business primary key of the business data and the identifier of the sliding window into a string, use the string as the key, and use the attributes and attribute values of the business data as the value, and store them in the cache.
[0037] Optionally, the sliding window module is used for:
[0038] The key corresponding to the business data whose attribute value has changed and is stored in the cache is stored in the current sliding window pointed to by the pointer.
[0039] Optionally, the synchronization module is used to:
[0040] Query the identifier of the unsynchronized sliding window from the task list;
[0041] Create a new sliding window, generate an identifier of the new sliding window, point a pointer to the sliding window, and store the identifier of the sliding window in the task list;
[0042] Lock the unsynchronized sliding window found out by the query, and synchronize the business data in the sliding window to the database;
[0043] The sliding window is deleted from the cache and the status of the sliding window in the task list is set to invalid.
[0044] Optionally, synchronizing the business data in the sliding window to a database includes:
[0045] Take the key from the sliding window, convert the business data corresponding to the key into a Java object, and synchronize it to the database;
[0046] Delete the key in the sliding window.
[0047] Optionally, the device further comprises:
[0048] The receiving module is used to receive the real-time data stream and assemble it into the business data to be updated;
[0049] The judgment module is used to call the persistence interface and judge whether to persist the business data to be updated in the database or store it in the cache according to the cache annotation on the persistence interface.
[0050] Optionally, determining, according to a cache annotation on the persistence interface, whether to persist the business data to be updated in a database or store it in a cache includes:
[0051] Call the persistence method and perform annotation parsing;
[0052] If the annotation parsing is successful, the business data to be updated is stored in the cache;
[0053] If annotation parsing fails, the business data to be updated will be persisted in the database.
[0054] According to another aspect of an embodiment of the present invention, there is further provided an electronic device, including:
[0055] one or more processors;
[0056] a storage device for storing one or more programs,
[0057] When the one or more programs are executed by the one or more processors, the one or more processors implement the method described in any of the above embodiments.
[0058] According to another aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored. When the program is executed by a processor, the method described in any one of the above embodiments is implemented.
[0059] An embodiment of the above invention has the following advantages or beneficial effects: because the technical means of storing the business data that has been changed in the cache into the current sliding window, creating a new sliding window, and synchronizing the business data in the original sliding window to the database is adopted, the technical problems of high cost and data loss are overcome; the embodiment of the present invention stores the objects updated within a certain time interval in the sliding window, and the scheduled task only needs to synchronize the objects that have been changed in the sliding window, which reduces the amount of data that needs to be synchronized each time and improves the application performance. Moreover, the embodiment of the present invention can also realize arbitrary switching between application write cache and database. If an abnormality occurs in the cache or the scheduled task, the application can be switched to directly write to the database, and the data stored in the cache can be synchronized to the corresponding record in the database, so downgrade can be achieved very conveniently.
[0060] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS
[0061] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0062] Figure 1 is a schematic diagram of the main process of the method for synchronizing data according to an embodiment of the present invention;
[0063] Figure 2 is a schematic diagram of a sliding window and a pointer according to an embodiment of the present invention;
[0064] Figure 3 is a schematic diagram of the main process of a method for synchronizing data according to a reference embodiment of the present invention;
[0065] Figure 4 is a schematic diagram of the main process of a method for synchronizing data according to another reference embodiment of the present invention;
[0066] Figure 5 is a schematic diagram of main modules of a device for synchronizing data according to an embodiment of the present invention;
[0067] Figure 6 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0068] Figure 7 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION
[0069] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0070] Figure 1 FIG. 1 is a schematic diagram of the main process of the method for synchronizing data according to an embodiment of the present invention. Figure 1 As shown, as an embodiment of the present invention, the method for synchronizing data includes:
[0071] Step 101: store the business data to be updated in the cache.
[0072] In this step, the business data to be updated within a certain time interval can be stored in the cache. Optionally, the cache can be redis, and the business data to be updated is hashed in redis. In addition, the time interval can be set according to actual needs, and the embodiment of the present invention is not limited to this.
[0073] As another embodiment of the present invention, step 101 may include: obtaining the identifier of the current sliding window pointed to by the pointer, assembling the business primary key of the business data and the identifier of the sliding window into a string, using the string as the key and the attributes and attribute values of the business data as the value, and storing them in the cache.
[0074] Specifically, business data can be stored in redis using a hash map, where the key is a string composed of the business primary key of each piece of business data and the identifier of the sliding window in which it is located, and the value format is {"field1":value1, "field2":value2,...}, and hIncrBy(key,field,delta) in redis is used to add 1 or subtract 1 to each attribute value. The identifier of the sliding window can be the current time or a constant, as long as it can distinguish the sliding window, and the embodiment of the present invention does not limit this.
[0075] For example, the business data can be as follows:
[0076] Business data 1:
[0077] businessId1_car_002:{“field1”:value1,“field2”:value2,…}
[0078] Business data 2:
[0079] businessId2_car_002:{“field1”:value1,“field2”:value2,…}
[0080] …
[0081] Business data N:
[0082] businessIdN_car_002:{“field1”:value1,“field2”:value2,…}
[0083] Among them, businessIdN is the business primary key, car_002 is the identifier of the sliding window, businessIdN_car_002 is the key, and {"field1":value1, "field2":value2,...} is the value.
[0084] Optionally, before step 101, the method further includes: receiving a real-time data stream and assembling it into business data to be updated; calling a persistence interface and determining whether to persist the business data to be updated in a database or store it in a cache according to a cache annotation on the persistence interface. Therefore, when an exception occurs in the cache or a scheduled task, the application can be switched to directly write to the database, and the data stored in the cache can be updated to the corresponding record in the database.
[0085] Optionally, the business data to be updated is persisted in the database or stored in the cache according to the cache annotation on the persistence interface, including: calling the persistence method and performing annotation parsing; if the annotation parsing is successful, the business data to be updated is stored in the cache; if the annotation parsing fails, the business data to be updated is persisted in the database. When the persistence method is called and the object is persisted, annotation parsing will be performed to choose whether to store the data in the cache or persist it in the database. The embodiment of the present invention uses annotations to access the cache layer, which can achieve isolation between the cache and the business, allowing developers to focus more on business development. The cache layer can also be made pluggable, so there is no need to modify the business code when you do not want to use the cache.
[0086] The embodiment of the present invention implements the update of business data through AOP (Aspect Oriented Programming). When using it, it is only necessary to add a custom annotation to the persistence method, thereby realizing the separation of business logic processing and data persistence.
[0087] For example, this can be achieved through the following custom annotation @RedisMappingCache:
[0088] @RedisMappingCache(pointor="p_test", cacheBean="redisBean", disable=true, timeOut=10*60)
[0089] public int updateDetail(Sample sample){
[0090] }
[0091] The custom annotation RedisMappingCache is described as follows:
[0092] pointor: pointer, which points to the current window. After the business data is modified, its key is placed in the window pointed to by pointeror.
[0093] cacheBean: The cache cluster to use.
[0094] disable: Whether to enable cache. When true, use cache storage; when false, use database storage.
[0095] timeOut: timeout period. If the object's lifetime exceeds this period, it will be automatically destroyed to prevent the object from being stored in the cache without being recycled.
[0096] Step 102: store the changed business data in the cache into the current sliding window.
[0097] Based on step 101, the key corresponding to the business data whose attribute value has changed in the cache is stored in the current sliding window pointed to by the pointer. It should be noted that the attribute values of the business data stored in the cache may not necessarily change. The cache stores both business data whose attribute value has changed and business data whose attribute value has not changed. Therefore, step 102 stores the key corresponding to the business data whose attribute value has changed in the current sliding window pointed to by the pointer, such as Figure 2 As shown in the figure, the sliding window can be implemented using the set collection in redis, and a pointer in redis points to the current sliding window.
[0098] It should be noted that if the same key appears in the sliding window, deduplication is performed to ensure that there is no same key in a sliding window.
[0099] Therefore, the embodiment of the present invention adds a sliding window to record the key of the business data whose attribute values have been changed in each time period, so as to update these business data to the database in real time in subsequent steps.
[0100] Step 103: Create a new sliding window, use it as the current sliding window, and synchronize the business data in the original sliding window to the database.
[0101] After storing the business data within a certain time interval in the cache, you can use a scheduled task to execute it at a certain interval: first create a new sliding window, point the pointer to the new window, and then synchronize the business data in the original sliding window to the database. It is impossible to update all business data in each time interval, so each time the scheduled task is executed, there is no need to update all business data stored in the cache to the database, only the data that has been updated in the current time period needs to be updated to the database.
[0102] As another embodiment of the present invention, step 103 may include: first, querying the identifier of the unsynchronized sliding window from the task list; then, creating a new sliding window, generating the identifier of the new sliding window, pointing the pointer to the sliding window, and storing the identifier of the sliding window in the task list; then, locking the unsynchronized sliding window that was queried, and synchronizing the business data in the sliding window to the database; finally, deleting the sliding window from the cache, and setting the status of the sliding window in the task list to invalid. The embodiment of the present invention queries the unsynchronized sliding windows through the task list, and then synchronizes these unsynchronized sliding windows in sequence to ensure that data will not be lost. Moreover, the sliding window can be prevented from being repeatedly processed by locking the sliding window.
[0103] Optionally, synchronizing the business data in the sliding window to the database includes: taking the key from the sliding window, converting the business data corresponding to the key into a Java object, and synchronizing it to the database; deleting the key in the sliding window. Optionally, the Java object can be associated with the business data stored in the cache through annotations, thereby converting the business data in the cache into a Java object. For example, the po object in Java is associated with the business data in redis through the custom annotation @RedisMapping.
[0104] The RedisMapping annotation is added to the class or attribute as follows:
[0105] @RedisMapping(key = "id")
[0106] Public class Sample{
[0107] @RedisMapping(field="id",type="set")
[0108] private String id;
[0109] @RedisMapping(field="waybill_count",type="incr")
[0110] private Integer waybillCount;
[0111] }
[0112] The custom annotation RedisMapping is described as follows:
[0113] Key: The hash key corresponding to the business data in redis.
[0114] field: The field corresponding to the business data attribute in the redis hash.
[0115] type: The corresponding field data change mode is incr or set; incr means adding to the original value; set means setting the field value to a new value.
[0116] According to the various embodiments described above, it can be seen that the present invention adopts a technical solution of storing the business data that has changed in the cache into the current sliding window, creating a new sliding window, and synchronizing the business data in the original sliding window to the database, which solves the problem of high cost of use and data loss. The embodiment of the present invention stores the objects updated within a certain time interval in the sliding window, and the scheduled task only needs to synchronize the objects that have changed in the sliding window, which reduces the amount of data that needs to be synchronized each time and improves the application performance. Moreover, the embodiment of the present invention can also realize arbitrary switching between application write cache and database. If an exception occurs in the cache or scheduled task, the application can be switched to directly write the database, and the data stored in the cache can be synchronized to the corresponding records in the database, so it can be very convenient to achieve downgrade. In addition, the embodiment of the present invention encapsulates the persistence of data into components, and through custom annotations, realizes the association of the mapping of the po object in Java and the hash structure in redis, as well as the decoupling from the business. When developing real-time reports, the component can be directly reused to reduce development costs and improve development efficiency.
[0117] Figure 3 1 is a schematic diagram of the main process of a method for synchronizing data according to a reference embodiment of the present invention. The method for synchronizing data may include:
[0118] Step 301, receiving the real-time data stream, assembling it into the business data to be updated, and calling the persistence interface;
[0119] Step 302, determine whether to store the business data in redis according to the cache annotation on the persistence interface; if so, execute step 303; if not, execute step 308;
[0120] Step 303, determine whether the sliding window in redis is empty; if not, execute step 304; if yes, execute step 308;
[0121] Step 304, obtaining the identifier of the current sliding window pointed to by the pointer;
[0122] Step 305, assemble the business primary key of the business data and the identifier of the sliding window into a key;
[0123] Step 306, updating the attribute values of the business data in redis;
[0124] Step 307, storing the key corresponding to the business data whose attribute value has changed into the current sliding window;
[0125] Step 308: persist the business data to be updated in the database.
[0126] In addition, the specific implementation content of the method for synchronizing data in a reference embodiment of the present invention has been described in detail in the method for synchronizing data described above, so the repeated content will not be described again here.
[0127] Figure 4 : is a schematic diagram of the main process of a method for synchronizing data according to another reference embodiment of the present invention, and the method for synchronizing data may include:
[0128] Step 401, querying the identifier of the unsynchronized sliding window from the task list;
[0129] Step 402, creating a new sliding window, generating an identifier of the new sliding window, pointing a pointer to the sliding window, and storing the identifier of the sliding window in a task list;
[0130] Step 403, locking the unsynchronized sliding window found out;
[0131] Step 404, taking out the key from the sliding window;
[0132] Step 405: convert the business data corresponding to the key stored in redis into a Java object;
[0133] Step 406, persisting the Java object into the data;
[0134] Step 407, deleting the key of the business data from the sliding window;
[0135] Step 408, whether the sliding window is empty; if not, execute step 404; if so, execute step 409;
[0136] Step 409, deleting the sliding window from redis;
[0137] Step 410: Set the status of the sliding window in the task list to invalid.
[0138] In addition, the specific implementation content of the method for synchronizing data in another reference embodiment of the present invention has been described in detail in the method for synchronizing data described above, so the repeated content will not be described again here.
[0139] Figure 5 Schematic diagram of main modules of a device for synchronizing data according to an embodiment of the present invention. Figure 5 As shown, the apparatus 500 for synchronizing data includes a storage module 501, a sliding window module 502, and a synchronization module 503. The storage module 501 stores the business data to be updated in a cache; the sliding window module 502 stores the business data that has been changed in the cache in the current sliding window; the synchronization module 503 creates a new sliding window, uses it as the current sliding window, and synchronizes the business data in the original sliding window to the database.
[0140] Optionally, the storage module 501 obtains the identifier of the current sliding window pointed to by the pointer, assembles the business primary key of the business data and the identifier of the sliding window into a string, uses the string as the key, and uses the attributes and attribute values of the business data as the value, and stores it in the cache.
[0141] Optionally, the sliding window module 502 stores the key corresponding to the business data whose attribute value has changed and is stored in the cache into the current sliding window pointed to by the pointer.
[0142] Optionally, the synchronization module 503 queries the identifier of the unsynchronized sliding window from the task list; creates a new sliding window, generates a new sliding window identifier, points a pointer to the sliding window, and stores the identifier of the sliding window in the task list; locks the unsynchronized sliding window that has been queried, and synchronizes the business data in the sliding window to the database; deletes the sliding window from the cache, and sets the status of the sliding window in the task list to invalid.
[0143] Optionally, synchronizing the business data in the sliding window to a database includes:
[0144] Take the key from the sliding window, convert the business data corresponding to the key into a Java object, and synchronize it to the database;
[0145] Delete the key in the sliding window.
[0146] Optionally, the device also includes a receiving module and a judging module, the receiving module receives the real-time data stream and assembles it into business data to be updated; the judging module calls the persistence interface and judges whether to persist the business data to be updated in the database or store it in the cache according to the cache annotation on the persistence interface.
[0147] Optionally, determining, according to a cache annotation on the persistence interface, whether to persist the business data to be updated in a database or store it in a cache includes:
[0148] Call the persistence method and perform annotation parsing;
[0149] If the annotation parsing is successful, the business data to be updated is stored in the cache;
[0150] If annotation parsing fails, the business data to be updated will be persisted in the database.
[0151] According to the various embodiments described above, it can be seen that the present invention adopts a technical solution of storing the business data that has changed in the cache into the current sliding window, creating a new sliding window, and synchronizing the business data in the original sliding window to the database, which solves the problem of high cost of use and data loss. The embodiment of the present invention stores the objects updated within a certain time interval in the sliding window, and the scheduled task only needs to synchronize the objects that have changed in the sliding window, which reduces the amount of data that needs to be synchronized each time and improves the application performance. Moreover, the embodiment of the present invention can also realize arbitrary switching between application write cache and database. If an exception occurs in the cache or scheduled task, the application can be switched to directly write the database, and the data stored in the cache can be synchronized to the corresponding records in the database, so it can be very convenient to achieve downgrade. In addition, the embodiment of the present invention encapsulates the persistence of data into components, and through custom annotations, realizes the association of the mapping of the po object in Java and the hash structure in redis, as well as the decoupling from the business. When developing real-time reports, the component can be directly reused to reduce development costs and improve development efficiency.
[0152] It should be noted that the specific implementation content of the device for synchronizing data in the present invention has been described in detail in the method for synchronizing data described above, so the repeated content will not be described again here.
[0153] Figure 6 An exemplary system architecture 600 is shown to which the method for synchronizing data or the method for synchronizing data according to an embodiment of the present invention can be applied.
[0154] like Figure 6As shown, system architecture 600 may include terminal devices 601, 602, 603, network 604 and server 605. Network 604 is used to provide a medium for communication links between terminal devices 601, 602, 603 and server 605. Network 604 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0155] Users can use terminal devices 601, 602, and 603 to interact with server 605 through network 604 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 601, 602, and 603, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only examples).
[0156] The terminal devices 601 , 602 , and 603 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers, etc.
[0157] The server 605 may be a server that provides various services, such as a backend management server (only an example) that provides support for shopping websites browsed by users using the terminal devices 601, 602, and 603. The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results (such as target push information, product information - only an example) to the terminal device.
[0158] It should be noted that the method for synchronizing data provided in the embodiment of the present invention is generally executed on the server 605 , and accordingly, the device for synchronizing data is generally disposed in the server 605 .
[0159] It should be understood that Figure 6 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0160] Reference below Figure 7 , which shows a schematic diagram of the structure of a computer system 700 of a terminal device suitable for implementing an embodiment of the present invention. Figure 7 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0161] like Figure 7As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage part 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, the ROM 702, and the RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.
[0162] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, a mouse, etc.; an output section 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, a modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 710 as needed, so that a computer program read therefrom is installed into the storage section 708 as needed.
[0163] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 709, and / or installed from the removable medium 711. When the computer program is executed by the central processing unit (CPU) 701, the above-mentioned functions defined in the system of the present invention are executed.
[0164] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0165] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0166] The modules involved in the embodiments of the present invention may be implemented by software or hardware. The modules described may also be set in a processor, for example, it may be described as: a processor includes a storage module, a sliding window module and a synchronization module, wherein the names of these modules do not constitute a limitation on the modules themselves in some cases.
[0167] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: storing the business data to be updated in a cache; storing the business data that has changed in the cache in the current sliding window; creating a new sliding window, using it as the current sliding window, and synchronizing the business data in the original sliding window to the database.
[0168] According to the technical solution of the embodiment of the present invention, the technical problem of high cost and data loss is overcome because the technical means of storing the business data that has changed in the cache into the current sliding window, creating a new sliding window, and synchronizing the business data in the original sliding window to the database is adopted; the embodiment of the present invention stores the objects updated within a certain time interval in the sliding window, and the scheduled task only needs to synchronize the objects that have changed in the sliding window, which reduces the amount of data that needs to be synchronized each time and improves the application performance. Moreover, the embodiment of the present invention can also realize arbitrary switching between application write cache and database. If an abnormality occurs in the cache or the scheduled task, the application can be switched to directly write to the database, and the data stored in the cache can be synchronized to the corresponding record in the database, so downgrade can be realized very conveniently.
[0169] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for synchronizing data, characterized in that: include: Receive real-time data streams and assemble them into business data to be updated; Call the persistence interface, and determine whether to persist the business data to be updated in the database or store it in the cache according to the cache annotation on the persistence interface; Store the business data to be updated in the cache; Storing the changed business data in the cache into the current sliding window; Create a new sliding window, use it as the current sliding window, and synchronize the business data in the original sliding window to the database; Store the business data to be updated in the cache, including: Get the identifier of the current sliding window pointed to by the pointer, assemble the business primary key of the business data and the identifier of the sliding window into a string, use the string as the key, and use the attributes and attribute values of the business data as the value, and store them in the cache.
2. The method according to claim 1, characterized in that Storing the changed business data in the cache into the current sliding window includes: The key corresponding to the business data whose attribute value has changed and is stored in the cache is stored in the current sliding window pointed to by the pointer.
3. The method according to claim 1, characterized in that Create a new sliding window, use it as the current sliding window, and synchronize the business data in the original sliding window to the database, including: Query the identifier of the unsynchronized sliding window from the task list; Create a new sliding window, generate an identifier of the new sliding window, point a pointer to the sliding window, and store the identifier of the sliding window in the task list; Lock the unsynchronized sliding window found out of the query, and synchronize the business data in the sliding window to the database; The sliding window is deleted from the cache and the status of the sliding window in the task list is set to invalid.
4. The method according to claim 3, characterized in that Synchronize the business data in the sliding window to the database, including: Take the key from the sliding window, convert the business data corresponding to the key into a Java object, and synchronize it to the database; Delete the key in the sliding window.
5. The method according to claim 1, characterized in that Determining, according to the cache annotation on the persistence interface, whether to persist the business data to be updated in the database or store it in the cache, includes: Call the persistence method and perform annotation parsing; If the annotation parsing is successful, the business data to be updated is stored in the cache; If annotation parsing fails, the business data to be updated will be persisted in the database.
6. A device for synchronizing data, characterized in that: include: A storage module, used to store the business data to be updated in a cache; A sliding window module, used for storing the changed business data in the cache into the current sliding window; The synchronization module is used to create a new sliding window, use it as the current sliding window, and synchronize the business data in the original sliding window to the database; The storage module is also used for: Get the identifier of the current sliding window pointed to by the pointer, assemble the business primary key of the business data and the identifier of the sliding window into a string, use the string as the key, and use the attributes and attribute values of the business data as the value, and store them in the cache; The device also includes: The receiving module is used to receive the real-time data stream and assemble it into the business data to be updated; The judgment module is used to call the persistence interface and judge whether to persist the business data to be updated in the database or store it in the cache according to the cache annotation on the persistence interface.
7. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 5.
8. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.
