A distributed multi-level cache method and device
By using the event change message broadcast mechanism in a distributed system, real-time update of cache item information is achieved, the problems of cache penetration and service jitter are solved, and the timeliness of cache updates are improved.
Patent Information
- Application Number
- CN202410531019.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-29
- Publication Date
- 2025-02-18
- Estimated Expiration
- 2044-04-29
AI Technical Summary
In distributed systems, the prior art has problems of cache penetration and service jitter during cache updates, and the cache update is insufficient timeliness.
By setting event change messages in the preset management platform, when the cache item information is modified, these messages are obtained and broadcast to the service instance, so that they pull the latest cache data from the distributed cache and update it into the process cache .
Unconscious cache updates are implemented, which avoid cache penetration and service jitter, and improves the timeliness of cache updates.
Smart Images

Figure CN118363884B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of information technology, and in particular to a distributed multi-level cache method and device. Background Art
[0002] In distributed systems, multi-level cache is generally used to process high-performance functional interfaces in order to increase data access speed.
[0003] When updating the cache, the validity period is usually set by the in-process cache, or the service instance is set to a scheduled task separately.
[0004] For the solution of setting validity period for in-process cache, in high-concurrency service scenarios, at the critical point of data expiration, a large number of requests cannot access the in-process cache and access Redis at the same time, which causes jitter in the requests. At the same time, Redis needs to have higher performance requirements to meet the critical point of access. In addition, for some non-existent data, there is a problem of cache penetration.
[0005] For the solution of scheduled task and scheduled update, it is necessary to serve a separate scheduled task to continuously pull data. Even if the data is not updated, it is necessary to continuously pull and update the data. If the scheduled task time is set too long, the cached data will not be updated in time. Summary of the invention
[0006] The present application provides a distributed multi-level cache method and device to solve the problems existing in the related art.
[0007] In the first aspect, the present invention provides a distributed multi-level caching method, including obtaining an event change message sent by a preset management platform when cache item information in the preset management platform is modified; broadcasting information about changes to the cache item information to a service instance, so that the corresponding service instance pulls the latest cache data from the distributed cache after receiving the broadcast message, and updates it to the process cache.
[0008] Optionally, before obtaining the event change message sent by the preset management platform, the method also includes: in response to a registration request being triggered when at least one service instance in the service is started, registering the at least one service instance, wherein the registration request includes an identifier of each service instance and a multi-level cache key that identifies cache data; after the service instance is started, loading the registered multi-level cache key into the local process cache.
[0009] Optionally, when the cache item information in the preset management platform is modified, before the latest cache data is pulled from the distributed cache, the modified cache item information is sent to the database, and after the database data format is converted, the processed cache item information is synchronously updated to the distributed cache.
[0010] Optionally, broadcasting the information about changes to the cache item information to the service instance includes: broadcasting a detection message to the service instance so that the service instance determines whether the data subscribed to the cache item is communicating normally; if the detection result received from the service instance indicates normal communication, sending an instruction to the service instance to update the cache data corresponding to the multi-level cache key.
[0011] Optionally, the method further includes: receiving and recording notification information of completion of service instance update; and determining whether the update is successful based on the recorded information.
[0012] Optionally, further determining whether the update is successful based on the record information includes: if the number of successes indicated by the record is consistent with the registered number, the data update is successful; and asynchronously sending a message indicating the success of the update to the preset management platform.
[0013] In the second aspect, the present invention provides a distributed multi-level cache device, including a change message acquisition unit, which is configured to obtain an event change message sent by a preset management platform when the cache item information in the preset management platform is modified; a broadcast unit, which is configured to broadcast information about changes to the cache item information to a service instance, so that the corresponding service instance pulls the latest cache data from the distributed cache after receiving the broadcast message, and updates it to the process cache.
[0014] Optionally, the device also includes: in response to a registration request being triggered when at least one service instance in the service is started, registering the at least one service instance, wherein the registration request includes an identifier of each service instance and a multi-level cache key that identifies cache data; after the service instance is started, loading the registered multi-level cache key into the local process cache.
[0015] In a third aspect, the present invention provides a computer-readable storage medium, wherein the storage medium stores a computer program, and when the computer program is executed by a processor, the method described in any one of the first aspects is implemented.
[0016] In a fourth aspect, the present invention provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method provided in the first aspect when executing the program.
[0017] The present invention discloses a distributed multi-level cache method and device, wherein the method includes obtaining an event change message sent by a preset management platform after the cache item information in the preset management platform is modified; broadcasting the information of the change of the cache item information to the service instance, so that the corresponding service instance pulls the latest cache data from the distributed cache after receiving the broadcast message, and updates it to the process cache. Through the event notification mechanism, when the data changes, the management end notifies each distributed service to pull the cache and complete the imperceptible update. The cache will not expire according to time. When the interface requests access, it only needs to access the in-process cache, which solves the problem of cache penetration and service jitter when updating the cache, and also solves the timeliness problem of cache update. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0019] Figure 1 A distributed multi-level caching method is provided for this application;
[0020] Figure 2 This is a schematic diagram of an application of a distributed multi-level cache method for this application;
[0021] Figure 3 A method corresponding to the present application Figure 1 Schematic diagram of an electronic device. DETAILED DESCRIPTION
[0022] In order to make the purpose, technical solution and advantages of the present application clearer, the technical solution of the present application will be clearly and completely described below in combination with the specific embodiments of the present application and the corresponding drawings. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.
[0023] The following is combined with Figure 1 The distributed multi-level cache method is exemplified. The system architecture applicable to the method of this embodiment may include a management platform, a database, a distributed cache, a service instance, and a cache manager. The method for implementing the distributed cache will be described in detail later. The method includes the following steps:
[0024] Step 101: When cache item information in a preset management platform is modified, an event change message sent by the preset management platform is obtained.
[0025] In this embodiment, the subject that executes the method of this embodiment may be a cache manager, referring to Figure 2 , the data can be changed through the preset data management platform.
[0026] Step 102: broadcast the information about the change to the cache item information to the service instance, so that the corresponding service instance pulls the latest cache data from the distributed cache and updates it to the process cache after receiving the broadcast message.
[0027] In this embodiment, the same service can include multiple service instances in a distributed system. Figure 2 API service instance 01, API service instance 02, etc. The cache manager can complete the update in the form of broadcast.
[0028] As an optional implementation method of this embodiment, before obtaining the event change message sent by the preset management platform, the method also includes: in response to a registration request being triggered when at least one service instance in the service is started, registering the at least one service instance, wherein the registration request includes an identifier of each service instance and a multi-level cache key for identifying cache data; after the service instance is started, loading the registered multi-level cache key into the local process cache.
[0029] In this optional implementation, reference Figure 2 When the API service instance is started, it needs to register the service with the cache manager of the execution subject of this embodiment. When registering, it needs to carry the service instance ID and the multi-level cache key to be loaded, such as Figure 2 The instance IDs are (API service instance 01, API service instance 02), and the multi-level cache key is (key1). When the API service instance is started, the registered cache key1 needs to be loaded into the local process.
[0030] The management platform carries the updated cache item information, such as key1, and sends a change event message to the cache manager.
[0031] As an optional implementation method of this embodiment, when the cache item information in the preset management platform is modified, before the latest cache data is pulled from the distributed cache, the modified cache item information is sent to the database, and after the data format of the database is converted, the processed cache item information is synchronously updated to the distributed cache.
[0032] In this optional implementation, after the cache item is modified, the Mysql data is updated and the data is converted into a format and then synchronously updated to the distributed cache Redis.
[0033] As an optional implementation method of this embodiment, broadcasting the information of the change to the cache item information to the service instance includes: broadcasting a detection message to the service instance so that the service instance determines whether the data subscribed to the cache item is communicating normally; if the detection result fed back by the service instance indicates normal communication, sending an instruction to the service instance to update the cache data corresponding to the multi-level cache key.
[0034] In this optional implementation, after receiving the cache change request, the cache manager broadcasts to the service subscribed to the key1 cache item: broadcasting a detection message to determine whether the data subscribed to this cache item is communicating normally. After receiving the detection result, a broadcast of the key1 cache update will be issued.
[0035] Furthermore, the service instance is configured to report information of successfully receiving the broadcast detection message to the cache server after receiving the broadcast. If the cache server receives this report information, it indicates that the cache server and the service instance are communicating normally.
[0036] As an optional implementation of this embodiment, the method further includes: receiving and recording notification information of completion of service instance update; and judging whether the update is successful based on the recorded information.
[0037] As an optional implementation of this embodiment, further determining whether the update is successful based on the record information includes: if the number of successes indicated by the record is consistent with the registered number, the data update is successful; and asynchronously sending a message of successful update to the preset management platform.
[0038] In this optional implementation, the update is considered successful only when all instances under the service are updated successfully. If part of them succeed, it will cause data inconsistency. Therefore, when the update number is equal to the registration number, the data is considered to be updated successfully and the distributed cache is updated successfully.
[0039] After receiving the change message, the API service instance pulls the latest cache data from Redis, updates it to the process cache, and then returns a successful update message.
[0040] This embodiment receives and records the update success response of each service instance. When the update success number is consistent with the registration number, it means that the data update is successful, and the management platform is informed asynchronously at the same time. If no successful response is received within a certain period of time (30s) or some update errors occur, an alarm message is sent via email or SMS.
[0041] This embodiment changes the cache data update logic from the previous active pull to an event notification mechanism. When the data changes, the management end notifies each distributed service to pull the cache and complete the imperceptible update. The cache will not expire on time. When the interface requests access, it only needs to access the in-process cache, which solves the problems of cache penetration and service jitter when updating the cache, and also solves the timeliness problem of cache updates.
[0042] The access breakdown problem during cache update is solved to prevent access jitter at the expiration critical point. The cache data can be updated in a relatively real-time manner without the need for additional scheduled tasks to continuously refresh the cache. For non-existent data, it is directly returned if it cannot be accessed in the process cache, and there is no cache penetration problem.
[0043] The above is a method provided for one or more embodiments of the present application. Based on the same idea, the present application also provides a corresponding distributed multi-level cache device, including: a change message acquisition unit, configured to obtain the event change message sent by the preset management platform when the cache item information in the preset management platform is modified; a broadcast unit, configured to broadcast the information of the change to the cache item information to the service instance, so that the corresponding service instance pulls the latest cache data from the distributed cache after receiving the broadcast message, and updates it to the process cache.
[0044] As an optional implementation method of this embodiment, before obtaining the event change message sent by the preset management platform, the method also includes: in response to a registration request being triggered when at least one service instance in the service is started, registering the at least one service instance, wherein the registration request includes an identifier of each service instance and a multi-level cache key for identifying cache data; after the service instance is started, loading the registered multi-level cache key into the local process cache.
[0045] As an optional implementation method of this embodiment, when the cache item information in the preset management platform is modified, before the latest cache data is pulled from the distributed cache, the modified cache item information is sent to the database, and after the data format of the database is converted, the processed cache item information is synchronously updated to the distributed cache.
[0046] As an optional implementation method of this embodiment, broadcasting the information of the change to the cache item information to the service instance includes: broadcasting a detection message to the service instance so that the service instance determines whether the data subscribed to the cache item is communicating normally; if the detection result fed back by the service instance indicates normal communication, sending an instruction to the service instance to update the cache data corresponding to the multi-level cache key.
[0047] As an optional implementation of this embodiment, the method further includes: receiving and recording notification information of completion of service instance update; and judging whether the update is successful based on the recorded information.
[0048] As an optional implementation of this embodiment, further determining whether the update is successful based on the record information includes: if the number of successes indicated by the record is consistent with the registered number, the data update is successful; and asynchronously sending a message of successful update to the preset management platform.
[0049] Figure 3 A schematic diagram of the physical structure of an electronic device provided by an embodiment of the present invention, such as Figure 3 As shown, the electronic device 50 includes: a processor 501 (processor), a memory 502 (memory) and a bus 503; wherein the processor 501 and the memory 502 communicate with each other through the bus 503; the processor 501 is used to call the program instructions in the memory 502 to execute the methods provided by the above-mentioned method embodiments.
[0050] This embodiment provides a non-transitory computer-readable storage medium, which stores computer instructions. The computer instructions enable a computer to execute the methods provided by the above method embodiments.
[0051] A person skilled in the art can understand that all or part of the steps of implementing the above method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above method embodiment; and the aforementioned storage medium includes: ROM, RAM, magnetic disk or optical disk, etc., various storage media that can store program codes.
[0052] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, i.e., they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Those of ordinary skill in the art may understand and implement it without creative effort.
[0053] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods of each embodiment or some parts of the embodiment.
[0054] Although the present invention has been described in detail above by general description and specific embodiments, it is obvious to those skilled in the art that some modifications or improvements can be made to the present invention. Therefore, these modifications or improvements made without departing from the spirit of the present invention all belong to the scope of protection claimed by the present invention.
Claims
1. A distributed multi-level cache method, characterized in that: include: When the cache item information in the preset management platform is modified, obtaining an event change message sent by the preset management platform; Broadcasting the information about the change of the cache item information to the service instance, so that the service instance pulls the latest cache data from the distributed cache and updates it to the process cache after receiving the broadcast message; Before obtaining the event change message sent by the preset management platform, the method further includes: In response to a registration request being triggered when at least one service instance in the service is started, registering the at least one service instance, wherein the registration request includes an identifier of each service instance and a multi-level cache key identifying cache data; after the service instance is started, loading the registered multi-level cache key into a local process cache; When the cache item information in the preset management platform is modified, before the latest cache data is pulled from the distributed cache, the modified cache item information is sent to the database, and after the data format of the database is converted, the processed cache item information is synchronously updated to the distributed cache.
2. The distributed multi-level cache method according to claim 1, characterized in that: Broadcasting the information about the change to the cache item information to the service instance includes: Broadcast a detection message to the service instance so that the service instance can determine whether the data subscribed to the cache item is communicating normally; If the detection result fed back by the service instance indicates normal communication, an instruction to update the cache data corresponding to the multi-level cache key is sent to the service instance.
3. The distributed multi-level cache method according to claim 1, characterized in that: The method further comprises: Receive and record notification information of service instance update completion; Determine whether the update is successful based on the recorded information.
4. The distributed multi-level cache method according to claim 3, characterized in that: Further determining whether the update is successful based on the record information includes: If the number of successes indicated by the record is consistent with the registered number, the data update is successful; A message indicating successful update is asynchronously sent to the preset management platform.
5. A distributed multi-level cache device, characterized in that: include: A change message acquisition unit, configured to acquire an event change message sent by the preset management platform when cache item information in the preset management platform is modified; A broadcast unit, configured to broadcast information about changes to the cache item information to the service instance, so that the service instance pulls the latest cache data from the distributed cache and updates it to the process cache after receiving the broadcast message; The apparatus further comprises: in response to a registration request being triggered when at least one service instance in the service is started, registering the at least one service instance, wherein the registration request includes an identifier of each service instance and a multi-level cache key identifying cache data; after the service instance is started, loading the registered multi-level cache key into a local process cache; When the cache item information in the preset management platform is modified, before the latest cache data is pulled from the distributed cache, the modified cache item information is sent to the database, and after the data format of the database is converted, the processed cache item information is synchronously updated to the distributed cache.
6. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the method according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Method and system for managing cache memories
CN103513935A
Data caching achieving method
CN103617131A