Cache optimization method, system and equipment and storage medium

By monitoring the life cycle status of the component and clearing the invalid message channel, the problem of excessive memory occupancy in the message bus is solved, memory overflow is avoided, and message transmission efficiency is optimized.

CN119938546APending Publication Date: 2025-05-06BEIJING 58 INFORMATION TTECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411979376.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-30
Publication Date
2025-05-06

AI Technical Summary

Technical Problem

Over time, the number of messages cached in the message bus gradually increases, resulting in an increase in memory usage, which may exceed the memory limit, causing memory overflow (OOM) problems.

Method used

Through the observation class instance bound to the monitoring class instance corresponding to the component, determine the life cycle state of the component, remove the observation class instance corresponding to the component in the destroyed state, and clear the message channels between the components to reduce memory usage.

Benefits of technology

It effectively reduces the memory usage rate in the message bus, avoids memory overflow problems, and optimizes the efficiency of message transmission.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119938546A_ABST
    Figure CN119938546A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a cache optimization method, system and device and a storage medium, and the method comprises the steps: determining the life cycle state of a first component through employing a first observation type instance bound with a first monitoring type instance; and determining the life cycle state of the second component by using a second observation type instance bound with the second monitoring type instance. Then, determining a target component in a destroyed state in the first component and the second component, and removing the observation class instance corresponding to the target component; and if the removed observation class instances comprise the first observation class instance and the second observation class instance, removing a message channel between the first component and the second component. When the first component and the second component are in the destroyed state, it is indicated that the first component and the second component do not need to carry out message transmission any more, the message channel between the first component and the second component can be removed, that is, the message cached in the message channel can be cleared, and therefore the occupancy rate of a memory in the message bus can be reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a cache optimization method, system, device and storage medium. Background Art

[0002] In practice, message transmission between different components in an application can be implemented through a message bus (MessageBus). The message bus can receive a message sent by a first component as a publisher and cache the message in its own memory so that a second component as a subscriber can quickly obtain the message from the message bus.

[0003] However, as time goes by, the number of cached messages in the message bus gradually increases. A large number of cached messages may occupy a large amount of memory in the message bus and may exceed the memory limit corresponding to the message bus, resulting in an Out Of Memory (OOM) problem.

[0004] Based on the above description, how to reduce the memory usage in the message bus becomes a technical problem that needs to be solved urgently. Summary of the invention

[0005] In view of this, an embodiment of the present invention provides a cache optimization method, system, device and storage medium to reduce the memory occupancy rate in a message bus.

[0006] In a first aspect, an embodiment of the present invention provides a cache optimization method, which is applied to a message bus, wherein the message bus includes observation class instances corresponding to components one by one, and the method includes:

[0007] Determining a lifecycle state of a first component as a publisher using a first observation class instance bound to a first monitoring class instance, the first monitoring class instance corresponding to the first component;

[0008] Determining a lifecycle state of a second component as a subscriber using a second observation class instance bound to a second monitoring class instance, the second monitoring class instance corresponding to the second component;

[0009] Determine a target component in the first component and the second component that is in a destroyed state, wherein the target component includes the first component and / or the second component;

[0010] Remove the observation class instance bound to the target monitoring class instance corresponding to the target component;

[0011] If the removed observation class instances include the first observation class instance and the second observation class instance, then the message channel between the first component and the second component in the message bus is removed.

[0012] In a second aspect, an embodiment of the present invention provides a cache optimization system, comprising: a message bus, a first component as a publisher, and a second component as a subscriber;

[0013] The first component is used to obtain a first monitoring class instance corresponding to the first component; and send the first monitoring class instance to the message bus;

[0014] The second component is used to obtain a second monitoring class instance corresponding to the second component; and send the second monitoring class instance to the message bus;

[0015] The message bus is used to determine the life cycle state of the first component using the first observation class instance bound to the first monitoring class instance; determine the life cycle state of the second component using the second observation class instance bound to the second monitoring class instance; determine the target component in the first component and the second component that is in a destroyed state; remove the observation class instance bound to the target monitoring class instance corresponding to the target component; if the removed observation class instances include the first observation class instance and the second observation class instance, remove the message channel between the first component and the second component in the message bus.

[0016] In a third aspect, an embodiment of the present invention provides a cache optimization device, including:

[0017] A life cycle state determination module, configured to determine the life cycle state of a first component as a publisher by using a first observation class instance bound to a first monitoring class instance, wherein the first monitoring class instance corresponds to the first component; and to determine the life cycle state of a second component as a subscriber by using a second observation class instance bound to a second monitoring class instance, wherein the second monitoring class instance corresponds to the second component;

