Cache asynchronous refreshing method, system, equipment and medium
By using an asynchronous cache refresh method and leveraging Redis atomic operations and thread pool executors, the database impact problem caused by cache invalidation under high concurrency is solved. This enables on-demand cache refresh and concurrency control, reducing development workload and saving server resources.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-26
- Publication Date
- 2026-03-31
AI Technical Summary
In high-concurrency scenarios, existing caching systems can cause database crashes and service outages when caches expire, and additional scheduled task code needs to be written, which affects performance and wastes resources.
By adopting an asynchronous refresh method with a cache, and configuring the data retrieval function, refresh lock expiration time, and thread pool executor, combined with Redis atomic operations, asynchronous refresh and concurrency control are achieved, avoiding the writing of scheduled tasks and resource waste.
It enables on-demand cache refresh, reduces development workload, avoids server resource waste, ensures user request response speed, and solves the problem of database impact under high concurrency.
Smart Images

Figure CN121764702A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of distributed caching technology, and in particular to a method, system, device, and medium for asynchronous cache refresh. Background Technology
[0002] Currently, most internet companies targeting consumers use distributed caching (such as Redis) to reduce database query pressure. The conventional data query logic is to first read the cache, and if the cache is missing, query the database and update the cache. However, in high-concurrency scenarios, if the cache expires instantly, a large number of requests will directly impact the database. Especially when there are slow queries, it may cause the database and service to crash.
[0003] Existing solutions often use scheduled tasks (such as XXLJob) to periodically refresh the cache of frequently accessed data in order to improve the cache hit rate. However, this approach has the following drawbacks:
[0004] (1) Additional scheduled task code needs to be written, increasing the development workload;
[0005] (2) It relies on scheduled task services, and too many tasks will affect the execution performance;
[0006] (3) The scheduled task cannot match the timing of user requests and continues to run in a loop even when there are no requests, resulting in the waste of server resources. Summary of the Invention
[0007] The purpose of this invention is to provide a method, system, device, and medium for asynchronous cache refresh, thereby solving all or one of the aforementioned problems in the prior art.
[0008] To solve the above-mentioned technical problems, the specific technical solution of the present invention is as follows:
[0009] On the one hand, the present invention provides an asynchronous cache refresh method, comprising the following steps:
[0010] Core parameter configuration steps:
[0011] Configure the data retrieval function, cache key name, refresh lock expiration time, Redis service operation object, and thread pool executor;
[0012] Distributed refresh lock control steps:
[0013] Set a refresh flag key based on the cache key name; call the Redis service operation object to read the value of the refresh flag key; according to the value of the refresh flag key, call the data acquisition function, the refresh lock expiration time, and the thread pool executor to control the refresh lock;
[0014] Cache read and data return steps:
[0015] In response to the refresh lock control operation, the Redis service operation object is invoked to read the main cache data corresponding to the cache key name; and a data return operation is performed based on the existence of the main cache data.
[0016] Furthermore, the core parameter configuration step further includes:
[0017] The data acquisition function is used to define the logic for retrieving target data from the database;
[0018] The target cache string is identified by the cache key name;
[0019] The refresh lock expiration time is used to control the effective duration of the refresh lock;
[0020] The Redis service operation object is used to provide Redis operation capabilities;
[0021] The thread pool executor is used to execute asynchronous tasks.
[0022] Furthermore, the step of setting a refresh identifier key based on the cache key name further includes:
[0023] A preset suffix is appended to the cache key name to obtain the refresh identifier key.
[0024] Furthermore, the step of controlling the refresh lock by calling the data acquisition function, the refresh lock expiration time, and the thread pool executor based on the refresh identifier key value further includes:
[0025] In response to the refresh identifier key being empty, check if a cache refresh operation exists. If no cache refresh operation exists, invoke Redis's atomic operation.
[0026] Furthermore, the atomic operation further includes:
[0027] Set the value of the refresh identifier key to one, and set the expiration time of the refresh identifier key to the expiration time of the refresh lock.
[0028] Furthermore, the distributed refresh lock control step further includes:
[0029] In response to the atomic operation returning success, the data retrieval function is asynchronously invoked based on the thread pool executor to perform database query and cache update operations.
[0030] Furthermore, the data return operation based on the existence of the main cache data further includes:
[0031] In response to the existence of the main cache data, the main cache data is deserialized and returned to the user;
[0032] In response to the absence of the main cache data, the data retrieval function is synchronously invoked to retrieve the latest data from the database and return it to the user.
[0033] On the other hand, the present invention also provides an asynchronous cache refresh system, comprising:
[0034] The core parameter configuration module is used to configure the data retrieval function, cache key name, refresh lock expiration time, Redis service operation object, and thread pool executor.
[0035] The distributed refresh lock control module is used to: set a refresh identifier key based on the cache key name; call the Redis service operation object to read the value of the refresh identifier key; and call the data acquisition function, the refresh lock expiration time, and the thread pool executor to perform refresh lock control according to the refresh identifier key value.
[0036] The cache read and data return module is used to: respond to the refresh lock control operation, call the Redis service operation object to read the main cache data corresponding to the cache key name; and perform a data return operation based on the existence of the main cache data.
[0037] On the other hand, the present invention also provides a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of the asynchronous cache refresh method.
[0038] On the other hand, the present invention also provides a computer device, the computer device including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; wherein:
[0039] The memory is used to store computer programs;
[0040] The processor is configured to execute the steps of the method by running a program stored in the memory.
[0041] The beneficial effects of the technical solution of this invention are:
[0042] 1. The asynchronous cache refresh method described in this invention can achieve the goal of eliminating the need to write scheduled task code, thus reducing development workload; it only performs refresh operations when a user request triggers a cache check, avoiding idle scheduled tasks and saving server resources; it controls the refresh lock through atomic operations to ensure that only one thread performs refresh at the same time, avoiding concurrency conflicts, and the asynchronous processing does not affect the response speed of user requests, thus making up for the shortcomings of the prior art and having high application value.
[0043] 2. The cache asynchronous refresh system described in this invention can achieve the cache asynchronous refresh method described in this invention through the cooperation of system modules.
[0044] 3. The computer-readable storage medium of the present invention can enable the boot system module to cooperate in order to realize the asynchronous cache refresh method of the present invention, and the computer-readable storage medium of the present invention also effectively improves the operability of the asynchronous cache refresh method.
[0045] 4. The computer device described in this invention can store and execute the computer-readable storage medium, thereby realizing the asynchronous cache refresh method described in this invention. Attached Figure Description
[0046] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0047] Figure 1 This is a flowchart illustrating the asynchronous cache refresh method described in Embodiment 1 of the present invention;
[0048] Figure 2 This is a schematic diagram of the core code of the asynchronous cache refresh method described in Embodiment 1 of the present invention;
[0049] Figure 3 This is a schematic diagram of the architecture of the asynchronous cache refresh system described in Embodiment 2 of the present invention;
[0050] Figure 4 This is a schematic diagram of the structure of the computer device described in Embodiment 4 of the present invention;
[0051] The markings in the attached diagram are explained as follows:
[0052] 1501. Processor; 1502. Communication interface; 1503. Memory; 1504. Communication bus. Detailed Implementation
[0053] The preferred embodiments of the present invention will now be described in detail with reference to the accompanying drawings, so that the advantages and features of the present invention can be more easily understood by those skilled in the art, thereby providing a clearer and more explicit definition of the scope of protection of the present invention.
[0054] In the description of this invention, it should be noted that the embodiments described in this invention are only some embodiments of this invention, not all embodiments; based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this invention.
[0055] The terms "first," "second," etc., used in this specification, claims, and accompanying drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, apparatus, product, or device that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or devices.
[0056] Example 1
[0057] This embodiment provides a method for asynchronous cache refresh, such as... Figure 1 and Figure 2 As shown, it includes the following steps:
[0058] The core functionality of this method lies in combining asynchronous processing with atomic operations to achieve on-demand cache refresh and concurrency control. Taking Redis as an example, the specifics are as follows:
[0059] S100, core parameter configuration steps, including:
[0060] S101, five core parameters are pre-configured, as follows:
[0061] The data retrieval function, which uses the java.util.function.Supplier type, is used to define the logic for retrieving target data from the database. The parameter name is exemplified by dataSupplier.
[0062] The cache key name is a string used to identify the target cache. The parameter name is 'cacheKey' for example.
[0063] Refresh lock expiration time, in milliseconds, is used to control the effective duration of the refresh lock;
[0064] Redis service operation objects provide Redis operation capabilities;
[0065] Thread pool executors are used to execute asynchronous tasks.
[0066] S200, Distributed Refresh Lock Control Procedures, including:
[0067] S201. Construct a refresh indicator key, as follows:
[0068] The default suffix "__refresh" is appended to the cache key name cacheKey. For example, when the cache key name is "my.cache", the corresponding refresh identifier key is "my.cache__refresh".
[0069] S202. Identify the refresh indicator key, as follows:
[0070] The refresh flag key value is read from the Redis service operation object to determine whether a refresh lock exists.
[0071] If the refresh flag key is empty, then check if other threads are refreshing the cache. If no refresh is in progress, it means there is no valid refresh lock. At this time, call Redis's atomic operation: set the value of the refresh flag key to "1" and set its expiration time to the preset refresh lock expiration time. This atomic operation ensures that only one thread can successfully set the refresh lock at the same time. After the operation returns successfully, the thread pool executor asynchronously calls the data retrieval function to perform database query and cache update operations.
[0072] S300, cache read and data return steps, including:
[0073] While performing refresh lock control, the cache data corresponding to the main cache key (i.e. the configured cache key name) is read through the Redis service operation object;
[0074] If the main cache data exists, deserialize it and return it directly to the user;
[0075] If the main cache data does not exist, the asynchronous process is skipped, and the data retrieval function dataSupplier is called synchronously to retrieve the latest data from the database and return it, ensuring that user requests are responded to in a timely manner.
[0076] It should be noted that the above examples are merely for explaining the present invention and should not be construed as limiting the scope of protection of the present invention.
[0077] Example 2
[0078] This embodiment is based on the same inventive concept as the asynchronous cache refresh method described in Embodiment 1, and provides an asynchronous cache refresh system, such as... Figure 3 As shown, it includes:
[0079] The core parameter configuration module is used to configure the data retrieval function, cache key name, refresh lock expiration time, Redis service operation object, and thread pool executor.
[0080] The distributed refresh lock control module is used to: set a refresh identifier key based on the cache key name; call the Redis service operation object to read the value of the refresh identifier key; and call the data acquisition function, the refresh lock expiration time, and the thread pool executor to perform refresh lock control according to the refresh identifier key value.
[0081] The cache read and data return module is used to: respond to the refresh lock control operation, call the Redis service operation object to read the main cache data corresponding to the cache key name; and perform a data return operation based on the existence of the main cache data.
[0082] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0083] Example 3
[0084] This embodiment provides a computer-readable storage medium, including:
[0085] The storage medium is used to store computer software instructions used to implement the asynchronous cache refresh method described in Embodiment 1 above. It includes a program for executing the asynchronous cache refresh method described above. Specifically, the executable program can be built into the asynchronous cache refresh system described in Embodiment 2. In this way, the asynchronous cache refresh system can implement the asynchronous cache refresh method described in Embodiment 1 by executing the built-in executable program.
[0086] Furthermore, the computer-readable storage medium in this embodiment can be any combination of one or more readable storage media, wherein the readable storage medium includes an electrical, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof.
[0087] Example 4
[0088] This embodiment provides an electronic device, such as... Figure 4 As shown, the electronic device may include: a processor 1501, a communication interface 1502, a memory 1503, and a communication bus 1504, wherein the processor 1501, the communication interface 1502, and the memory 1503 communicate with each other through the communication bus 1504.
[0089] Memory 1503 is used to store computer programs;
[0090] When the processor 1501 executes the computer program stored in the memory 1503, it implements the steps of the asynchronous cache refresh method described in Embodiment 1 above.
[0091] As one embodiment of the present invention, the communication bus mentioned in the terminal above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 4 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0092] As one embodiment of the present invention, the communication interface is used for communication between the aforementioned terminal and other devices.
[0093] In one embodiment of the present invention, the memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0094] As one embodiment of the present invention, the processor described above may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0095] It should be understood that in the various embodiments of this document, the sequence number of each process does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this document.
[0096] It should also be understood that, in the embodiments herein, the term "and / or" is merely a description of the relationship between associated objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / " in this document generally indicates that the preceding and following associated objects have an "or" relationship.
[0097] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this document.
[0098] In the embodiments provided herein, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the couplings or direct couplings or communication connections shown or discussed may be indirect couplings or communication connections through some interfaces, devices, or units, or they may be electrical, mechanical, or other forms of connection.
[0099] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of the embodiments described herein, depending on actual needs.
[0100] Furthermore, the functional units in the various embodiments of this document can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0101] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this paper, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this paper. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0102] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent structural or procedural transformations made based on the content of the present invention's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A method for cache asynchronous flush, characterized in that, The method comprises the following steps: The core parameter configuration step: Configure the data acquisition function, cache key name, refresh lock expiration time, Redis service operation object, and thread pool executor; Distributed refresh lock control step: Set a refresh identification key based on the cache key name; read the refresh identification key value by calling the Redis service operation object; perform refresh lock control by calling the data acquisition function, refresh lock expiration time, and thread pool executor according to the refresh identification key value; Cache reading and data returning step: In response to the refresh lock control operation, read the main cache data corresponding to the cache key name by calling the Redis service operation object; perform data returning operation according to the existence of the main cache data.
2. The cache asynchronous refresh method according to claim 1, wherein: The core parameter configuration step further comprises: Defining the logic of obtaining target data from the database by using the data acquisition function; Using the cache key name to identify the target cache string; Using the refresh lock expiration time to control the effective duration of the refresh lock; Using the Redis service operation object to provide Redis operation capability; Using the thread pool executor to execute asynchronous tasks.
3. The cache asynchronous refresh method according to claim 1, wherein: The refresh identification key is set based on the cache key name, further comprising: Splicing a preset suffix after the cache key name to obtain the refresh identification key.
4. The cache asynchronous refresh method according to claim 1, wherein: According to the refresh identification key value, the data acquisition function, the refresh lock expiration time, and the thread pool executor are called to perform refresh lock control, further comprising: In response to the refresh identification key value being empty, check whether there is a cache refresh operation, if there is no cache refresh operation, call the atomic operation of Redis.
5. The cache asynchronous refresh method according to claim 4, wherein: The atomic operation further comprises: Setting the value of the refresh identification key to one and setting the expiration time of the refresh identification key to the refresh lock expiration time.
6. The cache asynchronous refresh method according to claim 5, wherein: The distributed refresh lock control step further comprises: In response to the return success of the atomic operation, asynchronously call the data acquisition function based on the thread pool executor to perform database query and cache update operation.
7. The cache asynchronous refresh method according to claim 1, wherein: According to the existence of the main cache data, the data returning operation further comprises: In response to the existence of the main cache data, deserialize the main cache data and return it to the user; In response to the non-existence of the main cache data, synchronously call the data acquisition function to obtain the latest data from the database and return it to the user.
8. A cache asynchronous flush system, comprising: Comprise: The core parameter configuration module is configured to configure the data acquisition function, cache key name, refresh lock expiration time, Redis service operation object, and thread pool executor; The distributed refresh lock control module is configured to: set a refresh identification key based on the cache key name; call the Redis service operation object to read the refresh identification key value; and call the data acquisition function, the refresh lock expiration time, and the thread pool executor to perform refresh lock control according to the refresh identification key value. The cache reading and data returning module is configured to: in response to the refresh lock control operation, call the Redis service operation object to read the main cache data corresponding to the cache key name; and perform data returning operation according to the existence of the main cache data.
9. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the steps of the cache asynchronous refresh method in any one of claims 1-7.
10. A computer device, comprising: The computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the steps of the cache asynchronous refresh method in any one of claims 1-7. The computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the steps of the cache asynchronous refresh method in any one of claims 1-7.