Cache cleaning method, device and equipment
By using a time-wheel mechanism to store and clear cached data in a distributed system, the problem of high time complexity in cache clearing is solved, achieving efficient and timely cache cleanup and improving system performance.
Patent Information
- Application Number
- CN202511625908.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-07
- Publication Date
- 2026-03-17
AI Technical Summary
Existing cache clearing methods have high time complexity, especially in large-scale distributed systems, which can significantly impact system performance. Furthermore, existing methods cannot clear expired caches in a timely manner, which may lead to cache overflow.
A time wheel mechanism is used to store cached data in different slots of the time wheel according to the expiration time. When the time wheel pointer reaches the corresponding slot, the cached data is automatically cleared, reducing the need to traverse all cached data.
By using a time wheel mechanism, the time complexity of the cache cleanup process is reduced, the cache cleanup efficiency is improved, expired data is cleared in a timely manner, cache overflow is avoided, and system performance is improved.
Smart Images

Figure CN121681418A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of distributed systems technology, and in particular to a cache cleanup method, apparatus, and device. Background Technology
[0002] Caching is a widely used technique in computer systems. Cached data is typically cleared or updated periodically to release limited cache space or maintain data consistency. However, current cache clearing methods often suffer from high time complexity. Summary of the Invention
[0003] Based on the background technology, this invention proposes a cache cleaning method, apparatus, and device.
[0004] In a first aspect, the present invention provides a cache cleanup method applied to a distributed system, the method comprising: Initialize the time wheel, which includes multiple slots, the number of which corresponds to the period of the time wheel; Obtain the expiration time of multiple cached data items, and obtain the current timestamp of the slot to which the time pointer of the time wheel is located; Based on the expiration time and the current timestamp, the multiple cached data are stored in their respective slots; Run the time wheel so that when the time pointer of the time wheel reaches each slot of the time wheel, clear the cached data stored in that slot.
[0005] Optionally, the system includes multiple layers of time wheels, with different layers corresponding to different periodic times; the step of storing multiple cached data into corresponding slots based on the expiration time and the current timestamp includes: Obtain the time difference between the current timestamp and the expiration time, and compare the time difference with the cycle time of each layer of the time wheel; wherein, the current timestamp is the same as the timestamp of the slot currently pointed to by the time pointer of the time wheel with the shortest cycle among the multiple layers of time wheels; Based on the size relationship, the target time wheel corresponding to the cached data is determined in the multiple time wheels; Based on the time difference and the slot currently occupied by the target time wheel, the storage slot corresponding to the cached data in the target time wheel is determined, and the cached data is stored in the storage slot.
[0006] Optionally, the system includes multiple time wheels with the same period, where the current timestamp is the timestamp of the slot reached by the time pointer of the first time wheel currently to be stored; the step of storing the multiple cached data into their respective slots based on the expiration time and the current timestamp includes: Based on the time difference between the current timestamp and the expiration time, and the slot currently being run by the first time wheel, determine the first slot corresponding to the cached data in the first time wheel; If the amount of data in the first slot is greater than the preset amount of data, the cached data will be stored in the second slot of the second time wheel; Wherein, the second time wheel is any time wheel other than the first time wheel, and the position of the first slot in the first time wheel is the same as the position of the second slot in the second time wheel.
[0007] Optionally, the method includes multiple time wheels, where the cycle time of the current time wheel is the same as the time it takes for the next time wheel to complete one slot; the method further includes: After the first cached data in multiple slots of the current time wheel is cleared, the expiration time of the second cached data corresponding to the first slot in the next time wheel of the current time wheel is obtained; wherein, the current time wheel is any time wheel in the multiple time wheels; Based on the expiration time of each second cached data and the timestamp of the slot reached by the time pointer of the current layer time wheel, multiple second cached data are migrated to multiple slots of the current layer time wheel.
[0008] Optionally, before migrating multiple sets of second cached data to multiple slots of the current layer time wheel based on the expiration time of each set of second cached data and the timestamp of the slot reached by the time pointer of the current layer time wheel, the method further includes: Determine the degree of dispersion among the expiration times of multiple second cached data; Based on the degree of dispersion, the unit time of the current layer time wheel is adjusted to adjust the number of slots in the current layer time wheel, so that the amount of cached data stored in multiple slots of the current layer time wheel is balanced; wherein, the unit time represents the time required for the time wheel to run one slot.
[0009] Optionally, adjusting the unit time of the current layer time wheel based on the degree of dispersion includes: If the degree of dispersion is less than a preset degree of dispersion, the unit time of the current layer time wheel is reduced to increase the number of slots in the current layer time wheel.
[0010] Optionally, after migrating multiple pieces of the second cached data to multiple slots of the current layer time wheel based on the expiration time of each piece of the second cached data and the timestamp of the slot reached by the time pointer of the current layer time wheel, the method further includes: Compare the differences in the amount of data stored in multiple slots of the current layer time wheel; If there is a slot with a gap greater than a preset threshold, then at least one candidate time wheel is constructed, and the time period of the candidate time wheel is the same as the period of the current layer time wheel. A portion of the second cached data is migrated to at least one of the candidate time wheels, and the current time wheel and the candidate time wheel are run synchronously; wherein the slots where the cached data is located before and after the migration are corresponding.
[0011] Optionally, each cached data item carries a cache name, and the step of running the time wheel to clear the cached data in each slot when the time pointer of the time wheel reaches that slot includes: When the time wheel reaches the target slot, the linked list included in the target slot is traversed, and the linked list includes multiple cache names; For each cache name, determine the target cache data corresponding to the cache name, and clear the target cache data.
[0012] A second aspect of the present invention provides a cache cleanup device applied to a distributed system, the device comprising: An initialization module is used to initialize a time wheel, which includes multiple slots, the number of which corresponds to the period of the time wheel. The acquisition module is used to acquire the expiration time of multiple cached data, and to acquire the current timestamp of the slot to which the time pointer of the time wheel is running; The storage module is used to store multiple cache names into corresponding slots based on the expiration time and the current timestamp; The cleanup module is used to run the time wheel so that when the time pointer of the time wheel runs to each slot of the time wheel, the cached data stored in that slot is cleared.
[0013] A third aspect of the present invention provides an electronic device including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executed, implements the steps of the cache cleanup method as described in the first aspect.
[0014] The cache cleanup method provided by this invention includes: initializing a time wheel, the time wheel including multiple slots, the number of slots corresponding to the period of the time wheel; obtaining the expiration time of multiple cached data, and obtaining the current timestamp of the slot to which the time pointer of the time wheel is running; storing the multiple cached data into corresponding slots based on the expiration time and the current timestamp; running the time wheel so that when the time pointer of the time wheel runs to each slot of the time wheel, the cached data stored in that slot is cleared; Therefore, this invention stores multiple cached data into multiple slots of the time wheel based on the expiration time of the cached data and the current time represented by the time wheel's time pointer. When the time wheel's time pointer reaches each slot, the cached data stored in that slot can be cleared. In this way, when clearing the cache, it is not necessary to traverse each cached data; the cached data in the corresponding slot of the time wheel can be cleared, thus reducing the time complexity of the cache clearing process.
[0015] The above description is merely an overview of the technical solution of the present invention. In order to better understand the technical means of the present invention and to implement it in accordance with the contents of the specification, and in order to make the above and other objects, features and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described below. Attached Figure Description
[0016] To more clearly illustrate the technical solutions in the embodiments of the present invention or related technologies, the accompanying drawings used in the description of the embodiments or related technologies will be briefly introduced below. Obviously, the accompanying drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. It should be noted that the scale in the drawings is for illustration only and does not represent the actual scale.
[0017] Figure 1 The flowchart of the cache cleanup method provided in the embodiment of the present invention is shown; Figure 2 A schematic diagram of a time wheel according to an embodiment of the present invention is shown; Figure 3 A schematic diagram of another time wheel structure in an embodiment of the present invention is shown; Figure 4 A schematic diagram of the linked list stored in each slot of the time wheel in an embodiment of the present invention is shown; Figure 5 A schematic diagram of the cache cleanup device provided in an embodiment of the present invention is shown. Detailed Implementation
[0018] To make the above-mentioned objects, features, and advantages of the present invention more apparent and understandable, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0019] Caching is a widely used technique in computer systems, primarily belonging to the field of computer science. Because of the differences in the properties and transmission channels of various computer hardware components, the response time for service requests depends on the slowest action. Typically, to avoid this impacting overall service performance, a caching layer is added between fast and slow devices to improve service performance. Common caching techniques include: L1, L2, and L3 caches in CPUs; Redis and Memcached commonly used in distributed systems; and even CDNs can be considered a type of caching.
[0020] A distributed system is a system in which multiple devices (also known as multiple nodes) work together through network connections to provide business functions. In a distributed system, data is usually not centralized in one place, but distributed across devices in different locations. To improve the efficiency and performance of data access, distributed systems typically employ caching techniques to enhance data access performance. This way, when an access request is received, the corresponding data can be retrieved directly from the cache. However, cached data consumes storage space in the distributed system; therefore, cached data is generally periodically cleared to promptly release limited cache space.
[0021] There are usually several ways to clear a cache: (1) Get all the cache keys, iterate through each cache, and if the cache has expired, clear it. The time complexity of this method is O(n). (2) When a cache is retrieved, determine whether it has expired. If it has expired, clear it. The time complexity of this method is O(1). (3) Set a cache strategy, such as the LRU (Least Recently Used) algorithm, and evict the least recently used cache. Depending on the implementation, the time complexity may vary, but it is usually O(1). The O(1) time complexity method is the highest performance cache clearing method. However, methods (2) and (3) have their own problems. In method (2), if the system does not access a certain cache key, the cache may not be cleared in time even if it has expired, which may lead to a cache overflow. Method (3) itself requires a certain implementation complexity. In addition, the LRU algorithm may clear caches that it does not want to be cleared. It also has a certain triggering mechanism, such as the memory already used must be greater than a certain threshold before it starts working.
[0022] In large-scale distributed application systems, cached keys are often extremely large, ranging from 1 million to 10 million. Regularly clearing these cached keys requires a method to promptly and accurately clear expired cache, thereby cleaning up data, releasing memory, and ensuring data consistency. However, if a polling method is used, its O(n) time complexity will significantly impact system performance. Therefore, an algorithm with near-O(1) time complexity is needed to clean up expired cache in real time.
[0023] In view of this, the present invention provides a cache clearing method, apparatus and device, which stores cache data into different slots of a time wheel according to the expiration time. When the time pointer of the time wheel runs to the corresponding slot, the cache data stored in that slot is cleared. In this way, each time the cache is cleared, only the data stored in the corresponding slot needs to be cleared, without having to traverse every cache data, which reduces the time complexity of cache clearing and improves the efficiency of cache clearing.
[0024] Reference Figure 1 , Figure 1 The flowchart illustrating the steps of the cache cleanup method provided in this embodiment of the invention is shown below. Figure 1 As shown, this cache cleanup method is applied to a distributed system, and the cache cleanup method includes: Step S101: Initialize the time wheel.
[0025] The time wheel comprises multiple slots, the number of which corresponds to the time wheel's cycle. For example, the time wheel's cycle can be 100ms, 60s, 60min, 60h, etc. Taking a 60s cycle as an example, the time wheel can include 60 slots, each representing 1 second. When initializing the time wheel, the time unit, cycle, and number of layers can be set based on the typical cached data volume and expiration times in the distributed system. For instance, if the expiration times of cached data in the distributed system are relatively evenly distributed within each second, the time unit of the time wheel can be set to seconds (1 second per slot), and the time wheel's cycle time to be 60 seconds, meaning the time wheel includes 60 slots. If the expiration times include cached data expiring within seconds and cached data expiring within minutes, two layers of time wheels can be set: the first layer with a cycle time of 60 seconds and the second layer with a cycle time of 60 minutes, allowing the time wheel to store multiple cached data with different expiration times.
[0026] Initializing a time wheel can involve initializing one or multiple time wheels. The time wheel can have multiple layers, each with a different period. (Refer to...) Figure 2 , Figure 2 A schematic diagram of a multi-layered time wheel is shown. A multi-layered time wheel can include two, three, or more layers, which can be specifically configured based on the expiration time of the cached data; Figure 2 For example, a multi-layer time wheel consists of four layers, arranged in descending order of time period. The time period of each layer corresponds to the time of a slot in the next layer. For instance, the first layer has a time period of 60 seconds, with each slot representing 1 second; the second layer has a time period of 60 minutes, with each slot representing 1 minute (60 seconds total). Each layer includes a time pointer, which determines the cached data to be cleared based on the slot it points to. Essentially, the multi-layer time wheel operates like a clock; after the current layer completes one cycle, the time pointer of the next layer moves to a new slot, thus clearing cached data with different expiration times. Multiple time wheels can be used, and they can be identical and run synchronously. This allows multiple cached data with the same expiration time to be stored in different slots of different time wheels, minimizing the amount of cached data stored in each time wheel and improving the efficiency of cache clearing.
[0027] The structure of the time wheel is shown in Table 1 below: Table 1 Time Wheel Structure Definition
[0028] The structure of each slot in the time wheel can be shown in Table 2 below: Table 2 Definition of Time Wheel Groove Structure
[0029] Step S102: Obtain the expiration time of multiple cached data, and obtain the current timestamp of the slot to which the time pointer of the time wheel is running.
[0030] Step S103: Based on the expiration time and the current timestamp, store multiple cached data into their respective slots.
[0031] In this embodiment, each cached data needs to be stored in a corresponding slot of the time wheel. When the time wheel's pointer reaches each slot, the cached data in that slot is cleared. Therefore, when storing cached data, it's crucial to ensure that the time when the time wheel's pointer reaches a particular slot is exactly the expiration time of the cached data stored in that slot. Thus, when storing cached data, the current timestamp of the slot reached by the time wheel's pointer and the expiration time of the cached data can be determined, thereby identifying the appropriate slot for storing the cached data. It's important to note that cached data storage actually occurs simultaneously with the time wheel's operation. That is, during the time wheel's operation, the current timestamp of the slot reached by the time wheel's pointer is constantly changing. Therefore, each time cached data is stored, the current timestamp needs to be retrieved to ensure its accuracy.
[0032] Specifically, storing cached data based on expiration and current times involves determining the time difference between the expiration time and the current timestamp, and then using the ratio of this time difference to the unit time of the time wheel to determine the number of slots between the slot currently occupied by the time pointer and the slot where the cached data is stored. This gives the specific slot where the cached data is stored. For example, with a time wheel cycle of 60 seconds, a current timestamp of 1729171017, and a cached data expiration time of 1729171027, the expiration time of the cached data is determined to be 10 seconds greater than the current timestamp. Therefore, the cached data can be stored in the tenth slot after the slot currently occupied by the time wheel's time pointer. After the time wheel's time pointer continues running for another 10 seconds, the cached data is deleted, thus clearing the cached data upon expiration.
[0033] Understandably, in the case of multiple time wheels, each time wheel includes a time pointer. Therefore, it is necessary to first determine the time wheel to which the cached data belongs, and then determine the slot where the cached data is stored. In the case of multiple time wheels, a preset value can be set for the amount of data that each slot of the time wheel can store. The cached data is stored sequentially. When the amount of data in the slot of the current time wheel to be stored reaches the preset value, the cached data is stored in another time wheel, ensuring that the amount of data in the slot of each time wheel is not too large.
[0034] The cache data structure stored in the slots of the time wheel can be shown in Table 3 below: Table 3. Definition of cached data structure in slots
[0035] As shown in Table 3, the cached data in each slot of the time wheel is stored in the form of a linked list, which makes it convenient to traverse the linked list to clear the corresponding cached data when clearing. Using the linked list format can avoid omissions when clearing cached data.
[0036] Step S104: Run the time wheel so that when the time pointer of the time wheel reaches each slot of the time wheel, the cached data stored in that slot is cleared.
[0037] Specifically, the time wheel can be run directly after initialization. During the time wheel's operation, multiple cached data items are stored. Since each cached data storage process is determined based on the cached data's expiration time and the current time of the slot to which the time wheel is currently running, it ensures that the cached data is stored in the corresponding slot, and thus can be cleared when the cached data expires.
[0038] In this embodiment, during the operation of the time wheel, each time the time pointer of the time wheel moves to a slot, the cached data stored in that slot is retrieved and the cached data is cleared. In this way, each clearing process only needs to read the cached data stored in the slot, without having to traverse all the cached data.
[0039] The cache cleanup method provided in this embodiment of the invention uses a time wheel to store multiple cache data. When the time pointer of the time wheel runs to each slot, the multiple cache data in that slot are cleared. In this way, when clearing the cache, it is not necessary to traverse each cache data to determine the expiration time, thus improving the efficiency of cache cleanup.
[0040] In one embodiment, when the time wheel includes multiple layers, each layer of the time wheel includes a time pointer. If it's impossible to determine which time pointer's position determines the slot for cached data, it's necessary to first determine which layer of the time wheel the cached data belongs to, and then determine the storage slot for the cached data. Specifically, when there are multiple layers of time wheels, each corresponding to a different period, the steps for storing cached data can be as follows: First, obtain the time difference between the current timestamp and the expiration time, and compare the time difference with the period of each layer of the time wheel. The current timestamp is the same as the timestamp of the slot currently pointed to by the time pointer of the time wheel with the shortest period among the multiple time wheels. Next, based on the size relationship, determine the target time wheel corresponding to the cached data among the multiple time wheels. Then, based on the time difference and the slot currently being run by the target time wheel, determine the storage slot corresponding to the cached data in the target time wheel, and store the cached data in the storage slot.
[0041] Specifically, after determining the time difference, the time difference can be compared with the cycle time of the multi-layer time wheel. Considering that the operation of the multi-layer time wheel has a sequential relationship, the comparison can be made in order according to the operation relationship of the multi-layer time wheel. For example, the cycle time of the multi-layer time wheel can be compared with the time difference in order of increasing cycle time, and the time wheel with a cycle time greater than the time difference can be determined as the target time wheel.
[0042] Alternatively, the time difference can be compared with the period of each time wheel. The time wheel whose period is greater than the time difference and whose period of the adjacent time wheel is less than the time difference is identified as the target time wheel. For example, if there are three time wheels, the period of the first, second, and third time wheels can all be compared with the time difference. If the period of the first time wheel is greater than the time difference, then the first time wheel is identified as the target time wheel. If the period of the second time wheel is greater than the time difference and the period of the first time wheel is less than the time difference, then the second time wheel is identified as the target time wheel.
[0043] After determining the target time wheel, the storage slot for cached data is determined based on the slot currently occupied by the time pointer and the time difference. This ensures that the cached data is stored in the corresponding slot and promptly cleared upon expiration. It's important to note that the time pointer on the time wheel moves clockwise, similar to the hands of a clock. Therefore, determining the storage slot for cached data actually involves identifying which slot follows the current time pointer position in a clockwise direction.
[0044] For example, refer to Figure 3 , Figure 3The diagram illustrates a multi-layered time wheel according to an embodiment of the present invention, as shown below. Figure 3 As shown, it includes four time wheels. The first time wheel has a period of 60 seconds, the second time wheel has a period of 60 minutes, the third time wheel has a period of 24 hours, and the fourth time wheel has a period of 30 days. Figure 3 For example, if the current time is 1729171017 and the expiration time of the cached data is 1729171087, the time difference between the expiration time of the cached data and the current time is determined to be 70s. This is greater than the cycle time of the first time wheel and less than the cycle time of the second time wheel. Therefore, the cached data is located in the second time wheel. Then, the ratio of this time difference to the time unit of a slot can be determined, that is, the ratio of 70s to 60s. If this ratio is greater than 1 and less than 2, it means that the cached data is located in the first slot after the slot reached by the current time pointer.
[0045] Understandably, multiple time wheels can be initialized, each with the same period. Each time wheel can have multiple layers. When storing cached data, multiple time wheels can be used sequentially. For example, if the data volume in the slot of the current time wheel to be stored is large, the cached data can be stored in another time wheel to ensure that the amount of cached data in each time wheel's slot is not too large, avoiding the situation where too much cached data is processed simultaneously, thus reducing efficiency. Specifically, when there are multiple time wheels with the same period, and the current timestamp is the timestamp of the slot reached by the time pointer of the first time wheel to be stored, the specific steps for storing cached data include: First, determining the first slot of the cached data in the first time wheel based on the time difference between the current timestamp and the expiration time, and the slot currently reached by the first time wheel; then, if the data volume in the first slot is greater than a preset data volume, storing the cached data in the second slot of the second time wheel; where the second time wheel is any time wheel other than the first time wheel, and the position of the first slot in the first time wheel is the same as the position of the second slot in the second time wheel.
[0046] In this embodiment, when the amount of cached data is large and the expiration time is relatively concentrated, multiple time wheels can be obtained when initializing the time wheel. The multiple time wheels are completely identical and run synchronously. That is, the cached data stored in the slots with the same relative position in the multiple time wheels are cleared as parallel tasks, but the amount of cached data cleared in each task will not increase, thereby reducing the time complexity of the cache clearing process.
[0047] When storing cached data, one of multiple time wheels can be designated as the first time wheel to be stored, and the remaining time wheels as the second time wheels. After determining the first slot for storing cached data, the amount of data in that slot is obtained. If the amount of data is greater than a preset amount, it indicates that the cached data stored in the first time wheel has reached its limit. In this case, the cached data is stored in the second slot of the second time wheel. Since the second time wheel runs synchronously with the first time wheel, there is no need to reconfirm the storage slot of the cached data in the second time wheel. The second slot can be determined directly based on the first slot. For example, if the storage slot of the cached data is determined to be the third slot after the slot reached by the time pointer of the first time wheel based on the time difference, then when the amount of data in the first slot is greater than the preset amount, the cached data is stored in the third slot after the slot reached by the time pointer of the second time wheel.
[0048] In one embodiment, if multiple time wheels are included, and the cycle time of the current time wheel is the same as the time it takes for the next time wheel to run one slot, then after the cached data in multiple slots of the current time wheel is cleared, the cached data in one slot of the next time wheel can be migrated to multiple slots of the current time wheel. When using time wheels for clearing, only the cached data in multiple slots of the first time wheel is actually cleared. This ensures that the amount of cached data in each clearing process is not too large, avoiding increased time complexity. Specifically, this process can be as follows: after the first cached data in multiple slots of the current time wheel is cleared, the expiration time of the second cached data corresponding to the first slot in the next time wheel is obtained; where the current time wheel is any time wheel in the multiple time wheels; then, based on the expiration time of each second cached data and the timestamp of the slot reached by the time pointer of the current time wheel, multiple second cached data are migrated to multiple slots of the current time wheel.
[0049] Specifically, with Figure 3Taking the time wheel shown as an example, after all the cached data in the multiple slots of the first-level time wheel has been cleared, the cached data in the slot that the second-level time wheel has reached can be migrated to the multiple slots of the first-level time wheel according to the time pointer of the second-level time wheel. At this time, the time difference between the expiration time of the multiple cached data in the slot indicated by the time pointer of the second-level time wheel and the current time, as well as the slot that the current-level time wheel has reached at the current time, can be used to determine the slot corresponding to each cached data in the current-level time wheel. Then, multiple cached data can be migrated to their corresponding slots to facilitate the cache clearing steps. Similarly, after the cached data in the second-level time wheel has been cleared, the cached data in the slot that the time pointer of the third-level time wheel has reached can be migrated to the second-level time wheel, and the cached data in the slot that the time pointer of the second-level time wheel has reached can be migrated to the first-level time wheel. The cached data clearing can then be performed according to the slot that the pointer of the first-level time wheel has reached. This allows for the division of large amounts of cached data according to their expiration time, enabling the clearing of cached data with the corresponding expiration time without having to traverse all cached data, thus improving the efficiency of cache data cleanup.
[0050] It is understandable that if the expiration times of cached data in a slot within the next time wheel are concentrated, the amount of cached data in a slot may be large during the clearing process, which will also increase the time complexity. Therefore, during each migration, the time wheel can be optimized based on the expiration time distribution of the cached data in the slots to be migrated, or based on the difference in data volume between multiple slots in the current time wheel after migration, to ensure that the amount of cached data in a slot of the current time wheel is not too large.
[0051] In one example, the distribution of expiration times in the cached data to be migrated can be used to determine if there is a concentration of expiration times. This helps determine if the amount of data in a single slot within the current time wheel is too large after migration. If such a situation exists, the time unit of the current time wheel can be adjusted to increase the number of slots in the time wheel, thereby reducing the amount of cached data in a single slot. Then, multiple second cached data items can be migrated to multiple slots in the current time wheel. Specifically, firstly, the dispersion between the expiration times of the multiple second cached data items can be determined. Then, based on the dispersion, the unit time of the current time wheel can be adjusted to adjust the number of slots in the current time wheel, so that the amount of cached data stored in multiple slots of the current time wheel is balanced. Here, unit time represents the time required for the time wheel to run one slot.
[0052] Specifically, determining the dispersion of expiration times among multiple second-level cached data sets reveals whether their expiration times are concentrated or discrete. Low dispersion indicates concentrated expiration times, potentially leading to a large data volume in a single slot of the current time wheel. High dispersion indicates more dispersed expiration times, meaning smaller differences in data volume across slots within the current time wheel. In this case, the unit time of the current time wheel can be adjusted based on the dispersion. For example, with low dispersion, decreasing the unit time and increasing the number of slots reduces the data volume in a single slot. Conversely, with high dispersion, increasing the unit time and decreasing the number of slots avoids empty slots. Alternatively, if high dispersion occurs, no adjustment to the time wheel may be necessary, ensuring a small data volume within the current time wheel.
[0053] In the case of adjusting the unit time of the current layer time wheel according to the degree of dispersion, a preset degree of dispersion can be set in advance. The preset degree of dispersion is the degree of dispersion in which the expiration time distribution of multiple second cache data is relatively concentrated, and it is easy to have a large amount of data in a single slot of the time wheel. In this case, if the degree of dispersion is less than the preset degree of dispersion, the unit time of the current layer time wheel is reduced to increase the number of slots in the current layer time wheel.
[0054] In this embodiment, the extent to which the unit time of the current layer time wheel is reduced can be determined based on the specific degree of dispersion. That is, the lower the degree of dispersion, the smaller the unit time of the current layer time wheel, which means the larger the number of slots in the current layer time wheel. This is to ensure that the amount of cached data stored in each slot of the current layer time wheel is small, thus avoiding the situation where the amount of data in a single slot is too large, which would increase the time complexity.
[0055] In another example, after migrating multiple second cache data to multiple slots in the current layer time wheel, it can be determined whether the difference in the amount of cache data stored in the multiple slots is large. If the difference is large, it indicates that there may be a single slot with a large amount of data, which can easily increase time complexity and requires optimization of the time wheel. In this case, the difference in the amount of cache data stored in multiple slots in the current layer time wheel can be compared first. If there is a slot with a difference greater than a preset threshold, at least one candidate time wheel is constructed, and the period of the candidate time wheel is the same as the period of the current layer time wheel. Then, some of the second cache data is migrated to at least one candidate time wheel, and the current layer time wheel and the candidate time wheel are run synchronously. The slots where the second cache data is located before and after the migration are corresponding.
[0056] In this embodiment, after migrating multiple second cache data to multiple slots of the current layer time wheel, it can be determined whether there is a single slot with a large data volume based on the difference between the data volume of multiple slots. For example, the data volume of each slot on the current layer time wheel can be compared with the data volume of other slots to determine whether there is a slot with a large difference between the data volume of other slots. If so, a new time wheel, i.e. a candidate time wheel, can be constructed, and some of the second cache data can be migrated to the candidate time wheel so that the data volume of cache data in a single slot is not too large.
[0057] The number of candidate time wheels can be determined based on the amount of data. If the amount of data in a single slot is too large, and the amount of data in a single slot is still large when another candidate time wheel is built, then two, three or even more candidate time wheels can be built. The time pointer position of the candidate time wheel is the same as that of the current layer time wheel so that the current layer time wheel and the candidate time wheel run synchronously.
[0058] It is understandable that when migrating some of the second cache data to at least one candidate time wheel, the position of the slot where the second cache data was located before the migration relative to the current time wheel is equivalent to the position of the slot where the second cache data was located after the migration relative to the candidate time wheel. This is so that during synchronous operation, the cache data located in the current time wheel and the cache data in the candidate time wheel with the same expiration time as the cache data in the current time wheel can be cleaned up at the same time.
[0059] Specifically, based on the data volume of each slot, or the difference in data volume between multiple slots, the cached data portion of the slot with a larger data volume can be migrated to the candidate time wheel. This ensures that the data volume difference within each slot in the current time wheel is not significant, and the data volume within each slot is relatively small, thus not increasing the time complexity.
[0060] In any of the above embodiments, cached data can be stored in the form of a linked list. When clearing cached data in each slot, the cache name included in the linked list is read, and the relevant cache information is cleared according to the cache name. That is, each cached data carries a cache name. When the time wheel is running, when the time wheel reaches the target slot, the linked list included in the target slot is traversed. The linked list includes the cache name. For each cache name, the target cached data corresponding to the cache name is determined, and the target cached data is cleared.
[0061] In this embodiment, the cache structure definition can be as shown in Table 4 below: Table 4. Cache Structure Definition
[0062] In this case, refer to Figure 4 , Figure 4 A schematic diagram of the linked list included in each slot in an embodiment of the present invention is shown, such as... Figure 4 As shown, the cached data in each slot of the time wheel can be stored in the form of a linked list. When the time wheel runs to a certain slot, the linked list in that slot can be traversed to obtain the cache name, and then the relevant cache information can be cleared according to the cache name.
[0063] The cache cleanup method provided in this invention uses a time wheel to store multiple cached data. When the time pointer of the time wheel reaches each slot, the multiple cached data in that slot are cleared. In this way, it is not necessary to traverse each cached data to determine its expiration time during cache cleanup, thus improving the efficiency of cache cleanup. Furthermore, when the amount of data in a single slot of the time wheel is large, the time unit of the time wheel can be adjusted, or multiple time wheels can be constructed to control the amount of data in each slot within a single time wheel, ensuring that the amount of cached data does not become too large during cache cleanup, thereby increasing time complexity. In addition, the time wheel can run automatically after initialization without triggering conditions, avoiding cache overflow.
[0064] The cache cleanup method provided in this embodiment of the invention will be described below with reference to a specific example: by Figure 2 For example, initialization yields four time wheels. The first time wheel has a cycle time of 60 seconds, and the time to run one slot is 1 second. The first time wheel includes 60 slots. The second time wheel has a cycle time of 60 minutes, and the time to run one slot is 1 minute. The second time wheel includes 60 slots. The third time wheel has a cycle time of 60 hours, and the time to run one slot is 1 hour. The third time wheel includes 60 slots. The fourth time wheel has a cycle time of 150 days, and the time to run one slot is 60 hours. The fourth time wheel includes 60 slots.
[0065] If the current timestamp is 1729171017, the pointers of both the first and second time wheels point to slot 1. The expiration time of the stored cached data is 1729171027, and the difference between the expiration time and the current timestamp is 10 seconds. Therefore, the cached data is stored in slot 10. Simultaneously, another cached data item has an expiration time of 1729171087, and the time difference between the expiration time and the current timestamp is 70 seconds. First, the time difference is compared to the cycle time of each time wheel. If it is greater than the cycle time of the first time wheel but less than the cycle time of the second time wheel, the target time wheel for this cached data is determined to be the second time wheel. Then, the ratio of the time difference to the unit time of the second time wheel is determined. This unit time is the time required for the second time wheel to complete one slot. If the ratio is greater than 1 and less than 2, the cached data can be stored in slot 1 of the second time wheel.
[0066] After storing multiple cached data entries, the cached data stored in multiple slots is cleared sequentially as the time wheel runs. Specifically, when the time pointer of the first-level time wheel reaches slot 1 (slot_index=1), the linked list within slot 1 is retrieved, and each cache key is traversed to obtain it. The relevant cache information is then cleared based on the cache key. Afterward, the slot of the first-level time wheel moves forward one position, i.e., slot_index = slot_index + 1, and the cached data in multiple slots of the first-level time wheel is cleared in this manner. After the slot of the first-level time wheel moves forward 60 times, i.e., slot_index = 61, it indicates that the first cached data in multiple slots of the first-level time wheel has been cleared. At this point, the second cached data in slot 1 of the second-level time wheel is migrated to multiple slots of the first-level time wheel. During the migration process, the storage slot for each second cached data entry is determined based on the expiration time and current timestamp of the multiple second cached data entries in slot 1. Simultaneously, the time pointer of the second-level time wheel moves to slot 2, i.e., slot_index = slot_index + 1. Similarly, after all the cached data in the multiple slots of the second-level time wheel has been cleared, the cached data in slot 1 of the third-level time wheel is migrated to the multiple slots of the second-level time wheel. The migration process is similar to the migration process described above.
[0067] In the case of migrating cached data from the next time wheel to the current time wheel, there may be a situation where the amount of cached data is too large and the expiration time is relatively concentrated. This can easily lead to an excessive amount of data in a single slot of the time wheel, increasing the time complexity.
[0068] Specifically, before migrating the cached data in slot 1 of the second-level time wheel to the first-level time wheel, it can be determined that the dispersion of the expiration times of multiple cached data is less than a preset dispersion. Then, the unit time of the first-level time wheel can be reduced. For example, if the unit time of the first-level time wheel is reduced from s to 100ms, the number of slots in the first-level time wheel can be increased to 600. In this way, slots with a large amount of data can be split into multiple slots, thereby reducing the amount of data in each slot and reducing time complexity.
[0069] Alternatively, after migrating the cached data from slot 1 of the second-level time wheel to the first-level time wheel, the differences in data volume between multiple slots can be compared. If a slot has a difference greater than a preset threshold, a candidate time wheel is constructed. This candidate time wheel has a cycle time of 60 seconds, runs one slot in 1 second, and includes 60 slots. The time pointer of the candidate time wheel also points to slot 1. Then, a portion of the cached data from the slots with larger data volumes can be stored in the candidate time wheel. It's important to note that the position of the first slot where the cached data was stored before migration in the first-level time wheel is the same as the position of the second slot where the cached data is stored after migration in the candidate time wheel. For example, if the cached data before migration is located in slot 3 of the first-level time wheel, then the cached data after migration will be located in slot 3 of the candidate time wheel. This way, when the first-level time wheel and the candidate time wheel run synchronously to slot 3, the cached data in both time wheels is cleared, ensuring that the corresponding cached data is cleared before its expiration time.
[0070] Based on the same inventive concept, embodiments of the present invention also provide a cache clearing device, referring to... Figure 5 , Figure 5 A schematic diagram of the cache cleanup device provided in an embodiment of the present invention is shown, as follows: Figure 5 As shown, this cache cleanup device is applied to a distributed system, and the cache cleanup device specifically includes: Initialization module 201 is used to initialize the time wheel, which includes multiple slots, the number of which corresponds to the period of the time wheel; The first acquisition module 202 is used to acquire the expiration time of multiple cached data, and to acquire the current timestamp of the slot to which the time pointer of the time wheel is running; Storage module 203 is used to store multiple cache names into corresponding slots based on expiration time and current timestamp; The cleanup module 204 is used to run the time wheel so that when the time pointer of the time wheel runs to each slot of the time wheel, the cached data stored in that slot is cleared.
[0071] In some embodiments, a multi-layer time wheel is included, with different layers of time wheels corresponding to different period times; the storage module 203 includes: The acquisition submodule is used to obtain the time difference between the current timestamp and the expiration time, and compare the time difference with the cycle time of each time wheel; wherein, the current timestamp is the same as the timestamp of the slot currently pointed to by the time pointer of the time wheel with the shortest cycle in the multi-layer time wheel; The first determination submodule is used to determine the target time wheel corresponding to the cached data in a multi-level time wheel based on the size relationship; The storage submodule is used to determine the corresponding storage slot in the target time wheel based on the time difference and the slot currently being run by the target time wheel, and to store the cached data in the storage slot.
[0072] In some embodiments, the system includes multiple time wheels with the same period, and the current timestamp is the timestamp of the slot reached by the time pointer of the first time wheel currently to be stored among the multiple time wheels; the storage module 203 further includes: The second determining submodule is used to determine the first slot corresponding to the cached data in the first time wheel based on the time difference between the current timestamp and the expiration time, and the slot currently being run by the first time wheel; If the amount of data in the first slot is greater than the preset amount of data, the cached data will be stored in the second slot of the second time wheel; wherein, the second time wheel is any time wheel other than the first time wheel, and the position of the first slot in the first time wheel is the same as the position of the second slot in the second time wheel.
[0073] In some embodiments, a multi-layer time wheel is included, wherein the period of the current time wheel is the same as the time to complete one slot in the next layer of the time wheel; the cache cleanup device further includes: The second acquisition module is used to acquire the expiration time of the second cached data corresponding to the first slot in the next time wheel after the first cached data in multiple slots in the current time wheel has been cleared; wherein, the current time wheel is any time wheel in the multi-layer time wheel; The first migration module is used to migrate multiple second cache data to multiple slots of the current layer time wheel based on the expiration time of each second cache data and the timestamp of the slot reached by the time pointer of the current layer time wheel.
[0074] In some embodiments, the cache cleanup device further includes: The determination module is used to determine the degree of dispersion among the expiration times of multiple second cached data; The adjustment module is used to adjust the unit time of the current layer time wheel based on the degree of dispersion, so as to adjust the number of slots of the current layer time wheel and make the amount of cached data stored in multiple slots of the current layer time wheel balanced; where unit time represents the time required for the time wheel to run one slot.
[0075] In some embodiments, the adjustment module includes: The adjustment submodule is used to reduce the unit time of the current layer time wheel when the dispersion is less than the preset dispersion, so as to increase the number of slots in the current layer time wheel.
[0076] In some embodiments, the cache cleanup device further includes: The comparison module is used to compare the differences between the amounts of cached data stored in multiple slots in the current time wheel; The module is used to construct at least one candidate time wheel if there is a slot with a gap greater than a preset threshold. The cycle time of the candidate time wheel is the same as that of the current layer time wheel. The second migration module is used to migrate a portion of the second cached data to at least one candidate time wheel and to run the current time wheel and the candidate time wheel synchronously; wherein the slots where the second cached data is located before and after the migration are corresponding.
[0077] In some embodiments, each cached data item carries a cache name, and the cleanup module 204 includes: The traversal module is used to traverse the linked list included in the target slot when the time wheel runs to the target slot. The linked list includes the cache name. The cleanup submodule is used to determine the target cache data corresponding to each cache name and clear the target cache data.
[0078] Based on the same inventive concept, embodiments of the present invention also provide an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the steps of the cache clearing method as described in any of the above embodiments.
[0079] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. The same or similar parts between the various embodiments can be referred to each other.
[0080] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0081] The cache clearing method, apparatus, and device provided by the present invention have been described in detail above. Specific examples have been used to illustrate the principle and implementation of the present invention. The description of the above embodiments is only for the purpose of helping to understand the method and core idea of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation and application scope based on the idea of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.
[0082] Other embodiments of the invention will readily occur to those skilled in the art upon consideration of the specification and practice of the invention described herein. This invention is intended to cover any variations, uses, or adaptations of the invention that follow the general principles of the invention and include common knowledge or customary techniques in the art not invented herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of the invention are indicated by the following claims.
[0083] It should be understood that the present invention is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of the invention is limited only by the appended claims.
[0084] The terms "an embodiment," "embodiment," or "one or more embodiments" as used herein mean that a particular feature, structure, or characteristic described in connection with an embodiment is included in at least one embodiment of the invention. Furthermore, please note that the examples of the phrase "in one embodiment" do not necessarily all refer to the same embodiment.
[0085] Numerous specific details are set forth in the specification provided herein. However, it will be understood that embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures, and techniques have not been shown in detail so as not to obscure the understanding of this specification.
[0086] In the claims, any reference signs placed between parentheses should not be construed as limiting the claims. The word "comprising" does not exclude the presence of elements or steps not listed in the claims. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The invention can be implemented by means of hardware comprising a plurality of different elements and by means of a suitably programmed computer. In a unit claim enumerating a plurality of means, several of these means may be embodied by the same item of hardware. The use of the words first, second, and third, etc., does not indicate any order. These words may be interpreted as names.
[0087] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A cache cleaning method, characterized by, The method is applied to a distributed system, and the method comprises: initializing a time wheel, the time wheel comprising a plurality of slots, the number of the slots corresponding to a period time of the time wheel; obtaining expiration times of a plurality of cache data, and obtaining a current time stamp of a slot to which a time pointer of the time wheel runs; storing the plurality of cache data into corresponding slots based on the expiration times and the current time stamp; running the time wheel, so that when the time pointer of the time wheel runs to each slot of the time wheel, the cache data stored in the slot is cleared.
2. The cache cleaning method of claim 1, wherein, The time wheel comprises a plurality of layers, and the time wheels of different layers correspond to different period times; The storing of the plurality of cache data into corresponding slots based on the expiration times and the current time stamp comprises: obtaining a time difference value between the current time stamp and the expiration time, and comparing a size relationship between the time difference value and a period time of each layer of the time wheel; wherein the current time stamp is the same as a time stamp of a slot currently pointed to by a time pointer of a time wheel with the shortest period time among the plurality of layers of the time wheel; determining a target time wheel corresponding to the cache data among the plurality of layers of the time wheel based on the size relationship; determining a storage slot corresponding to the cache data in the target time wheel according to the time difference value and a slot currently run to by the target time wheel, and storing the cache data into the storage slot.
3. The cache cleaning method of claim 1, wherein, The time wheel comprises a plurality of time wheels with the same period time, and the current time stamp is a time stamp of a slot currently run to by a time pointer of a first time wheel to be stored among the plurality of time wheels; The storing of the plurality of cache data into corresponding slots based on the expiration times and the current time stamp comprises: determining a first slot corresponding to the cache data in the first time wheel according to a time difference value between the current time stamp and the expiration time and a slot currently run to by the first time wheel; storing the cache data into a second slot of a second time wheel in a case where a data amount of the first slot is greater than a preset data amount; wherein the second time wheel is any time wheel except the first time wheel, and a position of the first slot in the first time wheel is the same as a position of the second slot in the second time wheel.
4. The cache cleaning method of claim 1, wherein, The time wheel comprises a plurality of layers, a period time of a current layer of the time wheel is the same as a time for running one slot in a next layer of the time wheel of the current layer; and the method further comprises: after first cache data of a plurality of slots in the current layer of the time wheel is cleaned, obtaining expiration times of second cache data corresponding to first slots in a next layer of the time wheel of the current layer; wherein the current layer of the time wheel is any layer of the plurality of layers of the time wheel; migrating the plurality of second cache data into a plurality of slots of the current layer of the time wheel according to the expiration times of each of the second cache data and a time stamp of a slot run to by a time pointer of the current layer of the time wheel.
5. The cache cleaning method of claim 4, wherein, Before migrating the second cache data into the slots of the current layer time wheel according to the expiration time of each second cache data and the time stamp of the slot where the time pointer of the current layer time wheel runs to, the method further comprises: determining the dispersion degree between the expiration times of the second cache data; based on the dispersion degree, adjusting the unit time of the current layer time wheel to adjust the number of slots in the current layer time wheel, so that the data amount of the cache data stored in the slots in the current layer time wheel is balanced; wherein the unit time represents the time required for the time wheel to run one slot.
6. The cache cleaning method of claim 5, wherein, The adjustment of the unit time of the current layer time wheel based on the dispersion degree comprises: in the case that the dispersion degree is less than a preset dispersion degree, reducing the unit time of the current layer time wheel to increase the number of slots in the current layer time wheel.
7. The cache cleaning method of claim 4, wherein, After migrating the second cache data into the slots of the current layer time wheel according to the expiration time of each second cache data and the time stamp of the slot where the time pointer of the current layer time wheel runs to, the method further comprises: comparing the gap between the data amount of the cache data stored in the slots in the current layer time wheel; if there is a slot with a gap greater than a preset threshold, at least one candidate time wheel is constructed, and the period time of the candidate time wheel is the same as that of the current layer time wheel; part of the second cache data is migrated into at least one candidate time wheel, and the current layer time wheel and the candidate time wheel are run synchronously; wherein the slots corresponding to the second cache data before and after migration are corresponding.
8. The cache cleaning method of claim 1, wherein, Each cache data carries a cache name, and when the time wheel is run to each slot of the time wheel, the cache data in the slot is cleared, which comprises: when the time wheel runs to a target slot, traversing a linked list included in the target slot, the linked list including a plurality of cache names; for each cache name, determining the target cache data corresponding to the cache name, and clearing the target cache data.
9. A cache cleaning apparatus, characterized by, The device is applied to a distributed system, and the device comprises: an initialization module for initializing a time wheel, the time wheel including a plurality of slots, the number of slots corresponding to the period time of the time wheel; an acquisition module for acquiring the expiration time of a plurality of cache data, and acquiring the current time stamp of the slot where the time pointer of the time wheel runs to; a storage module for storing a plurality of cache names into corresponding slots based on the expiration time and the current time stamp; a cleaning module for running the time wheel to clear the cache data stored in each slot of the time wheel when the time pointer of the time wheel runs to the slot.
10. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor implements the steps in the cache cleaning method according to any one of claims 1-8 when executed.