[0018] a target component determination module, used to determine a target component in a destroyed state among the first component and the second component, wherein the target component includes the first component and / or the second component;

[0019] A removal module is used to remove the observation class instance bound to the target monitoring class instance corresponding to the target component; if the removed observation class instance includes the first observation class instance and the second observation class instance, the message channel between the first component and the second component in the message bus is removed.

[0020] In a fourth aspect, an embodiment of the present invention provides an electronic device, comprising a processor and a memory, wherein the memory is used to store one or more computer instructions, wherein the one or more computer instructions, when executed by the processor, implement the cache optimization method in the first aspect. The electronic device may also include a communication interface for communicating with other devices or communication systems.

[0021] In a fifth aspect, an embodiment of the present invention provides a non-temporary machine-readable storage medium having executable code stored thereon. When the executable code is executed by a processor of an electronic device, the processor can at least implement the cache optimization method as described in the first aspect above.

[0022] In a sixth aspect, an embodiment of the present invention provides a computer program product, which includes a computer program or instructions. When the computer program or instructions are executed by a processor, the processor is enabled to implement the cache optimization method as described in the first aspect above.

[0023] In the cache optimization method provided by an embodiment of the present invention, the message bus can use the first observation class instance bound to the first monitoring class instance to determine the life cycle state of the first component as the publisher; and use the second observation class instance bound to the second monitoring class instance to determine the life cycle state of the second component as the subscriber. Among them, the first monitoring class instance corresponds to the first component, and the second monitoring class instance corresponds to the second component. Then, the message bus can determine the target component in the first component and the second component that is in a destroyed state, and the target component includes the first component and / or the second component. Afterwards, the message bus can remove the observation class instance bound to the target monitoring class instance corresponding to the target component. If the removed observation class instance includes the first observation class instance and the second observation class instance, the message channel between the first component and the second component in the message bus is removed.

[0024] It can be seen that in the above scheme, the message bus can know the life cycle status of the first component and the second component respectively through the observation class instance bound to the monitoring class instance corresponding to the first component and the second component respectively. And when the first component and the second component are in the destroyed state, it indicates that the first component and the second component no longer need to transmit messages. Then, after removing the observation class instance corresponding to the first component and the second component respectively, the message bus can further remove the message channel between the first component and the second component, that is, it can clear the messages cached in the message channel, thereby reducing the memory occupancy rate in the message bus. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0026] Figure 1 A flowchart of a cache optimization method provided by an embodiment of the present invention;

[0027] Figure 2 A schematic diagram of the working process of a cache optimization method provided by an embodiment of the present invention;

[0028] Figure 3 A schematic diagram of the structure of a cache optimization system provided by an embodiment of the present invention;

[0029] Figure 4 A schematic diagram of a message publishing and subscription process provided by an embodiment of the present invention;

[0030] Figure 5 A schematic diagram of the structure of a cache optimization device provided by an embodiment of the present invention;

[0031] Figure 6 For Figure 5 A schematic diagram of the structure of an electronic device corresponding to the cache optimization device provided in the illustrated embodiment. DETAILED DESCRIPTION

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

[0033] The terms used in the embodiments of the present invention are only for the purpose of describing specific embodiments, and are not intended to limit the present invention. The singular forms "a", "said" and "the" used in the embodiments of the present invention and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings, and "multiple" generally includes at least two, but does not exclude the inclusion of at least one.

[0034] It should be understood that the term "and / or" used in this article is only a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this article generally indicates that the associated objects before and after are in an "or" relationship.

[0035] As used herein, the words "if" and "if" may be interpreted as "at the time of" or "when" or "in response to determining" or "in response to identifying", depending on the context. Similarly, the phrases "if it is determined" or "if (stated condition or event) is identified" may be interpreted as "when determining" or "in response to determining" or "when identifying (stated condition or event)" or "in response to identifying (stated condition or event)", depending on the context.

[0036] It should be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a product or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such product or system. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the product or system including the elements.

[0037] It should also be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in the present invention are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0038] Before describing in detail the cache optimization method and cache optimization system provided by the following embodiments of the present invention, the related concepts involved in the following embodiments may be explained first:

[0039] LiveData is a data holding container class with lifecycle awareness, which implements the observer pattern. That is, LiveData allows registered observer class instances to receive notifications when data changes. In other words, when the data in LiveData changes, all components that observe this LiveData (i.e. observers) will receive notifications and obtain the changed data. Among them, the observer class instance refers to the specific instance used to implement the observer.

