Method and system for cleaning or updating cache
By designing a cache system that combines query module, management background module and buffer management module, the problems of cache consistency and cleaning efficiency in the microservice architecture are solved, and automated cache cleaning and updates are realized, improving system performance and user experience .
Patent Information
- Application Number
- CN202411463765.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-18
- Publication Date
- 2025-02-11
AI Technical Summary
In microservice architecture, traditional cache cleaning and update methods are difficult to achieve cache consistency across services, and manual cleaning logic is inefficient and error-prone.
A system for cleaning or updating caches is designed. Through the combination of query modules, management background modules and buffer management modules, cache annotations, AOP interception, cache management center and cross-instance cleaning mechanisms are used to achieve automated cache cleaning and updates.
It realizes cache consistency across services, improves the efficiency and accuracy of cache cleaning, reduces user waiting time, improves user experience, and significantly reduces the latency of obtaining data from external services.
Smart Images

Figure CN119415560A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of software development and database management, and specifically relates to a method and system for cleaning or updating a cache. Background Art
[0002] With the rapid development of network technology and the continuous improvement of users' requirements for website and application performance, caching technology has become an important means to improve performance and user experience. By storing snapshots of data, caching avoids repeated creation, processing and transmission of data, significantly improving the response speed and throughput of the system. However, caching technology also brings a series of challenges. First, the cache content is usually scattered, and a user request may involve the reading of multiple cache items. For example, when displaying a list of car models, the name of the car model, the guide price, price reduction information, etc. may be stored in different cache items. When these data sources are modified, how to ensure that all related cache items can be updated in time has become an important issue. Secondly, there are many ways to cache, including traditional local memory cache and cache solutions based on distributed systems, such as Redis. Different caching methods have different characteristics in terms of data consistency, reliability, and scalability. How to choose the appropriate one according to specific business needs and technology stack? Another challenge is to find a cache method and formulate corresponding cache cleaning and update strategies. Finally, with the rise of microservice architecture, the system is split into multiple independent services, each of which may have its own cache of the same data. In this case, how to ensure that the cached data in all services can remain consistent has become a more complex problem. The traditional cache cleaning and update method is usually to write cleaning logic for each cached data in Java code. Although this method is easy to understand, it has many shortcomings in practical applications. On the one hand, with the expansion of system scale and the complexity of business needs, the number and location of cache items may become very large and scattered. Manually writing cleaning logic is not only inefficient but also prone to errors. On the other hand, due to the introduction of microservice architecture, cached data may be scattered in multiple services. Manually writing cleaning logic is also difficult to achieve cache consistency across services. Therefore, in order to solve the above problems, it is necessary to improve and optimize this. Summary of the invention
[0003] The object of the present invention is to provide a method and system for cleaning or updating a cache to solve the problems raised in the above background technology.
[0004] In order to achieve the above object, the present invention provides the following technical solution: a system for cleaning or updating cache, the system comprising a query module, a management background module and a buffer management module.
[0005] Preferably, the specific steps of the method are:
[0006] Step 1: Cache annotation mark: add @Cache and @Cacheable annotations to the methods that need to be cached;
[0007] Step 2: Register the instance to the cache management center: When the application starts, register the applicationName and IP address of the current instance to the cache management center (such as Redis); use the zset structure of Redis to manage the instance timeout with score (current timestamp + 10 minutes);
[0008] Step 3: Trigger the cache cleanup operation: In the source data management backend or other places, initiate a request with cache cleanup parameters;
[0009] Step 4: Request processing and cache cleaning: Use AOP (aspect-oriented programming) to determine whether the request contains cache cleaning parameters;
[0010] Step 5: Cross-instance cache cleanup: The current instance cleans up the remote cache, and there is a local or secondary cache, so it is necessary to notify other instances to clean up as well; query the addresses of other instances through the cache management center, and send a request with the parameter _replicate=true to clean up;
[0011] Step 6: Automatically propagate cleanup tags: During the request execution process, call other microservices or third-party interfaces to automatically add cache cleanup tags to the request parameters;
[0012] Step 7, new data cache: After executing the method with cache annotation, if there is a cache cleanup parameter and the method is executed successfully, the new result is automatically cached;
[0013] Step 8: Continue to execute and complete: Repeat steps 4 to 7 until the request processing is completed.
[0014] Preferably, the specific steps of registering the instance to the cache management center in step 2 include:
[0015] A1, Instance registration: When the application is started, the application instance registers its applicationName and IP address as key information to the cache management center;
[0016] A2, set timeout management: To manage instance timeout, set an initial score value for each instance;
[0017] A3, regularly update the instance score: To prevent the instance from accidentally expiring in the cache management center, regularly update its score value;
[0018] A4, processing expired instances: When the cache management center needs to determine whether an instance is expired, it does so by querying the score value of the instance in the zset;
[0019] A5, Optimization and expansion: To improve performance, use Redis pipeline technology to batch process ZADD and query operations to reduce the number of interactions with Redis.
[0020] Preferably, the specific steps of triggering the cache clearing operation in step 3 include:
[0021] B1, trigger request: when the cache needs to be cleaned up in the source data management background or other trigger points, a request with cache cleanup parameters is initiated;
[0022] B2, parameter setting: The request needs to contain enough information to identify the cache items that need to be cleared;
[0023] B3, request processing: The cache cleaning request is sent to the cache management module, which is responsible for parsing the request parameters and performing the corresponding cleaning operation;
[0024] B4, cleaning strategy: According to the parameters specified in the request, the cache management module will execute different cleaning strategies;
[0025] B5, Notification and Log: After performing the cleanup operation, the cache management module can send a notification to the relevant system and user to inform them of the cache cleanup results;
[0026] B6, Exception handling: When an exception or error occurs during the cleaning operation, corresponding processing is required;
[0027] B7, Performance optimization: For large-scale cache cleanup operations, batch operations and asynchronous processing are needed to improve the efficiency of the cleanup operations.
[0028] Preferably, the specific steps of request processing and cache cleaning in step 4 include:
[0029] C1, AOP interception: When the application receives an external request, it intercepts the method with specific annotations through AOP (Aspect-Oriented Programming) technology;
[0030] C2, determine the cleanup parameters: the request contains cache cleanup parameters, and the AOP aspect will execute and process these parameters;
[0031] C3, delete related cache items: After determining that the cache needs to be cleaned, the AOP aspect will execute different cleanup logic according to the cache type;
[0032] C4, cache cleaning logic: change the cleaning logic according to the different cache types;
[0033] C5, execute the intercepted method: After cleaning the relevant cache items, the AOP aspect will continue to execute the intercepted method.
[0034] Preferably, the specific steps of cross-instance cache cleanup in step 5 include:
[0035] D1, identify cache type: when performing a cache cleanup operation, first identify whether the cache being cleaned is a remote cache, and determine whether there is a local cache or a secondary cache;
[0036] D2, cross-instance cleanup requirements: When there is a local cache or a secondary cache, and the current instance is only one of multiple instances, ensure that the same cache on other instances is also cleaned;
[0037] D3, query the addresses of other instances: query the address information of other instances in the current system through the cache management center (such as Redis);
[0038] D4, build a cleanup request: build a request with specific parameters for each instance address that needs to be cleaned;
[0039] D5, send cleanup request: send the cleanup request to each other instance using an appropriate communication mechanism;
[0040] D6, processing the received cleanup request: on each instance that receives the cleanup request, execute the corresponding cache cleanup logic according to the parameters in the request;
[0041] D7, ensure cleanup consistency: when performing cross-instance cache cleanup, ensure the consistency of the cleanup operation;
[0042] D8, exception handling and notification: When any exception or error occurs when sending or processing a cross-instance cleanup request, the error information is recorded and a notification is sent to the relevant personnel.
[0043] Preferably, the specific steps of automatically propagating the cleanup mark in step 6 include:
[0044] E1, identifying cache cleaning requirements: when executing a request, first determine whether the request requires cache cleaning;
[0045] E2, add cleanup tag: If the request needs to clean the cache, add a specific cleanup tag to the request;
[0046] E3, modify request parameters: modify the request parameters and add the cleanup tag to the request;
[0047] E4, calling other microservices or interfaces: During the request execution process, it involves calling other microservices and third-party interfaces. It is necessary to ensure that the cleanup mark can be passed correctly;
[0048] E5, Automatically propagate tags: When calling other microservices or interfaces, ensure that the cleanup tags are passed as part of the request;
[0049] E6, receiving-side processing: When the called microservice or interface receives a request with a cleanup tag, it needs to identify and process this tag;
[0050] E7, cache cleaning logic: On the receiving side, execute the corresponding cache cleaning logic according to the cleaning mark;
[0051] E8, ensure the transitivity of cleanup: When there are multiple microservices or interface calls in the call chain, ensure that the cleanup mark can be passed throughout the call chain and trigger the cache cleanup operation;
[0052] E9, Logging and Monitoring: Log the delivery of cleanup tags and cache cleanup operations for monitoring and troubleshooting;
[0053] E10, Optimization and Extension: As the system expands and evolves, the automatic propagation mechanism of cache cleaning needs to be continuously optimized and extended.
[0054] Preferably, the specific steps of caching new data in step seven include:
[0055] F1, execute the method with cache annotation: In the business logic, when a method with cache annotation is called, the method is marked as cacheable;
[0056] F2, check cache clearing parameters: Before executing the method with cache annotation, you need to check whether the request contains cache clearing parameters;
[0057] F3, execute the method and obtain the result: When the request does not contain the cache clearing parameter, or the clearing operation has been successfully completed, continue to execute the method with cache annotation and obtain the result of the method execution;
[0058] F4, determine whether the method is executed successfully: After executing the method, you need to determine whether the method is executed successfully;
[0059] F5, check cache annotation configuration: When the method is executed successfully and the method has a cache annotation, further check the configuration of the cache annotation;
[0060] F6, automatically cache new results: automatically store the results of method execution in the cache according to the configuration of the cache annotation;
[0061] F7, handle cache conflicts: When caching new results, if a cache with the same key is found, it needs to be handled according to the cache strategy.
[0062] The beneficial effects of the present invention are as follows:
[0063] 1. The present invention effectively avoids repeated execution of the same computing tasks or repeated database query operations by caching newly generated data. In the traditional data processing process, whenever a new request arrives, the system usually needs to recalculate or query operations to obtain the required data, which not only increases the response time but also may lead to excessive consumption of server resources. However, the caching technology of the present invention breaks this limitation. Once the data is generated or queried and its validity and accuracy are confirmed, it will be cached. When the same request arrives again, the system no longer needs to repeat the same operation, but can directly retrieve the pre-stored results from the cache. This fast response capability greatly reduces the user's waiting time and improves the user experience.
[0064] 2. By caching new data, the system can significantly reduce the delay in obtaining data from external services or data sources. Because the cache is usually located locally or close to the user, data access speed is faster. Especially in mobile applications or environments with poor network conditions, the cache can provide users with a smoother and more responsive user experience.
[0065] 3. The present invention uses precise cache strategies and cleaning logic, so that the system can achieve efficient cache utilization and fast data access while ensuring data consistency. Through precise cache strategies and cleaning logic, the system can achieve efficient cache utilization and fast data access while ensuring data consistency. When there is a cache cleaning operation in the system, the mechanism of automatically caching new data can ensure that new and accurate data is cached in a timely manner after cleaning old data. BRIEF DESCRIPTION OF THE DRAWINGS
[0066] Figure 1 This is the overall flow chart of automatic cache clearing of the present invention;
[0067] Figure 2 This is a flow chart of the automatic cache cleaning code implementation of the present invention;
[0068] Figure 3 Clean up other instance caches for the present invention - step diagram. DETAILED DESCRIPTION
[0069] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only 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.
[0070] like Figures 1 to 3 As shown, an embodiment of the present invention provides a method and system for cleaning or updating a cache, and the system is composed of a query module, a management background module and a buffer management module.
[0071] Among them, a method for clearing or updating the cache includes the following specific steps:
[0072] Step 1: Cache annotation mark: Add @Cache or @Cacheable annotation to the method that needs to be cached;
[0073] Step 2: Register the instance to the cache management center: When the application starts, register the applicationName and IP address of the current instance to the cache management center (such as Redis); use the zset structure of Redis to manage the instance timeout with score (current timestamp + 10 minutes); update the instance score every 3 minutes to ensure that the instance does not expire in the cache management center;
[0074] Step 3: Trigger the cache cleanup operation: In the source data management backend or other places, initiate a request with cache cleanup parameters;
[0075] Step 4: Request processing and cache cleaning: Use AOP (aspect-oriented programming) to determine whether the request contains cache cleaning parameters; if so, delete the relevant cache items before executing the method with cache annotations; execute the corresponding cleaning logic according to the cache type (remote / local / secondary cache);
[0076] Step 5: Cross-instance cache cleanup: The current instance cleans up the remote cache, and there is a local or secondary cache, so it is necessary to notify other instances to clean up as well; query the addresses of other instances through the cache management center, and send a request with the parameter _replicate=true to clean up;
[0077] Step 6: Automatically propagate cleanup tags: During the request execution process, call other microservices or third-party interfaces to automatically add cache cleanup tags to the request parameters;
[0078] Step 7, new data cache: After executing the method with cache annotation, if there is a cache cleanup parameter and the method is executed successfully, the new result is automatically cached;
[0079] Step 8: Continue to execute and complete: Repeat steps 4 to 7 until the request processing is completed.
[0080] The specific steps of registering the instance to the cache management center in step 2 include:
[0081] A1, Instance registration: When the application starts, the application instance registers its applicationName and IP address as key information to the cache management center. In Redis, we can choose to use an ordered set (zset) to store this information because zset allows us to sort elements according to scores;
[0082] A2, set timeout management: To manage instance timeout, set an initial score value for each instance. This score value can be the current timestamp plus a specific timeout period (such as 10 minutes). Use the Redis ZADD command to set the instance's IP address and applicationName as members, and add the calculated score value as the score to the zset.
[0083] A3, regularly update the instance score: To prevent the instance from accidentally expiring in the cache management center, regularly update its score value. You can set a background task (such as using a scheduled task framework) to execute every 3 minutes, traverse all instances in the zset, and update their score values to the current timestamp plus 10 minutes. Use the Redis ZADD command to update. If the instance score value is new enough (for example, no more than 3 minutes from the current time), there is no need to update.
[0084] A4, Processing expired instances: When the cache management center needs to determine whether an instance is expired, it queries the score value of the instance in zset. If the score value of the instance is less than the current timestamp, it means that the instance has expired. You can perform corresponding processing, such as removing the instance from the load balancer, sending an alarm notification, etc. You can set a scheduled task or listener to regularly (such as every minute) check and process expired instances.
[0085] A5, Optimization and expansion: To improve performance, use Redis pipeline technology to batch process ZADD and query operations to reduce the number of interactions with Redis. If the number of instances is very large, consider using Redis clusters to improve the scalability and stability of the system. You can add more metadata to the instance registration information according to business needs, such as the instance's health status, load conditions, etc., for more refined management.
[0086] The specific steps for triggering the cache cleanup operation in step 3 include:
[0087] B1, trigger request: when the cache needs to be cleaned up in the source data management background or other trigger points, a request with cache cleanup parameters is initiated;
[0088] B2, parameter setting: The request needs to contain enough information to identify the cache items that need to be cleared; these parameters may include cache key, mode (such as wildcard matching), cache type (such as user cache, product cache, etc.); according to business needs, some additional parameters can also be set, such as whether to force clearing, clearing priority, etc.;
[0089] B3, Request processing: The cache cleaning request is sent to the cache management module, which is responsible for parsing the request parameters and performing the corresponding cleaning operation. Before processing the request, some verification and permission checks can be performed to ensure that only authorized users or systems can trigger the cache cleaning operation.
[0090] B4, Cleanup strategy: The cache management module will execute different cleanup strategies according to the parameters specified in the request. If a specific cache key is specified in the request, the cache item corresponding to the key will be deleted directly. If the request uses a wildcard matching pattern, all cache items will be traversed and the cache items that match the matching pattern will be deleted. Batch cleanup operations can also be performed based on cache type or other conditions.
[0091] B5, Notification and log: After executing the cleanup operation, the cache management module can send notifications to related systems and users to inform them of the cache cleanup results. At the same time, it is also necessary to record the log of the cleanup operation, including the trigger time, request parameters, cleanup results, etc., for subsequent auditing and troubleshooting;
[0092] B6, exception handling: When an exception or error occurs during the cleaning operation, corresponding processing is required; the exception information can be recorded and an alarm notification can be sent to relevant personnel to facilitate timely investigation and resolution of the problem;
[0093] B7, Performance optimization: For large-scale cache cleanup operations, batch operations and asynchronous processing are needed to improve the efficiency of the cleanup operations. At the same time, the cache management module also needs to be optimized to ensure stable operation in high-concurrency scenarios.
[0094] The specific steps of request processing and cache cleaning in step 4 include:
[0095] C1, AOP interception: When the application receives an external request, it intercepts the method with a specific annotation (such as @Cacheable) through AOP (aspect-oriented programming) technology; during the interception process, the AOP aspect checks whether the request contains parameters for clearing the cache; these parameters may be request headers, request parameters, or specific context information;
[0096] C2, determine the cleanup parameters: the request contains cache cleanup parameters, and the AOP aspect will execute and process these parameters; the parameters may include cache key, cache type (such as remote cache, local cache, secondary cache, etc.) and other cleanup-related configurations;
[0097] C3, delete related cache items: After determining that the cache needs to be cleaned, the AOP aspect will perform different cleanup logic according to the cache type; for remote caches (such as Redis), the aspect will call the API of the remote cache service to delete the specified cache items; for local caches (such as caches in JVM memory), the aspect will directly operate the local cache object to delete the cache items; for secondary caches (such as distributed cache clusters), the aspect may need to coordinate multiple nodes to ensure cache consistency and perform corresponding cleanup operations;
[0098] C4, cache cleaning logic: Change the cleaning logic according to the cache type. For a simple key-value cache, simply delete the specified key. If the cache stores a complex data structure (such as a list or map), you may need to traverse the structure and delete related cache items. You also need to consider factors such as cache expiration time and cache dependencies to ensure the accuracy and consistency of the cleaning operation.
[0099] C5, execute the intercepted method: After cleaning the relevant cache items, the AOP aspect will continue to execute the intercepted method (that is, the method with cache annotations). If the method needs to access the cleared cache items, since the cache has been cleared, the method will recalculate or obtain new data and may store it back in the cache.
[0100] The specific steps of cross-instance cache cleanup in step 5 include:
[0101] D1, identify the cache type: when performing a cache cleanup operation, first identify whether the cache being cleaned is a remote cache (such as Redis), and determine whether there is a local cache or a secondary cache;
[0102] D2, cross-instance cleanup requirements: When there is a local cache or a secondary cache, and the current instance is only one of multiple instances, ensure that the same cache on other instances is also cleaned;
[0103] D3, query the addresses of other instances: query the address information of other instances in the current system through the cache management center (such as Redis); this information is usually registered to the cache management center when the instance is started;
[0104] D4, build a cleanup request: build a request with specific parameters for each instance address that needs to be cleaned up; in this scenario, you need to add the _replicate=true parameter to indicate that this is a cross-instance cache cleanup request;
[0105] D5, Send cleaning request: Use an appropriate communication mechanism (such as HTTP request, message queue, etc.) to send the cleaning request to each other instance; the request should contain enough information so that the recipient can recognize and process the cache cleaning operation;
[0106] D6, processing the received cleanup request: on each instance that receives the cleanup request, the corresponding cache cleanup logic is executed according to the parameters in the request; because the request contains the parameter _replicate=true, these instances will know that this is a cross-instance cleanup operation and will clean up their local cache or L2 cache accordingly;
[0107] D7, ensure cleanup consistency: When performing cross-instance cache cleanup, ensure the consistency of the cleanup operation; if an instance fails or encounters delays when processing a cleanup request, it may cause cache data to be inconsistent between different instances; therefore, some additional measures may be required to ensure that the cleanup operation can be successfully executed on all instances;
[0108] D8, exception handling and notification: When any exception or error occurs when sending or processing a cross-instance cleanup request, the error information is recorded and a notification is sent to the relevant personnel; at the same time, according to business needs, it may be necessary to consider whether to roll back the previous cleanup operation or take other remedial measures.
[0109] The specific steps of automatically propagating the cleanup mark in step 6 include:
[0110] E1, Identify cache cleanup requirements: When executing a request, first determine whether the request requires cache cleanup; this can be determined by checking request parameters, request headers, or based on business logic;
[0111] E2, add a cleanup tag: If the request needs to clean up the cache, add a specific cleanup tag (such as _cacheInvalidate = true) to the request; this tag will be used to identify that the request needs to trigger a cache cleanup operation;
[0112] E3, Modify request parameters: Modify the request parameters and add the cleanup marker to the request. The specific method of adding depends on the communication protocol and framework used. For HTTP requests, the cleanup marker can be added as a query parameter, request header, or part of the request body.
[0113] E4, calling other microservices or interfaces: During the request execution process, it involves calling other microservices and third-party interfaces. It is necessary to ensure that the cleanup mark can be passed correctly. This usually requires corresponding processing at each layer of the call chain.
[0114] E5, Automatically propagate tags: When calling other microservices or interfaces, ensure that the cleanup tags are passed as part of the request; this can be done by explicitly adding the cleanup tags when calling, or using some mechanism (such as interceptors, filters, etc.) to automatically add and propagate the cleanup tags;
[0115] E6, receiving-side processing: When the called microservice or interface receives a request with a cleanup tag, it needs to identify and process the tag. The specific processing method depends on the business requirements and system architecture. Possible methods include: directly performing cache cleanup operations; passing the cleanup tag to other systems or services so that they can perform corresponding cache cleanup; recording the cleanup request and processing it asynchronously later;
[0116] E7, cache cleaning logic: On the receiving side, execute the corresponding cache cleaning logic according to the cleaning mark; this may involve deleting specific cache items, clearing the entire cache area, or triggering other cleaning operations;
[0117] E8, ensure the transitivity of cleaning: When there are multiple microservices or interface calls in the call chain, ensure that the cleaning mark can be passed throughout the call chain and trigger the cache cleaning operation; this may require the implementation of consistent cache cleaning strategies and mechanisms throughout the system;
[0118] E9, Logging and Monitoring: Log the passing of cleanup tags and cache cleanup operations for monitoring and troubleshooting; this helps track the execution of cleanup operations and ensure cache consistency;
[0119] E10, Optimization and Extension: As the system expands and evolves, the automatic propagation mechanism of cache cleaning needs to be continuously optimized and extended; this may include improving the way cleanup marks are delivered, optimizing the performance of cleanup operations, adding more cache cleaning strategies, etc.
[0120] The specific steps of caching new data in step seven include:
[0121] F1, execute a method with a cache annotation: In the business logic, when a method with a cache annotation (such as @Cacheable) is called, the method is marked as cacheable; this means that the execution result of the method will be stored in the cache, so that subsequent identical requests can directly obtain the result from the cache without re-executing the method;
[0122] F2, check cache clearing parameters: Before executing a method with a cache annotation, you need to check whether the request contains cache clearing parameters; this can be achieved by checking the request header, request parameters, or specific context information;
[0123] F3, execute the method and obtain the result: When the request does not contain the cache clearing parameter, or the clearing operation has been successfully completed, continue to execute the method with cache annotation and obtain the result of the method execution;
[0124] F4, determine whether the method is executed successfully: After executing the method, you need to determine whether the method is executed successfully; this can usually be determined by checking the method's return value, exception handling, or specific business logic;
[0125] F5, check cache annotation configuration: When the method is executed successfully and the method has a cache annotation, further check the configuration of the cache annotation; this includes determining the cache key, cache expiration time, cache storage location (such as remote cache, local cache, etc.), etc.;
[0126] F6, Automatically cache new results: Automatically store the results of method execution in the cache according to the configuration of the cache annotation; this usually involves serializing the results and storing them in the specified cache storage location; at the same time, it is also necessary to associate the cache key with the result so that the result can be retrieved from the cache based on the key later;
[0127] F7, handling cache conflicts: When caching a new result, it is found that there is a cache item with the same key (possibly due to concurrent operations), which needs to be handled according to the cache strategy; possible strategies include: using optimistic locking or distributed locking to ensure cache consistency; overwriting the old cache item and replacing it with the new result; adding the new result to the cache but keeping the old cache item (for example, using version control to manage multiple cache items).
[0128] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device.
[0129] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A system for cleaning or updating a cache, characterized in that: The system consists of a query module, a management background module and a buffer management module.
2. A method for cleaning or updating a cache, based on the above system, characterized in that: The specific steps to clean or update the cache method are: Step 1: Cache annotation mark: add @Cache and @Cacheable annotations to the methods that need to be cached; Step 2: Register the instance to the cache management center: When the application starts, register the applicationName and IP address of the current instance to the cache management center; Use Redis's zset structure to manage instance timeouts with scores; Step 3: Trigger the cache cleanup operation: In the source data management backend or other places, initiate a request with cache cleanup parameters; Step 4: Request processing and cache cleaning: Use AOP to determine whether the request contains cache cleaning parameters; Step 5: Cross-instance cache cleanup: The current instance cleans up the remote cache, and there is a local or secondary cache, so it is necessary to notify other instances to clean up as well; query the addresses of other instances through the cache management center, and send a request with the parameter _replicate=true to clean up; Step 6: Automatically propagate cleanup tags: During the request execution process, call other microservices or third-party interfaces to automatically add cache cleanup tags to the request parameters; Step 7, new data cache: After executing the method with cache annotation, if there is a cache cleanup parameter and the method is executed successfully, the new result is automatically cached; Step 8: Continue to execute and complete: Repeat steps 4 to 7 until the request processing is completed.
3. A method for clearing or updating a cache according to claim 2, characterized in that: The specific steps of registering the instance to the cache management center in step 2 include: A1, Instance registration: When the application is started, the application instance registers its applicationName and IP address as key information to the cache management center; A2, set timeout management: To manage instance timeout, set an initial score value for each instance; A3, regularly update the instance score: To prevent the instance from accidentally expiring in the cache management center, regularly update its score value; A4, processing expired instances: When the cache management center needs to determine whether an instance is expired, it does so by querying the score value of the instance in the zset; A5, Optimization and expansion: To improve performance, use Redis pipeline technology to batch process ZADD and query operations to reduce the number of interactions with Redis.
4. A method for clearing or updating a cache according to claim 2, characterized in that: The specific steps of triggering the cache clearing operation in step 3 include: B1, trigger request: when the cache needs to be cleaned up in the source data management background or other trigger points, a request with cache cleanup parameters is initiated; B2, parameter setting: The request needs to contain enough information to identify the cache items that need to be cleared; B3, request processing: The cache cleaning request is sent to the cache management module, which is responsible for parsing the request parameters and performing the corresponding cleaning operation; B4, cleaning strategy: According to the parameters specified in the request, the cache management module will execute different cleaning strategies; B5, Notification and Log: After performing the cleanup operation, the cache management module sends a notification to the relevant system and user to inform them of the cache cleanup result; B6, Exception handling: When an exception or error occurs during the cleaning operation, corresponding processing is required; B7, Performance optimization: For large-scale cache cleanup operations, batch operations and asynchronous processing are needed to improve the efficiency of the cleanup operations.
5. A method for clearing or updating a cache according to claim 2, characterized in that: The specific steps of request processing and cache cleaning in step 4 include: C1, AOP interception: When the application receives an external request, the method with a specific annotation is intercepted through AOP technology; C2, determine the cleanup parameters: the request contains cache cleanup parameters, and the AOP aspect will execute and process these parameters; C3, delete related cache items: After determining that the cache needs to be cleaned, the AOP aspect will execute different cleanup logic according to the cache type; C4, cache cleaning logic: change the cleaning logic according to the different cache types; C5, execute the intercepted method: After cleaning the relevant cache items, the AOP aspect will continue to execute the intercepted method.
6. A method for clearing or updating a cache according to claim 2, characterized in that: The specific steps of cross-instance cache cleanup in step 5 include: D1, identify cache type: when performing a cache cleanup operation, first identify whether the cache being cleaned is a remote cache, and determine whether there is a local cache or a secondary cache; D2, cross-instance cleanup requirements: When there is a local cache or a secondary cache, and the current instance is only one of multiple instances, ensure that the same cache on other instances is also cleaned; D3, query the addresses of other instances: query the address information of other instances in the current system through the cache management center (such as Redis); D4, build a cleanup request: build a request with specific parameters for each instance address that needs to be cleaned; D5, send cleanup request: send the cleanup request to each other instance using an appropriate communication mechanism; D6, processing the received cleanup request: on each instance that receives the cleanup request, execute the corresponding cache cleanup logic according to the parameters in the request; D7, ensure cleanup consistency: when performing cross-instance cache cleanup, ensure the consistency of the cleanup operation; D8, exception handling and notification: When any exception or error occurs when sending or processing a cross-instance cleanup request, the error information is recorded and a notification is sent to the relevant personnel.
7. A method for clearing or updating a cache according to claim 2, characterized in that: The specific steps of automatically propagating the cleaning mark in step 6 include: E1, identifying cache cleaning requirements: when executing a request, first determine whether the request requires cache cleaning; E2, add cleanup tag: If the request needs to clean the cache, add a specific cleanup tag to the request; E3, modify request parameters: modify the request parameters and add the cleanup tag to the request; E4, calling other microservices or interfaces: During the request execution process, it involves calling other microservices and third-party interfaces. It is necessary to ensure that the cleanup mark can be passed correctly; E5, Automatically propagate tags: When calling other microservices or interfaces, ensure that the cleanup tags are passed as part of the request; E6, receiving-side processing: When the called microservice or interface receives a request with a cleanup tag, it needs to identify and process this tag; E7, cache cleaning logic: On the receiving side, execute the corresponding cache cleaning logic according to the cleaning mark; E8, ensure the transitivity of cleanup: When there are multiple microservices or interface calls in the call chain, ensure that the cleanup mark can be passed throughout the call chain and trigger the cache cleanup operation; E9, Logging and Monitoring: Log the delivery of cleanup tags and cache cleanup operations for monitoring and troubleshooting; E10, Optimization and Extension: As the system expands and evolves, the automatic propagation mechanism of cache cleaning needs to be continuously optimized and extended.
8. A method for clearing or updating a cache according to claim 2, characterized in that: The specific steps of caching new data in step seven include: F1, execute the method with cache annotation: In the business logic, when a method with cache annotation is called, the method is marked as cacheable; F2, check cache clearing parameters: Before executing the method with cache annotation, you need to check whether the request contains cache clearing parameters; F3, execute the method and obtain the result: When the request does not contain the cache clearing parameter, or the clearing operation has been successfully completed, continue to execute the method with cache annotation and obtain the result of the method execution; F4, determine whether the method is executed successfully: After executing the method, you need to determine whether the method is executed successfully; F5, check cache annotation configuration: When the method is executed successfully and the method has a cache annotation, further check the configuration of the cache annotation; F6, automatically cache new results: automatically store the results of method execution in the cache according to the configuration of the cache annotation; F7, handle cache conflicts: When caching new results, if a cache with the same key is found, it needs to be handled according to the cache strategy.
Citation Information
Cited By
Caching method and system for list data structure
CN120872861A