Dynamic data cache updating method, system and equipment based on Redis and medium
By using the Redis cache layer in API data processing, the server load and response time problems caused by frequent requests of API data are solved, and more efficient data processing and performance improvements are achieved.
Patent Information
- Application Number
- CN202411928902.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-25
- Publication Date
- 2025-04-22
Smart Images

Figure CN119862048A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of data processing, and relates to a dynamic data cache update method, system, device and medium based on Redis. Background Art
[0002] With the popularity of mobile applications and network services, the number of data requests has increased dramatically, resulting in a decline in API performance and affecting user experience. Especially in scenarios with large amounts of data, frequent requests for API data will increase server load and increase response time, which in turn affects the overall performance of the application. Summary of the invention
[0003] The purpose of the present invention is to overcome the shortcomings of the above-mentioned prior art and provide a Redis-based dynamic data cache update method, system, device and medium, which can solve the problem of increased server load and prolonged response time caused by frequent requests for API data.
[0004] To achieve the above object, the present invention discloses a dynamic data cache update method based on Redis, comprising:
[0005] Use Redis as the cache layer and store the API data to be displayed in Redis;
[0006] When a user requests API data, it first checks whether the requested API data exists in Redis. If so, it directly returns the API data cached in Redis. When the requested API data does not exist in Redis, it initiates a request to the API and outputs the API data returned by the API.
[0007] The further improvement of the Redis-based dynamic data cache update method of the present invention is:
[0008] Furthermore, when the requested API data does not exist in Redis, initiating a request to the API, and outputting the API data returned by the API also includes:
[0009] Store the API data returned by the API in Redis.
[0010] Furthermore, it also includes:
[0011] Set up a scheduled task and regularly update the API data in Redis according to the scheduled task.
[0012] Furthermore, it also includes: when any API data is adjusted, the corresponding entry in Redis is updated through the API interface.
[0013] Furthermore, it also includes: generating a log for recording the API data request process.
[0014] The present invention discloses a dynamic data cache update system based on Redis, comprising:
[0015] The preprocessing module is used to use Redis as a cache layer and store the API data to be displayed in Redis;
[0016] The request module is used to check whether the API data to be requested exists in Redis when the user requests API data. If so, the API data cached in Redis is directly returned. When the API data to be requested does not exist in Redis, a request is initiated to the API and the API data returned by the API is output.
[0017] The further improvement of the Redis-based dynamic data cache update system of the present invention is:
[0018] Furthermore, it also includes:
[0019] The storage module is used to store the API data returned by the API in Redis.
[0020] Furthermore, it also includes:
[0021] The update module is used to set a scheduled task and regularly update the API data in Redis according to the scheduled task.
[0022] The present invention discloses a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the Redis-based dynamic data cache update method are implemented.
[0023] The present invention discloses a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the dynamic data cache update method based on Redis are implemented.
[0024] The present invention has the following beneficial effects:
[0025] In specific operation, the Redis-based dynamic data cache update method, system, device and medium described in the present invention first obtains API data from the cache layer when a user requests it. When the requested API data does not exist in the cache layer, a request is sent to the API to obtain the API data from the API, thereby avoiding that all API data requests are obtained from the API, thereby solving the problem of increased server load and prolonged response time caused by frequent requests for API data.
[0026] Furthermore, when a request is sent to an API, the API data returned by the API is stored in a cache layer for next use. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] The accompanying drawings constituting a part of the present invention are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the accompanying drawings:
[0028] Figure 1 The figure is a flow chart of the method of the present invention. DETAILED DESCRIPTION
[0029] 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 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.
[0030] In the description of the present invention, it should be understood that the terms “include” and “comprises” indicate the presence of described features, wholes, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or collections thereof.
[0031] It should also be understood that the terms used in the present specification are only for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in the present specification and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include plural forms.
[0032] It should be further understood that the term "and / or" used in the present specification and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, and includes these combinations. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in the present invention generally indicates that the associated objects are in an "or" relationship.
[0033] It should be understood that, although the terms first, second, third, etc. may be used to describe preset ranges, etc. in the embodiments of the present invention, these preset ranges should not be limited to these terms. These terms are only used to distinguish preset ranges from each other. For example, without departing from the scope of the embodiments of the present invention, the first preset range may also be referred to as the second preset range, and similarly, the second preset range may also be referred to as the first preset range.
[0034] The word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrases "if it is determined" or "if (stated condition or event) is detected" may be interpreted as "when it is determined" or "in response to determining" or "when detecting (stated condition or event)" or "in response to detecting (stated condition or event)", depending on the context.
[0035] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of the embodiments. The components of the embodiments of the present invention described and shown in the drawings here can usually be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present invention provided in the drawings is not intended to limit the scope of the claimed invention, but merely represents selected embodiments of the present invention. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0036] Various structural schematic diagrams of the embodiments disclosed in the present invention are shown in the accompanying drawings. These figures are not drawn to scale, and some details are magnified and some details may be omitted for the purpose of clear expression. The shapes of various regions and layers shown in the figures and the relative sizes and positional relationships therebetween are only exemplary, and may deviate in practice due to manufacturing tolerances or technical limitations, and those skilled in the art may additionally design regions / layers with different shapes, sizes, and relative positions according to actual needs.
[0037] As is known to all, Redis is an open source, high-performance key-value database. The following is a detailed introduction to Redis: Definition: Redis, the full name of Remote Dictionary Server, is an open source log-type, Key-Value database written in ANSI C language, supporting the network, based on memory and can be persistent, and provides APIs in multiple languages. Features: Fast speed: All data in Redis is stored in memory, so the reading and writing speed is very fast. Rich data structure: The value in Redis can not only be a string, but also a specific data structure, such as hash, list, set, ordered set (zset), bitmap, HyperLogLog and GEO (geographic information positioning), etc. These data structures enable Redis to meet the needs of many application scenarios. Persistence: Redis provides two persistence methods, namely RDB and AOF, which can save data in memory to the hard disk to ensure the persistence of data. High scalability: Redis supports master-slave replication and distributed clusters, which can achieve high availability, read-write separation and capacity expansion. Multiple client languages: Redis provides a simple TCP communication protocol. Many programming languages can be easily connected to Redis. There are also many client languages that support Redis, almost covering all mainstream programming languages.
[0038] API, the full name of Application Programming Interface, is a set of definitions, programs and protocols. These sets are mainly used to achieve communication between software applications. Through API, developers do not need to build functions from scratch, but can call existing API interfaces to quickly develop feature-rich and highly interactive applications.
[0039] The main functions and roles of API: Data interaction: API is a bridge for data exchange between software. Through API, different software programs can share and exchange data, thereby achieving functional integration and data synchronization. Functional expansion: API allows developers to quickly add new functions and services based on existing software by calling external API interfaces. This not only saves development time, but also improves the flexibility and scalability of the software. Cross-platform compatibility: API can help developers achieve cross-platform functional compatibility. By calling a unified API interface, developers can ensure that software on different operating systems and different devices can achieve the same functions. Improve development efficiency: API provides developers with a rich library of functions and tools, which can help developers quickly build applications and improve development efficiency. Promote innovation: The openness and scalability of API provide developers with unlimited imagination. By calling and combining different API interfaces, developers can create unprecedented new applications and services.
[0040] Embodiment 1
[0041] refer to Figure 1 The Redis-based dynamic data cache update method of the present invention comprises the following steps:
[0042] Use Redis as the cache layer and store the API data to be displayed in Redis. When a user requests API data, first check whether the API data to be requested exists in Redis. If so, directly return the API data cached in Redis. When the API data to be requested does not exist in Redis, initiate a request to the API and store the API data returned by the API in Redis.
[0043] As an implementation mode of the present invention, this embodiment also includes: setting a scheduled task, regularly updating the data in Redis according to the scheduled task, and the scheduled task can be configured according to business needs to ensure the timeliness and accuracy of the data.
[0044] As an implementation mode of the present invention, this embodiment also includes: when any data is adjusted, the corresponding entry in Redis is updated through the API interface without updating the entire cache, which can significantly reduce the time of the update operation, avoid affecting other data, and improve dynamics and performance.
[0045] As an implementation mode of the present invention, this embodiment also includes: when the update fails, re-updating is performed through a retry mechanism to ensure the final consistency of the data.
[0046] As an implementation mode of the present invention, this embodiment also includes: generating a log according to the update process to facilitate operation and maintenance monitoring and troubleshooting.
[0047] Embodiment 2
[0048] The Redis-based dynamic data cache update system of the present invention includes:
[0049] The preprocessing module is used to use Redis as a cache layer and store the API data to be displayed in Redis;
[0050] The request module is used to check whether the API data to be requested exists in Redis when the user requests API data. If so, the API data cached in Redis is directly returned. When the API data to be requested does not exist in Redis, a request is initiated to the API and the API data returned by the API is output.
[0051] As an embodiment of the present invention, it also includes:
[0052] The storage module is used to store the API data returned by the API in Redis.
[0053] As an embodiment of the present invention, it also includes:
[0054] The update module is used to set a scheduled task and regularly update the API data in Redis according to the scheduled task.
[0055] The division of modules in the embodiments of the present application is schematic and is only a logical function division. There may be other division methods in actual implementation. In addition, each functional module in each embodiment of the present application may be integrated into a processor, or may exist physically separately, or two or more modules may be integrated into one module. The above-mentioned integrated modules may be implemented in the form of hardware or in the form of software functional modules.
[0056] Embodiment 3
[0057] A computer device comprises a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the dynamic data cache update method based on Redis are implemented, for example, comprising: using Redis as a cache layer to store API data to be displayed in Redis; when a user requests API data, first checking whether the API data to be requested exists in Redis, and if so, directly returning the API data cached in Redis; when the API data to be requested does not exist in Redis, initiating a request to the API and outputting the API data returned by the API; before initiating a request to the API and outputting the API data returned by the API when the API data to be requested does not exist in Redis, the method further comprises: storing the API data returned by the API in Redis; further comprising: setting a scheduled task to regularly update the API data in Redis according to the scheduled task; further comprising: when any API data is adjusted, updating the corresponding entry in Redis through the API interface; further comprising: generating a log for recording the process of API data request. The memory may include a memory, such as a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk memory, etc. The processor, network interface, and memory are interconnected through an internal bus, and the internal bus may be an industrial standard architecture bus, a peripheral component interconnection standard bus, an extended industrial standard architecture bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. The memory is used to store programs. Specifically, the program may include a program code, and the program code includes computer operation instructions. The memory may include a memory and a non-volatile memory, and provide instructions and data to the processor.
[0058] Embodiment 4
[0059] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the dynamic data cache update method based on Redis are implemented, for example, using Redis as a cache layer, storing the API data to be displayed in Redis; when a user requests API data, first check whether the API data to be requested exists in Redis, and if so, directly return the API data cached in Redis; when the API data to be requested does not exist in Redis, initiate a request to the API, and output the API data returned by the API; when the API data to be requested does not exist in Redis, initiate a request to the API, and output the API data returned by the API, the method also includes: storing the API data returned by the API in Redis; setting a scheduled task, and regularly updating the API data in Redis according to the scheduled task; when any API data is adjusted, updating the corresponding entry in Redis through the API interface; and generating a log for recording the process of API data request. Specifically, the computer-readable storage medium includes, but is not limited to, for example, volatile memory and / or non-volatile memory. The volatile memory may include a random access memory (RAM) and / or a cache memory (cache), etc. The non-volatile memory may include a read-only memory (ROM), a hard disk, a flash memory, an optical disk, a magnetic disk, etc.
[0060] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0061] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0062] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0063] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0064] Those skilled in the art will readily appreciate other embodiments of the present invention after considering the specification and disclosure of the invention. This application is intended to cover any variations, uses or adaptations of the present invention that follow the general principles of the present invention and include common knowledge or customary techniques in the art that are not disclosed by the present invention. The specification and examples are to be considered exemplary only, and the true scope and spirit of the present invention are indicated by the following claims.
[0065] It should be understood that the present invention is not limited to the exact construction that has been described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the present invention is limited only by the appended claims.
[0066] The above description is only a preferred embodiment of the present invention and does not limit the present invention in any way. Any simple modification, change and equivalent structural change made to the above embodiment based on the technical essence of the present invention still falls within the protection scope of the technical solution of the present invention.
Claims
1. A dynamic data cache update method based on Redis, characterized in that: include: Use Redis as the cache layer and store the API data to be displayed in Redis; When a user requests API data, it first checks whether the requested API data exists in Redis. If so, it directly returns the API data cached in Redis. When the requested API data does not exist in Redis, it initiates a request to the API and outputs the API data returned by the API.
2. The Redis-based dynamic data cache update method according to claim 1, characterized in that: When the requested API data does not exist in Redis, a request is made to the API, and before the API data returned by the API is output, the following steps are also included: Store the API data returned by the API in Redis.
3. The dynamic data cache update method based on Redis according to claim 1, characterized in that: Also includes: Set up a scheduled task and regularly update the API data in Redis according to the scheduled task.
4. The Redis-based dynamic data cache update method according to claim 1, characterized in that: Also includes: When any piece of API data is adjusted, the corresponding entry in Redis is updated through the API interface.
5. The Redis-based dynamic data cache update method according to claim 1, characterized in that: Also includes: Generates logs for recording the API data request process.
6. A dynamic data cache update system based on Redis, characterized in that: include: The preprocessing module is used to use Redis as a cache layer and store the API data to be displayed in Redis; The request module is used to check whether the API data to be requested exists in Redis when the user requests API data. If so, the API data cached in Redis is directly returned. When the API data to be requested does not exist in Redis, a request is initiated to the API and the API data returned by the API is output.
7. The Redis-based dynamic data cache update system according to claim 6, characterized in that: Also includes: The storage module is used to store the API data returned by the API in Redis.
8. The Redis-based dynamic data cache update system according to claim 6, characterized in that: Also includes: The update module is used to set a scheduled task and regularly update the API data in Redis according to the scheduled task.
9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the Redis-based dynamic data cache update method are implemented as described in any one of claims 1 to 5.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the dynamic data cache update method based on Redis are implemented as described in any one of claims 1 to 5.