[0040] LifecycleOwner is an interface that represents a component with a lifecycle. The lifecycle state of a component with a lifecycle can be monitored by implementing a specific instance of the LifecycleOwner interface, i.e., a monitoring class instance. Among them, a component with a lifecycle may include an Activity component or a Fragment component, etc.

[0041] Before describing in detail the cache optimization method and cache optimization system provided by each embodiment of the present invention, an application scenario of cache optimization may be schematically described:

[0042] As described in the background technology, the message transmission between the first component as a publisher and the second component as a subscriber in the application can be realized through a message bus. Among them, the components in the application can include Activity components or Fragment components, etc.

[0043] And the message bus has the function of caching the messages sent by the first component. However, in actual applications, as time goes by, the messages sent by the first component may continue to increase, which will cause the number of cached messages in the message bus to gradually increase. A large number of cached messages may occupy a large amount of memory in the message bus, and may also exceed the memory limit corresponding to the message bus, thereby causing a memory overflow problem. In order to improve the above problems, the cache optimization method and cache optimization system provided in the following embodiments of the present invention can be used.

[0044] Based on the above description, some embodiments of the present invention are described in detail below in conjunction with the accompanying drawings. In the case where there is no conflict between the embodiments, the following embodiments and the features in the embodiments can be combined with each other. In addition, the step sequence in the following method embodiments is only an example and not a strict limitation.

[0045] Figure 1 A flowchart of a cache optimization method provided by an embodiment of the present invention. The cache optimization method provided by an embodiment of the present invention can be executed by a message bus. Optionally, the message bus can include observation class instances corresponding to components one by one. In other words, the message bus can be understood as a message bus implemented based on LiveData, which has data observation capabilities. It can be understood that the message bus can be implemented as software, or a combination of software and hardware. Figure 1 As shown, the method comprises the following steps:

[0046] 101. Determine the life cycle state of a first component as a publisher by using a first observation class instance bound to a first monitoring class instance, the first monitoring class instance corresponding to the first component.

[0047] 102. Determine the life cycle state of a second component as a subscriber by using a second observation class instance bound to a second monitoring class instance, the second monitoring class instance corresponding to the second component.

[0048] 103 , determining a target component in the first component and the second component that is in a destroyed state, where the target component includes the first component and / or the second component.

[0049] 104. Remove the observation class instance bound to the target monitoring class instance corresponding to the target component.

[0050] 105. If the removed observation class instances include the first observation class instance and the second observation class instance, remove the message channel between the first component and the second component in the message bus.

[0051] The message bus can use the first observation class instance bound to the first monitoring class instance to determine the life cycle state of the first component as the publisher. At the same time, the message bus can also use the second observation class instance bound to the second monitoring class instance to determine the life cycle state of the second component as the subscriber.

[0052] Optionally, the first monitoring class instance corresponds to the first component, and may specifically include a specific instance corresponding to the first component for implementing the LifecycleOwner interface to monitor the lifecycle state of the first component. The second monitoring class instance corresponds to the second component, and may specifically include a specific instance corresponding to the second component for implementing the LifecycleOwner interface to monitor the lifecycle state of the second component. Optionally, the lifecycle state may include a creation state, a start state, a resume state, a pause state, a stop state, and a destruction state, etc.

[0053] Optionally, the first monitoring class instance can directly notify the first observation class instance of the life cycle state of the first monitoring class instance through a function call. Specifically, the first component can notify the first observation class instance of the life cycle state of the first monitoring class instance when calling a message sending method (such as the setValue() method). It can be understood that methods are usually regarded as a specific form of function in programming. The message bus can then determine the life cycle state of the first component based on the life cycle state of the first monitoring class instance received by the first observation class instance.

[0054] Optionally, the second monitoring class instance can also directly notify the second observation class instance of the life cycle state of the second monitoring class instance through a function call. Specifically, the second component can notify the second observation class instance of the life cycle state of the second monitoring class instance when calling a message receiving method (such as the observe() method). The message bus can then determine the life cycle state of the second component based on the life cycle state of the second monitoring class instance received by the second observation class instance.

[0055] Afterwards, the message bus can use the determined life cycle state of the component to further determine the target component in the destroyed state from the first component and the second component. The target component includes the first component and / or the second component. In actual applications, when the target component is in the destroyed state, it indicates that the life cycle of the target component has ended and no longer needs to transmit messages. The message bus can remove the observation class instance bound to the target monitoring class instance corresponding to the target component, that is, there is no need to observe the target component anymore.

[0056] One situation may be: if the removed observation class instances include the first observation class instance corresponding to the first component and the second observation class instance corresponding to the second component, indicating that the first component and the second component are both in a destroyed state and no longer need to send or subscribe to messages, the message bus can directly remove the message channel between the first component and the second component in the message bus.

[0057] In this embodiment, the message bus can know the life cycle status of the first component and the second component through the observation class instance bound to the monitoring class instance corresponding to the first component and the second component respectively. And when the first component and the second component are in the destroyed state, it indicates that the first component and the second component no longer need to transmit messages. Then, after removing the observation class instance corresponding to the first component and the second component respectively, the message bus can further remove the message channel between the first component and the second component, that is, it can clear the messages cached in the message channel, thereby reducing the memory occupancy rate in the message bus.

[0058] Figure 1 It has been mentioned in the illustrated embodiment that the message bus can determine the life cycle state of a component by utilizing the binding relationship between an observation class instance and a monitoring class instance.

[0059] Wherein, for determining the binding relationship between the first observation class instance and the first monitoring class instance, optionally, the message bus can establish a binding relationship between the first monitoring class instance and the first observation class instance in response to the registration of the first observation class instance. Wherein, the first observation class instance is used to observe the life cycle changes of the first component.

[0060] Similarly, for determining the binding relationship between the second observation class instance and the second monitoring class instance, optionally, the message bus can establish a binding relationship between the second monitoring class instance and the second observation class instance in response to the registration of the second observation class instance, wherein the second observation class instance is used to observe the life cycle changes of the second component.

[0061] Optionally, the observation class instance can register itself in the message bus by calling a function (such as a register() method or a subscribe() method). Optionally, the message bus can include a registration center, which can be used to manage the observation class instance.

[0062] In this embodiment, in response to the registration of the observation class instance, the message bus binds the observation class instance with the monitoring class instance, so that when the life cycle of the monitoring class instance corresponding to the component changes, the life cycle state of the component can be quickly and accurately determined.

[0063] In addition, Figure 1 One situation is given in step 105 of the illustrated embodiment. Another situation may be: if the removed observation class instances include the first observation class instances, it indicates that the first component as the publisher is in a destroyed state, that is, the first component will no longer send message objects to the message bus in the future. Then the message bus can further determine the type of message objects stored in the message channel between the first component and the second component.

[0064] If it is determined that the type of the message object is a non-sticky message, it indicates that the message bus does not need to permanently store the message object, and other components will not obtain the message object in the future, so the message bus can remove the non-sticky message. If it is determined that the type of the message object is a sticky message, it indicates that the message bus needs to permanently store the message object, and other components may need to obtain the message object in the future, so the message bus may not remove the sticky message, so that the second component as a subscriber can obtain the message object stored in the message bus in the future. Among them, the specific process of determining the type of the message object can be referred to the description in the following embodiments.

[0065] In this embodiment, Figure 1 The situation mentioned in the illustrated embodiment is different. When the first component is in a destroyed state, it indicates that the first component no longer needs to send messages. Then, the type of the message object stored in the message channel between the first component and the second component can be further determined to determine whether the message object needs to be retained.

[0066] When the type of the message object stored in the message channel is a non-sticky message, it indicates that the message object does not need to be permanently stored and other components will no longer obtain the message object. Therefore, the message bus can reduce the memory usage in the message bus by clearing the message objects that are non-sticky messages stored in the message channel.

[0067] When the type of the message object stored in the message channel is a sticky message, it indicates that the message object needs to be permanently stored, and other components may need to obtain the message object in the future. In this case, the message bus continues to retain the message object that is a sticky message stored in the message channel to ensure that other components can quickly obtain the message object of the sticky message type from the message channel.

[0068] In addition, Figure 1 One situation is given in step 105 of the illustrated embodiment. Another situation may be: if the removed observation class instances include the second observation class instances, it indicates that the second component as the subscriber is in a destroyed state, and the second component no longer needs to subscribe to the message object. However, other subsequent components may subscribe to the message object from the message bus according to their own needs. In this case, the message bus may not remove the message object stored in the message channel.

[0069] In this embodiment, considering the situation that only the second component as the subscriber is in the destroyed state, and in this case, although the second component no longer needs to subscribe to the message, considering that other components may need to obtain the message object in the future, the message bus may not remove the message object stored in the message channel to ensure the normal subscription of other components to the message object.

[0070] For ease of understanding, you can combine Figure 2 The specific implementation process of the cache optimization method provided above is exemplified as follows:

[0071] In the above embodiments, it has been mentioned that the message bus can provide an observer mode, and the message bus can include an observer class instance corresponding to each component. In actual applications, the message bus will also provide an observer wrapper class, which is used to manage the life cycle state of the observer (i.e., the first component and the second component).

[0072] The message bus may first create a message channel between the first component and the second component, and the message bus may record a version number (mVersion), which is used to record the update status of the message object. When the message channel is first created, no message object is stored in the message channel, and at this time, the initial value of the version number may be set to -1.

[0073] For any observer, it also has a version number (mLastVersion), which is used to record the version number of the message object in the message channel it last received. And the initial value of the observer's version number is also -1.

[0074] The first component can call a message sending method (such as the setValue() method) to send the first monitoring class instance and the first observation class instance to the message bus. The second component can call a message receiving method (such as the observe() method) to input the second monitoring class instance and the second observation class instance corresponding to the second component into the message bus, so that the message bus can bind the first monitoring class instance and the first observation class instance, and bind the second monitoring class instance and the second observation class instance respectively. Among them, the first monitoring class instance includes the specific instance corresponding to the first component for implementing the lifecycle interface (i.e., LifecycleOwner interface). The second monitoring class instance includes the specific instance corresponding to the second component for implementing the lifecycle interface (i.e., LifecycleOwner interface).

[0075] Then, the message bus can use the first observation class instance bound to the first monitoring class instance to determine the life cycle state of the first component. At the same time, the message bus can use the second observation class instance bound to the second monitoring class instance to determine the life cycle state of the second component.

[0076] If it is determined that the life cycle state of the component is not in the destroyed state, the message bus can compare whether the observer version number is less than the message bus version number to determine whether there is a data update. Specifically, when the first component calls the message sending method to send the message object to the message bus, it indicates that there is an update to the data in the message bus, and the version number (mVersion) will increase, such as the version number + 1. At this time, the message bus can traverse the observer and compare whether the observer version number (mLastVersion) is less than the message bus version number (mVersion). If it is determined that the observer's version number is less than the message bus version number, the message bus can notify the observer of the message object corresponding to the currently incremented message bus version number and update the observer version number.

[0077] Afterwards, the message bus may call a message update notification method (such as an onChanged() method) to notify the observer that the message object has been updated, and send the updated message object to the observer. Optionally, the observer may include an observer of the first component or an observer of the second component.

[0078] If the message bus determines that the life cycle state of the target component is in the destroyed state, the message bus can remove the observation class instance corresponding to the target component, where the target component includes the first component and / or the second component.

[0079] Furthermore, the message bus can determine whether there are any remaining observation class instances. If it is determined that there are no remaining observation class instances, that is, the removed observation class instances include the first observation class instance corresponding to the first component and the second observation class instance corresponding to the second component, indicating that the first component and the second component are both in a destroyed state and no longer need to send or subscribe to messages, the message bus can directly remove the message channel between the first component and the second component in the message bus to reduce the memory occupancy rate in the message bus.

[0080] If the message bus determines that there are still remaining observation class instances, it further determines whether all first observation class instances have been removed. If all first observation class instances have been removed, that is, the removed observation class instances include the first observation class instances, it indicates that the first component as the publisher is in a destroyed state, that is, the first component will no longer send message objects to the message bus in the future, then the message bus can further determine whether the message object stored in the message channel between the first component and the second component is a non-sticky message.

[0081] If it is determined that the type of the message object is a non-sticky message, it means that the message bus does not need to permanently store the message object and other components will not obtain the message object later. The message bus can remove the message object belonging to the non-sticky message type to reduce the memory usage in the message bus.

[0082] In addition, the contents not described in detail in this embodiment and the technical effects that can be achieved can also be found in the description of the above embodiments, and will not be repeated here.

[0083] Figure 3 A schematic diagram of a cache optimization system provided by an embodiment of the present invention is shown in FIG. Figure 3 As shown, the cache optimization system includes: a message bus, a first component as a publisher, and a second component as a subscriber.

[0084] The first component may obtain a first monitoring class instance corresponding to the first component and send the first monitoring class instance to the message bus. The second component may obtain a second monitoring class instance corresponding to the second component and send the second monitoring class instance to the message bus.

[0085] The first monitoring class instance may specifically include a specific instance corresponding to the first component for implementing the LifecycleOwner interface to monitor the lifecycle state of the first component. The second monitoring class instance may specifically include a specific instance corresponding to the second component for implementing the LifecycleOwner interface to monitor the lifecycle state of the second component.

[0086] The message bus can use the first observation class instance bound to the first monitoring class instance to determine the life cycle state of the first component. At the same time, the message bus can use the second observation class instance bound to the second monitoring class instance to determine the life cycle state of the second component. Optionally, the life cycle state can include a creation state, a start state, a resume state, a pause state, a stop state, and a destruction state, etc.

[0087] Then, the message bus can use the life cycle state of the component determined by the observation class instance to further determine the target component in the destroyed state from the first component and the second component. Among them, the target component includes the first component and / or the second component. And in actual applications, when the target component is in the destroyed state, it indicates that the life cycle of the target component has ended and no longer needs to transmit messages. Then the message bus can remove the observation class instance bound to the target monitoring class instance corresponding to the target component, that is, there is no need to observe the target component anymore.

[0088] Optionally, if the removed observation class instances include the first observation class instance corresponding to the first component and the second observation class instance corresponding to the second component, indicating that the first component and the second component are both in a destroyed state and there is no need to send or subscribe to messages, the message bus can directly remove the message channel between the first component and the second component in the message bus.

[0089] In the cache optimization system provided by an embodiment of the present invention, the cache optimization system includes a message bus, a first component as a publisher, and a second component as a subscriber. Among them, the first component can obtain a first monitoring class instance corresponding to the first component, and send the first monitoring class instance to the message bus. The second component can obtain a second monitoring class instance corresponding to the second component, and send the second monitoring class instance to the message bus. The message bus can use the first observation class instance bound to the first monitoring class instance to determine the life cycle state of the first component as a publisher; and use the second observation class instance bound to the second monitoring class instance to determine the life cycle state of the second component as a subscriber. Then, the message bus can determine the target component in the first component and the second component that is in a destroyed state, and remove the observation class instance bound to the target monitoring class instance corresponding to the target component. If the removed observation class instances include the first observation class instance and the second observation class instance, the message channel between the first component and the second component in the message bus is removed.

[0090] It can be seen that in the above scheme, the message bus can know the life cycle status of the first component and the second component respectively through the observation class instance bound to the monitoring class instance corresponding to the first component and the second component respectively. And when the first component and the second component are in the destroyed state, it indicates that the first component and the second component no longer need to transmit messages. Then, after removing the observation class instance corresponding to the first component and the second component respectively, the message bus can further remove the message channel between the first component and the second component, that is, it can clear the messages cached in the message channel, thereby reducing the memory occupancy rate in the message bus.

[0091] Optionally, if the removed observation class instances include the first observation class instances, it indicates that the first component as the publisher is in a destroyed state, that is, the first component will no longer send message objects to the message bus. The message bus can further determine the type of the first message object stored in the message channel between the first component and the second component.

[0092] If it is determined that the type of the first message object is a non-sticky message, the message bus may remove the first message object belonging to the non-sticky message. If it is determined that the type of the first message object is a sticky message, the message bus may not remove the first message object belonging to the sticky message.

[0093] It should be noted that in order to distinguish the message object sent during the message channel creation phase from the message object sent after the message channel already exists, the message object sent by the first component after the message channel already exists can be called the first message object. Figures 1-2 The message object in the illustrated embodiment may also be referred to as a first message object, and the message object sent by the first component during the message channel creation phase is referred to as a second message object. In other words, the second message object is sent earlier than the first message object.

[0094] In addition, the contents not described in detail in this embodiment and the technical effects that can be achieved can also be found in the description of the above embodiments, and will not be repeated here.

[0095] In actual applications, the message bus may include multiple message channels, and different message channels may be used to transmit different types of messages.

[0096] For the creation of a message channel, optionally, the message bus can first obtain a second message object encapsulating a class instance and sent by the first component. The second message object is sent earlier than the first message object. Then, the message bus can obtain the class identifier of the class instance. Optionally, the message bus can use a message processing component in a cache optimization system (such as a Kotlin compiler) to execute an inline function by the message processing component to identify the class identifier of the class instance. Afterwards, the message bus can use the class identifier as a channel identifier to create a message channel corresponding to the class identifier. And the message bus can store the second message object in the message channel.

[0097] Among them, for the generation of the second message object, optionally, the first component can first create a class instance with a class identifier, and generate a second message object encapsulating the class instance. Specifically, the first component can first determine the declaration information, and create a class instance with a class identifier based on the class identifier in the declaration information. Among them, the declaration information includes a class that defines the properties and methods of the message. The class instance refers to a specific instance created according to the class identifier in the declaration information, which can implement the properties and methods of the message defined in the declaration information. Then, the first component can generate a second message object encapsulating the class instance according to a preset message format. Afterwards, the first component can call a message sending method (such as a setValue() method) to send the second message object to the message bus.

[0098] In actual applications, the message bus can optionally create different message channels based on different message objects published by different components. For example, Figure 4 As shown, in this case, the message publishing and subscription process is as follows:

[0099] Assuming that the first component as publisher 1 sends message object A to the message bus, the message bus can identify the class identifier A corresponding to the message object A and create a message channel corresponding to the class identifier A. And the second component as subscriber 1 can directly subscribe to the message object A from the message channel corresponding to the class identifier A in the message bus.

[0100] Similarly, the third component as publisher 2 can send message object B to the message bus, and the message bus can identify the class identifier B corresponding to the message object B, and create a message channel corresponding to the class identifier B. And the fourth component as subscriber 2 can directly subscribe to the message object B from the message channel corresponding to the class identifier B in the message bus.

[0101] After the message channel is created by using the second message object, further, when the first component sends the first message object to the message bus, the message bus may also determine whether the type of the first message object is a sticky message.

[0102] One case may be: if the first message object in the message channel is still stored in the message channel after being acquired by the second component, indicating that the first message object needs to be permanently stored, the message bus may determine that the type of the first message object is a sticky message. Furthermore, the message bus may not remove the sticky message to ensure that other subsequent components can quickly acquire the first message object from the message channel.

[0103] Another case may be: if the first message object in the message channel is removed from the message channel after being acquired by the second component, indicating that the first message object does not need to be permanently stored, then it is determined that the type of the first message object stored in the message channel between the first component and the second component is a non-sticky message. Furthermore, the message bus can remove non-sticky messages to reduce the memory usage in the message bus.

[0104] In this embodiment, the message bus can obtain the class identifier of the class instance, and use the class identifier as the channel identifier to create a message channel corresponding to the class identifier, so that the message object can be accurately transmitted from the message channel with the channel identifier. Further, the message bus can determine the type of the first message object stored in the message channel to accurately determine whether the first message object needs to be removed, so as to reduce the memory usage in the message bus as much as possible.

[0105] In addition, the contents not described in detail in this embodiment and the technical effects that can be achieved can also be found in the description of the above embodiments, and will not be repeated here.

[0106] The cache optimization device of one or more embodiments of the present invention will be described in detail below. Those skilled in the art will appreciate that these cache optimization devices can be configured using commercially available hardware components through the steps taught in this solution.

[0107] Figure 5 A schematic diagram of a cache optimization device provided by an embodiment of the present invention is shown in FIG. Figure 5 As shown, the device comprises:

[0108] The life cycle state determination module 11 is used to determine the life cycle state of the first component as a publisher by using the first observation class instance bound to the first monitoring class instance, and the first monitoring class instance corresponds to the first component; and to determine the life cycle state of the second component as a subscriber by using the second observation class instance bound to the second monitoring class instance, and the second monitoring class instance corresponds to the second component.

[0109] The target component determination module 12 is used to determine a target component in the first component and the second component that is in a destroyed state, and the target component includes the first component and / or the second component.

[0110] The removal module 13 is used to remove the observation class instance bound to the target monitoring class instance corresponding to the target component; if the removed observation class instance includes the first observation class instance and the second observation class instance, the message channel between the first component and the second component in the message bus is removed.

[0111] The first monitoring class instance notifies the first observing class instance of the life cycle status of the first monitoring class instance through a function call.

[0112] Optionally, the removal module 13 is also used to determine the type of message object stored in the message channel between the first component and the second component if the removed observation class instance includes a second observation class instance; if the type of the message object is a non-sticky message, remove the message object belonging to the non-sticky message.

[0113] Optionally, the apparatus further comprises an establishing module 14, configured to establish a binding relationship between the first monitoring class instance and the first observation class instance in response to the registration of the first observation class instance.

[0114] Figure 5 The device shown can perform Figure 1 to Figure 2 For the method of the embodiment shown in the figure, the part not described in detail in this embodiment can be referred to Figure 1 to Figure 2 The implementation process and technical effects of this technical solution refer to Figure 1 to Figure 2 The description in the illustrated embodiment will not be repeated here.

[0115] In a possible design, the structure of the cache optimization device can be implemented as an electronic device, such as Figure 6 As shown, the electronic device may include: a processor 21 and a memory 22. The memory 22 is used to store the electronic device to perform the above Figure 1 to Figure 2 The program of the cache optimization method provided in the illustrated embodiment, the processor 21 is configured to execute the program stored in the memory 22 .

[0116] The program includes one or more computer instructions, wherein the one or more computer instructions can implement the following steps when executed by the processor 21:

[0117] Determining a lifecycle state of a first component as a publisher using a first observation class instance bound to a first monitoring class instance, the first monitoring class instance corresponding to the first component;

[0118] Determining a lifecycle state of a second component as a subscriber using a second observation class instance bound to a second monitoring class instance, the second monitoring class instance corresponding to the second component;

[0119] Determine a target component in the first component and the second component that is in a destroyed state, wherein the target component includes the first component and / or the second component;

[0120] Remove the observation class instance bound to the target monitoring class instance corresponding to the target component;

[0121] If the removed observation class instances include the first observation class instance and the second observation class instance, then the message channel between the first component and the second component in the message bus is removed.

[0122] Optionally, the processor 21 is further configured to execute the aforementioned Figure 1 to Figure 2 All or part of the steps in the illustrated embodiments.

[0123] The structure of the electronic device may further include a communication interface 23 for the electronic device to communicate with other devices or a communication network.

[0124] In addition, an embodiment of the present invention provides a non-transitory machine-readable storage medium for storing computer software instructions used by the above electronic device, which includes instructions for executing the above Figure 1-2 The procedures involved in the cache optimization method shown.

[0125] In addition, an embodiment of the present invention provides a computer program product. The computer program product includes a computer program or an instruction. When the computer program or the instruction is executed by a processor, the processor is enabled to implement the above Figure 1-2 The steps or functions of the cache optimization method shown.

[0126] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A cache optimization method, characterized in that: Applied to a message bus, the message bus includes observation class instances corresponding one-to-one to components, the method includes: Determining a lifecycle state of a first component as a publisher using a first observation class instance bound to a first monitoring class instance, the first monitoring class instance corresponding to the first component; Determining a lifecycle state of a second component as a subscriber using a second observation class instance bound to a second monitoring class instance, the second monitoring class instance corresponding to the second component; Determine a target component in the first component and the second component that is in a destroyed state, wherein the target component includes the first component and / or the second component; Remove the observation class instance bound to the target monitoring class instance corresponding to the target component; If the removed observation class instances include the first observation class instance and the second observation class instance, then the message channel between the first component and the second component in the message bus is removed.

2. The method according to claim 1, characterized in that The method further comprises: If the removed observation class instances include the first observation class instance, determining the type of the message object stored in the message channel between the first component and the second component; If the type of the message object is a non-sticky message, the message object belonging to the non-sticky message is removed.

3. The method according to claim 1, characterized in that The first monitoring class instance notifies the first observing class instance of the life cycle status of the first monitoring class instance through a function call.

4. The method according to claim 1, characterized in that: The method further comprises: In response to the registration of the first observation class instance, a binding relationship between the first monitoring class instance and the first observation class instance is established.

5. A cache optimization system, characterized in that: include: A message bus, a first component as a publisher, and a second component as a subscriber; The first component is used to obtain a first monitoring class instance corresponding to the first component; and send the first monitoring class instance to the message bus; The second component is used to obtain a second monitoring class instance corresponding to the second component; Sending the second monitoring class instance to the message bus; The message bus is used to determine the life cycle state of the first component by using the first observation class instance bound to the first monitoring class instance; Using the second observation class instance bound to the second monitoring class instance, determine the life cycle status of the second component; determine the target component in the first component and the second component that is in a destroyed state; remove the observation class instance bound to the target monitoring class instance corresponding to the target component; if the removed observation class instances include the first observation class instance and the second observation class instance, remove the message channel between the first component and the second component in the message bus.

6. The system according to claim 5, characterized in that The message bus is used to determine the type of the first message object stored in the message channel between the first component and the second component if the removed observation class instance includes the first observation class instance; if the type of the first message object is a non-sticky message, remove the first message object belonging to the non-sticky message; if the type of the first message object is a sticky message, do not remove the first message object belonging to the sticky message.

7. The system according to claim 6, characterized in that The first component is used to create a class instance with a class identifier; generate a second message object encapsulating the class instance; send the second message object to the message bus, and the sending of the second message object is earlier than the sending of the first message object; The message bus is used to obtain the class identifier of the class instance; use the class identifier as a channel identifier, and create a message channel corresponding to the class identifier; Storing the second message object in the message channel; If the first message object in the message channel is stored in the message channel after being acquired by the second component, it is determined that the type of the first message object is a sticky message.

8. The system according to claim 7, characterized in that The message bus is used to determine that the type of the first message object stored in the message channel between the first component and the second component is a non-sticky message if the first message object is removed from the message channel after the message object in the message channel is acquired by the second component.

9. An electronic device, characterized in that: include: A memory and a processor; wherein the memory stores executable code, and when the executable code is executed by the processor, the processor executes the cache optimization method according to any one of claims 1 to 4.

10. A non-transitory machine-readable storage medium, characterized in that: The non-transitory machine-readable storage medium stores executable code, and when the executable code is executed by a processor of an electronic device, the processor is caused to execute the cache optimization method according to any one of claims 1 to 4.