Caching method, system and device based on AOP and medium
By introducing AOP-based cache methods and Redis cache annotation classes in the business system, dynamically generate cache keys, the shortcomings of traditional cache strategies in high concurrency and dynamic business scenarios are solved, efficient and flexible cache management is achieved, and system performance and user experience are improved.
Patent Information
- Application Number
- CN202510064488.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-15
- Publication Date
- 2025-05-30
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Traditional data caching strategies are difficult to meet the needs of fast response and precise caching in high concurrency and dynamic business scenarios, resulting in low cache hit rate and excessive database load, which affects user experience.
Using AOP-based caching method, the automatic processing and unified management of cache logic is realized by defining Redis cache annotation classes and configuring AOP tangent points. Dynamically generated cache keys use Spring EL expressions to generate accurate cache keys based on real-time business parameters.
It improves the cache hit rate, reduces the database I/O operation, meets the needs of fast response in high concurrency scenarios, reduces development and maintenance costs, and improves user experience.
Smart Images

Figure CN120067155A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of cache applications, and particularly relates to a cache method, system, device and medium based on AOP. Background Art
[0002] As the data processing requirements in business systems are getting higher and higher, the impact on data processing speed is increasing. However, the traditional data cache strategy obviously cannot meet the requirements and has many limitations. On the one hand, many business systems use hard-coded methods to implement cache logic, directly embedding cache operations into business code. This way leads to extremely high code coupling. Once the cache requirements change, such as adjusting the generation rule of cache keys or replacing the cache storage medium, developers have to modify the business code, which is not only time-consuming and laborious, but also very likely to introduce new errors, and also makes the system less maintainable.
[0003] On the other hand, for data cache processing in dynamic business scenarios, the existing methods have low flexibility. Taking an e-commerce system as an example, during promotional activities, the query frequency of commodity data will soar instantly, and the cache requirements for different commodity categories and different user groups are different. However, traditional cache methods are difficult to dynamically generate accurate cache keys based on real-time business parameters (such as user region, popularity of commodities, etc.), cannot achieve accurate caching, often result in low cache hit rates, and a large number of business requests still need to frequently access the database, ultimately causing the database to be overloaded, the business system to respond slowly, and seriously affecting the user experience. Summary of the Invention
[0004] In a first aspect, an embodiment of the present application provides a cache method based on AOP, including the following steps: S1. Define a Redis cache annotation class for the business system, configure the AOP pointcut, initialize the Redis template, and write cache processing logic with around advice as an AOP interceptor; S2. After the backend service receives a business request, trigger the cache processing logic interception through the AOP pointcut, and verify whether the business request is a cache business according to the existence of the Redis cache annotation class; S3. When the business request is a cache business, parse the parameters of the Redis cache annotation class in the business request to generate a Redis cache key; S4. Query in the Redis cache service using the Redis cache key, check whether there is matching cache data, and return the matching cache data to the frontend service; When there is no matching cached data in the Redis service, query business data from the database service according to the around advice of the cache processing logic, return the queried business data to the front-end service, and add a Redis cache key for the business data to the Redis cache service.
[0005] Further, the specific steps of step S1 are as follows: S11. Design a custom Redis cache annotation class for the business system, and set the Redis cache annotation class to take effect during the business processing method class and program runtime; S12. Define attributes for configuring the cache key prefix and attributes of the Spring EL expression for dynamically generating cache keys for the Redis cache annotation class; S13. Add the Redis cache annotation class to the business that needs to perform data caching; S14. Configure the AOP pointcut for the business processing method that needs to add the Redis cache annotation class for cache processing; S15. Initialize the Redis template, configure the connection factory, serialization mechanism, and transaction manager; S16. Write the cache processing logic as an around advice and use the around advice as an AOP interceptor.
[0006] Further, the specific steps of step S2 are as follows: S21. After the back-end service receives the business request from the front-end service, enter the around advice of the AOP pointcut; S22. Obtain the signature of the current business request according to the cache processing logic corresponding to the around advice, and extract the business object; S23. Detect whether the Redis cache annotation class is added to the business processing method corresponding to the business object; If so, enter step S3; If not, enter step S24; S24. Determine that the business request does not need to be cached, execute the business request and return it to the front-end service, and end.
[0007] Further, the business object includes the business name, parameter type, and return type.
[0008] Further, the specific steps of step S3 are as follows: S31. Extract the attribute value of the cache key prefix and the attribute value of the Spring EL expression for dynamically generating the cache key from the Redis cache annotation class of the business processing method corresponding to the business request; S32. Initialize the Spring EL expression parser, create an expression context, and obtain the parameter name array of the business request; S33. Obtain the actual parameter value array of the service request, and map the parameter name array and the actual parameter value array to the expression context; S34. Parse the property value of the Spring EL expression for dynamically generating the cache key of the service request, generate the dynamic parameter value according to the actual parameter value, and splice the dynamic parameter value with the property value of the cache key prefix to generate the Redis cache key.
[0009] Further, the specific steps of step S4 are as follows: S41. Query in the Redis cache service using the Redis cache key to check whether there is the same Redis cache key; If so, go to step S42; If not, go to step S5; S42. Obtain the cache data matched by the same Redis cache key, and return it to the front-end service, and end.
[0010] Further, the specific steps of step S5 are as follows: S51. Process the service request according to the business processing logic that continues to execute in the around advice of the cache processing logic; S52. Query the business data from the database service according to the service request; S53. Determine whether the query result is empty; If so, go to step S54; If not, go to step S55; S54. Send a failure of the service request to the front-end service, and end; S55. Return the business data in the query result to the front-end service; S56. Add the Redis cache key to the queried business data, and add the business data and the corresponding Redis cache key to the Redis service.
[0011] In a second aspect, the embodiments of the present application further provide an AOP-based cache system, including: Redis annotation definition and initialization module, which is used to define the Redis cache annotation class for the business system, configure the AOP pointcut, initialize the Redis template, and write the cache processing logic with around advice as the AOP interceptor; Business request cache check module, which is used to trigger the cache processing logic interception through the AOP pointcut after the back-end service receives the business request, and verify whether the business request is a cache business according to the existence of the Redis cache annotation class; A parameter parsing and cache key generation module, which is used to parse the parameters of the Redis cache annotation class in the service request when the service request is a cache service, and generate a Redis cache key; A cache query module, which is used to query in the Redis cache service using the Redis cache key, check whether there is matching cache data, and return the matching cache data to the front-end service; A database query and cache writing module, which is used to query service data from the database service according to the around advice of the cache processing logic when there is no matching cache data in the Redis service, return the queried service data to the front-end service, and add the Redis cache key set for the service data to the Redis cache service.
[0012] Thirdly, an embodiment of the present application further provides an electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, the steps of the AOP-based caching method described in the first aspect are implemented.
[0013] Fourthly, an embodiment of the present application further provides a storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the AOP-based caching method described in the first aspect are implemented.
[0014] From the above technical solutions, the following advantages of the present invention can be seen: In the AOP-based caching method, system, device and medium provided by the present application, through the combination of AOP technology and Redis cache annotation class, a caching mechanism is introduced in the service request processing flow; so that repeated service data queries can directly obtain results from the Redis cache, avoiding frequent inefficient access to the database, reducing the I / O operations of the database, meeting the business requirements of fast response in high-concurrency scenarios, and improving the user experience; the present application decouples the cache logic from the service code and uses custom annotations and AOP aspects for unified management and control, enabling developers to only focus on the relevant configurations of the annotations and aspects, reducing the development and maintenance costs; the present application uses the Spring EL expression to implement dynamic cache key generation, which can accurately generate cache keys according to various parameters carried by the service request in real time, ensure the matching degree of cache data and services, improve the cache hit rate, and reduce the space occupied by invalid caches; the expansion of the edge service system function module of the present application, new business logics only need to add corresponding Redis cache annotations as needed, and can be seamlessly connected to the existing cache system without large-scale reconstruction of the overall architecture, improving the expansion performance of the service system. Description of the Drawings
[0015] To more clearly illustrate the technical solution of the present invention, the accompanying drawings required in the description will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other accompanying drawings can be obtained based on these drawings.
[0016] Figure 1 It is a schematic flow chart of the caching method based on AOP of the present invention.
[0017] Figure 2 It is a schematic diagram of the caching system based on AOP of the present invention. Detailed embodiments
[0018] In the following, the specific steps of the caching method based on AOP will be described in detail, and various embodiments of the present disclosure will be described more comprehensively. The present disclosure can have various embodiments, and adjustments and changes can be made therein. However, it should be understood that there is no intention to limit the various embodiments of the present disclosure to the specific embodiments disclosed herein, but the present disclosure should be understood to cover all adjustments, equivalents, and / or alternative solutions falling within the spirit and scope of the various embodiments of the present disclosure.
[0019] Exemplarily, with the increasing demand for data processing in business systems, the requirement for processing speed is getting higher and higher. The traditional data caching strategy has gradually shown its limitations and is difficult to meet the current needs. The primary problem is that many business systems directly embed the caching logic into the business code in a hard-coded manner, which greatly increases the coupling degree of the code. Whenever the caching requirements are adjusted, such as changing the generation logic of the cache key or replacing the cache storage method, developers need to modify the business code deeply, which is not only time-consuming and error-prone but also reduces the maintainability of the system.
[0020] In addition, when dealing with data caching in dynamic business scenarios, the traditional method is particularly rigid. Taking the promotion activities of an e-commerce platform as an example, the query volume of commodity data during the activity period will increase sharply, and the caching requirements for different commodity categories and user groups are different. However, the traditional caching strategy is difficult to flexibly generate accurate cache keys according to real-time business parameters (such as the location of the user, the popularity of the commodity, etc.), resulting in a low cache hit rate. A large number of business requests still need to frequently access the database, which not only increases the burden on the database but also prolongs the response time of the business system and affects the user experience.
[0021] To address the above problems, this embodiment provides a caching method based on AOP, which realizes the automated processing and unified management of caching logic by introducing AOP technology and Redis caching annotation classes.
[0022] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0023] Please refer to Figure 1 The figure shows a flowchart of a caching method based on AOP in a specific embodiment. The method includes the following steps: S1. Define a Redis cache annotation class for the business system, configure the AOP pointcut, initialize the Redis template, and write cache processing logic with around advice as an AOP interceptor; It should be noted that Redis is a high-performance in-memory database that supports key-value pair storage and is suitable for scenarios such as caching and message middleware; AOP is short for Aspect Oriented Programming, which is aspect-oriented programming. It realizes the unified maintenance of program functions through pre-compilation and runtime dynamic proxy and is often used for logging, transaction management, performance monitoring, etc.; By defining the Redis cache annotation class and configuring the AOP pointcut, the automatic injection and unified management of cache logic are realized, reducing the workload of manual coding; at the same time, initializing the Redis template and writing cache processing logic with around advice provide a basis for subsequent cache processing; S2. After the backend service receives a business request, it triggers the cache processing logic interception through the AOP pointcut and verifies whether the business request is a cache business according to the existence of the Redis cache annotation class; It should be noted that by triggering the cache processing logic interception through the AOP pointcut, the automatic triggering of cache logic is realized, and there is no need for business code to actively call the cache function, reducing the complexity of business development; accurately identifying cache business based on the AOP pointcut can timely start the cache processing process, improving the response timeliness of the business system to cache requirements; verifying whether the business request is a cache business according to the existence of the Redis cache annotation class can quickly identify business requests that require cache optimization, improve the processing speed of requests without cache requirements, ensure that the business system resources are concentrated on business scenarios that require caching, and improve the overall resource utilization efficiency; S3. When the business request is a cache business, parse the parameters of the Redis cache annotation class in the business request to generate a Redis cache key; It should be noted that by parsing the parameters of the Redis cache annotation class in the business request, the key information carried by the business request can be obtained, which is transformed into the elements required for generating the cache key to ensure that the cache key can accurately reflect the business characteristics and improve the recognizability of the cached data; by parsing the parameters of the Redis cache annotation class, the dynamic generation of the Redis cache key is realized, improving the accuracy of the cache key. At the same time, the generated Redis cache key provides a basis for subsequent cache query and update; S4. Query in the Redis cache service using the Redis cache key, check whether there is matching cached data, and return the matching cached data to the front-end service; It should be noted that by querying in the Redis cache service using the Redis cache key, the high efficiency of the Redis cache service can be utilized to improve the speed of finding whether there is matching cached data, giving full play to the high-performance read and write advantages of the Redis in-memory database, transforming the original database query operation into a fast memory query, shortening the data acquisition time, and improving the response speed of the business system; checking whether there is matching cached data and returning the matching cached data to the front-end service so that when the cache is hit, the cached data can be directly pushed to the front-end service, avoiding the repeated execution of business logic and the time-consuming process of database query, improving the speed of meeting user requests, and reducing the back-end business processing pressure and database load; S5. When there is no matching cached data in the Redis service, query the business data from the database service according to the around advice of the cache processing logic, return the queried business data to the front-end service, and set the Redis cache key for the business data and add it to the Redis cache service; It should be noted that when there is no matching cached data in the Redis service, querying the business data from the database service according to the around advice of the cache processing logic can ensure that the business system can still obtain the latest data according to the traditional database query when the cache is not hit, ensuring the normal operation of the business and maintaining the integrity of the functions of the business system; by returning the queried business data to the front-end service, it can ensure that the front-end users can finally obtain the required business data and maintain the service quality of the business system; and setting the Redis cache key for the business data and adding it to the Redis cache service, so that after the new data is obtained through database query, it can be cached in time, laying a foundation for the fast response of subsequent identical business requests, realizing the self-optimization and dynamic update of the cache, improving the cache hit rate of the business system, and reducing the overall business processing cost.
[0024] In this embodiment, by defining a Redis cache annotation class and configuring the AOP pointcut, the automated processing of cache logic is realized, reducing the workload of manual coding, improving the maintainability and scalability of the code. At the same time, using Redis as the cache storage intermediary improves the speed and efficiency of data access.
[0025] Further, as a refinement and extension of the specific implementation manner of the above embodiment, in order to fully illustrate the specific implementation process in this embodiment, another AOP-based cache method is provided. The method includes the following steps: S1. Define a Redis cache annotation class for the business system, configure the AOP pointcut, initialize the Redis template, and write the cache processing logic with around advice as the AOP interceptor. The specific steps of step S1 are as follows: S11. Design a custom Redis cache annotation class for the business system and set the Redis cache annotation class to take effect during the business processing method class and program runtime. Exemplarily, design a custom Redis cache annotation class MyRedisCache, and add the annotation @Target(ElementType.METHOD), indicating that this annotation takes effect during method runtime, and add the annotation @Retention(RetentionPolicy.RUNTIME) indicating that this annotation takes effect during program runtime. S12. Define the attributes for configuring the cache key prefix and the Spring EL expression for dynamically generating the cache key for the Redis cache annotation class. Exemplarily, define the cache key prefix keyPrefix and the Spring EL expression matchValue attribute for dynamically generating the cache key in the Redis cache annotation class, so that the annotation can be applied to different business scenarios. S13. Add the Redis cache annotation class to the business that needs to perform data caching. Exemplarily, add the @RedisCache annotation to the method that needs to perform data caching, such as @RedisCache(keyPrefix = "user",matchValue = "#id"), where #id is a placeholder used to resolve and obtain the value corresponding to the placeholder at runtime. S14. Configure the AOP pointcut for the business processing method that needs to add the Redis cache annotation class for cache processing. Exemplarily, define an aspect class, add the @Aspect annotation to the aspect class to indicate that this class is an aspect class, and configure the Redis cache annotation class as the AOP pointcut. Thus, based on precise rule matching, it can be ensured that the business processing method logic using the Redis cache annotation class will be intercepted and cached; S15. Initialize the Redis template, configure the connection factory, serialization mechanism, and transaction manager; It should be noted that through the initialization of the Redis template, the stability and execution efficiency of Redis operations are ensured; S16. Write the cache processing logic as a around advice, and use the around advice as an AOP interceptor; Exemplarily, write the cache processing logic such as the doCache method as the around advice. As the core part of the around advice as an AOP interceptor, complex cache logic can be implemented, including querying, writing, and exception handling; It should be noted that through the definition and configuration process of the Redis cache annotation class, the cache implementation of the business system is ensured to proceed smoothly. By defining the cache key prefix and the Spring EL expression attribute for dynamically generating cache keys for the annotation class, the flexibility and configurability of the cache key are improved, and the cache hit rate and utilization rate are increased; S2. After the backend service receives a business request, trigger the cache processing logic interception through the AOP pointcut, and verify whether the business request is a cache business according to the existence of the Redis cache annotation class. The specific steps of step S2 are as follows: S21. After the backend service receives the business request from the frontend service, enter the around advice of the AOP pointcut; Exemplarily, when the user clicks on the page, the frontend service sends a data acquisition request to the backend service. For example, the id in the request parameters is 1. After the backend service receives the request from the frontend, it enters the around advice of the aspect. The ProceedingJoinPoint in the around advice is passed as a parameter for method enhancement; S22. Obtain the signature of the current business request according to the cache processing logic corresponding to the around advice, and extract the business object; the business object includes the business name, parameter type, and return type; Exemplarily, call the method signature information of the ProceedingJoinPoint to obtain the connection point, that is, the Signature object. This object contains detailed information about the connection point, such as the method name, parameter type, return type, etc.; S23. Detect whether the business processing method corresponding to the business object adds the Redis cache annotation class; If so, enter step S3; If not, enter step S24; S24. Determine that the service request does not need to be cached, execute the service request and return it to the front-end service, and end; Exemplarily, call the getMethod method of Signature to obtain the Method object of the method proxied in step S14. The Method object contains the method name, parameter types, return type, modifiers, etc. Then, obtain the method annotation of the proxy in step S14 through the getAnnotation method of the Method object; It should be noted that by triggering the cache processing logic interception through the AOP pointcut, the automatic cache check of the service request is realized, avoiding the cumbersome process of manually judging the cache requirements; at the same time, whether to perform cache processing is determined according to whether the business object adds the Redis cache annotation class, improving the accurate and reliable identification of the cache logic; S3. When the service request is a cache service, parse the parameters of the Redis cache annotation class in the service request to generate a Redis cache key. The specific steps of step S3 are as follows: S31. Extract the attribute value of the cache key prefix and the attribute value of the Spring EL expression for dynamically generating the cache key from the Redis cache annotation class of the service request corresponding business processing method; Exemplarily, after obtaining the parameters of the Redis cache annotation class in the service request, then extract the keyPrefix and matchValue attribute values for subsequent generation of a unique cache key; S32. Initialize the Spring EL expression parser, create an expression context, and obtain the parameter name array of the service request; Exemplarily, initialize the Spring EL expression parser SpelExpressionParser for parsing expressions containing placeholders; create an expression context StandardEvaluationContext, and use the DefaultParameterNameDiscoverer to obtain the parameter name array of the target method to provide the runtime environment and parameter information for parsing the expression; S33. Obtain the actual parameter value array of the service request and map the parameter name array and the actual parameter value array to the expression context; It should be noted that by mapping the parameter name and parameter value to the expression context, it is ensured that the correct parameter value can be accessed during expression parsing; S34. Parse the attribute value of the Spring EL expression for dynamically generating the cache key of the service request, generate dynamic parameter values according to the actual parameter values, and splice the dynamic parameter values with the attribute value of the cache key prefix to generate a Redis cache key; It should be noted that by generating a cache key, parsing the parameters of the Redis cache annotation class and the Spring EL expression, the dynamic generation of the cache key is realized, and the cache key is flexibly and accurately implemented; S4. Query in the Redis cache service using the Redis cache key, check whether there is matching cache data, and return the matching cache data to the front-end service; The specific steps of step S4 are as follows: S41. Query in the Redis cache service using the Redis cache key to check whether the same Redis cache key exists; If so, go to step S42; If not, go to step S5; S42. Obtain the cache data matching the same Redis cache key and return it to the front-end service, and end; It should be noted that by querying in the Redis cache service using the Redis cache key, the fast access and return of the cache data are realized, improving the speed and efficiency of data access; At the same time, according to the query result, it is decided whether to continue to execute the database query, avoiding unnecessary database access and reducing the burden on the database; S5. When there is no matching cache data in the Redis service, query the business data from the database service according to the around advice of the cache processing logic, return the queried business data to the front-end service, and add the Redis cache key for the business data to the Redis cache service; The specific steps of step S5 are as follows: S51. Process the business request according to the business processing logic that continues to execute in the around advice of the cache processing logic; S52. Query the business data from the database service according to the business request; S53. Judge whether the query result is empty; If so, go to step S54; If not, go to step S55; S54. Send a business request failure to the front-end service and end; S55. Return the business data in the query result to the front-end service; S56. Add a Redis cache key to the queried business data, and add the business data and the corresponding Redis cache key to the Redis service; Exemplarily, if the Redis cache service query misses, the target business logic is executed through the proceed method of the ProceedingJoinPoint to obtain the execution result of the method. Check whether the execution result is empty. If it is empty, return the default value; if the execution result is not empty, serialize the result and write it into the Redis cache service so that subsequent business requests can directly obtain data from the cache; and return the business data of the execution result to the front-end service. After receiving the data, the front-end service renders and displays it to the user. It should be noted that when there is no matching cache data in the Redis service, the business data is queried from the database service according to the around advice of the cache processing logic, and the query result is added to the Redis cache service, realizing the automatic update and supplement of the cache.
[0026] It should be understood that the magnitudes of the sequence numbers of the steps in the above embodiments do not mean 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 to the implementation process of the embodiments of the present invention.
[0027] As Figure 2 shown, the following are embodiments of the AOP-based cache system provided by the embodiments of the present disclosure. This system and the AOP-based cache methods of the above embodiments belong to the same inventive concept. For the details not described in detail in the embodiments of the AOP-based cache system, reference may be made to the embodiments of the AOP-based cache method above.
[0028] The system includes: A Redis annotation definition and initialization module, which is used to define a Redis cache annotation class for the business system, configure the AOP pointcut, initialize the Redis template, and write the cache processing logic with around advice as an AOP interceptor; A business request cache check module, which is used to trigger the cache processing logic interception through the AOP pointcut after the back-end service receives a business request, and verify whether the business request is a cache business according to the existence of the Redis cache annotation class; A parameter parsing and cache key generation module, which is used to parse the parameters of the Redis cache annotation class in the business request and generate a Redis cache key when the business request is a cache business; A cache query module, which is used to query in the Redis cache service using the Redis cache key, check whether there is matching cache data, and return the matching cache data to the front-end service; The database query and cache writing module is used to query business data from the database service according to the around advice of the cache processing logic when there is no matching cache data in the Redis service, return the queried business data to the front-end service, and add a Redis cache key for the business data to the Redis cache service.
[0029] In this embodiment, through the design of the Redis annotation definition and initialization module, the business request cache check module, the parameter parsing and cache key generation module, the parameter parsing and cache key generation module, the cache query module, and the database query and cache writing module, the cache processing logic is divided into different functional modules, realizing the process and automation of cache processing. At the same time, each module works together to improve the overall performance stability of the cache.
[0030] The AOP-based caching method provided by the embodiments of the present application can be applied to electronic devices. Those skilled in the art can understand that the electronic device structure involved in the embodiments of the present invention does not constitute a limitation on the electronic device. The electronic device may include more or fewer components than shown in the figure, or combine certain components, or have different component arrangements. In the embodiments of the present invention, the electronic device includes, but is not limited to, laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as personal digital processors, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown in the figure, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the embodiments of the present application described and / or claimed herein.
[0031] The electronic device may include a processor, an external memory interface, an internal memory, a universal serial bus (USB) interface, a charging management module, a power management module, a battery, a wireless communication module, an audio module, a speaker, a microphone, a sensor module, a key, a camera, a display screen, and a SIM card interface, etc.
[0032] It can be understood that the structure schematically shown in the embodiments of the present application does not constitute a specific limitation on the electronic device. In other embodiments of the present application, the electronic device may include more or fewer components than shown in the figure, or combine certain components, or split certain components, or have different component arrangements. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0033] The processor may include one or more processing units. For example, the processor may include a central processing unit (CPU), an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Among them, different processing units may be independent devices or integrated in one or more processors.
[0034] Among them, the processor may be the nerve center and command center of the electronic device. The controller may generate operation control signals according to the instruction operation code and timing signal to complete the control of fetching and executing instructions.
[0035] A memory may also be provided in the processor for storing instructions and data. In some embodiments, the memory in the processor is a cache memory. This memory can save the instructions or data that the processor has just used or recycled. If the processor needs to use the instruction or data again, it can directly call it from this memory. This avoids repeated accesses, reduces the waiting time of the processor, and thus improves the system efficiency.
[0036] The above-mentioned electronic device implements the technical solution of the Redis cache annotation class defined for the business system, configuring the AOP pointcut, initializing the Redis template, and writing the cache processing logic with around advice as the AOP interceptor in the cache method based on AOP of the present application. After the backend service receives a business request, it triggers the cache processing logic interception through the AOP pointcut, and verifies whether the business request is a cache business according to the existence of the Redis cache annotation class. When the business request is a cache business, it parses the parameters of the Redis cache annotation class in the business request to generate a Redis cache key, queries in the Redis cache service using the Redis cache key to check if there is matching cache data, and returns the matching cache data to the frontend service. When there is no matching cache data in the Redis service, it queries the business data from the database service according to the around advice of the cache processing logic, returns the queried business data to the frontend service, and adds the business data with the Redis cache key set to the Redis cache service, achieving the beneficial effects of automating the cache logic processing, reducing the manual coding workload, improving the maintainability and scalability of the code. At the same time, using Redis as the cache storage intermediary improves the speed and efficiency of data access.
[0037] In the storage medium provided by the present application, there is a program product capable of implementing the cache method based on AOP.
[0038] The cache method based on AOP includes: defining a Redis cache annotation class for the business system, configuring the AOP pointcut, initializing the Redis template, and writing the cache processing logic with around advice as the AOP interceptor; after the backend service receives a business request, it triggers the cache processing logic interception through the AOP pointcut, and verifies whether the business request is a cache business according to the existence of the Redis cache annotation class; when the business request is a cache business, it parses the parameters of the Redis cache annotation class in the business request to generate a Redis cache key, queries in the Redis cache service using the Redis cache key to check if there is matching cache data, and returns the matching cache data to the frontend service; when there is no matching cache data in the Redis service, it queries the business data from the database service according to the around advice of the cache processing logic, returns the queried business data to the frontend service, and adds the business data with the Redis cache key set to the Redis cache service.
[0039] In some possible implementation manners, the cache method based on AOP of the present disclosure may be implemented in the form of a program product, which includes program code. When the program product runs on a terminal device, the program code is used to cause the terminal device to execute the steps according to various exemplary embodiments of the present disclosure described in the "Exemplary Method" section above of this specification.
[0040] The storage medium of the present disclosure may adopt any combination of one or more readable media. The readable media may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the above. More specific examples (a non-exhaustive list) of the readable storage medium include: an electrical connection having one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0041] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the broadest scope consistent with the principles and novel features disclosed herein.
Claims
1. A caching method based on AOP, characterized in that: The steps include: S1. Define the Redis cache annotation class for the business system, configure the AOP pointcut, initialize the Redis template, and write the cache processing logic with surround notification as an AOP interceptor; S2. After the backend service receives the business request, it triggers the cache processing logic interception through the AOP cut-off point, and verifies whether the business request is a cache business based on whether there is a Redis cache annotation class; S3. When the business request is a cache business, parse the parameters of the Redis cache annotation class in the business request to generate a Redis cache key; S4. Use the Redis cache key to query the Redis cache service, check whether there is matching cache data, and return the matching cache data to the front-end service; S5. When there is no matching cache data in the Redis service, query the database service for business data according to the surround notification of the cache processing logic, return the queried business data to the front-end service, and set the Redis cache key for the business data and add it to the Redis cache service.
2. The AOP-based caching method according to claim 1, characterized in that: The specific steps of step S1 are as follows: S11. Design a custom Redis cache annotation class for the business system, and set the Redis cache annotation class to take effect when the business processing method class and program are running; S12. Define the properties for configuring the cache key prefix and the properties for the SpringEL expression for dynamically generating the cache key for the Redis cache annotation class; S13. Add Redis cache annotation class for the business that needs data caching; S14. Configure AOP pointcuts for business processing methods that need to add Redis cache annotation classes for cache processing; S15. Initialize the Redis template, configure the connection factory, serialization mechanism, and transaction manager; S16. Write cache handling logic as around advice and use around advice as AOP interceptor.
3. The AOP-based caching method according to claim 2, characterized in that: The specific steps of step S2 are as follows: S21. After receiving the business request from the front-end service, the back-end service enters the surround notification of the AOP pointcut; S22. Obtain the signature of the current business request according to the cache processing logic corresponding to the surround notification and extract the business object; S23. Check whether the business processing method corresponding to the business object adds the Redis cache annotation class; If yes, go to step S3; If not, proceed to step S24; S24. Determine that the business request does not need to be cached, execute the business request and return it to the front-end service, and end.
4. The AOP-based caching method according to claim 3, characterized in that: The business object includes a business name, parameter type and return type.
5. The AOP-based caching method according to claim 3, characterized in that: The specific steps of step S3 are as follows: S31. Extract the attribute value of the cache key prefix and the attribute value of the Spring EL expression for dynamically generating the cache key from the Redis cache annotation class of the business processing method corresponding to the business request; S32. Initialize the Spring EL expression parser, create an expression context, and obtain the parameter name array of the business request; S33. Get the actual parameter value array of the business request, and map the parameter name array and the actual parameter value array to the expression context; S34. Parse the attribute value of the Spring EL expression of the dynamically generated cache key of the business request, generate a dynamic parameter value according to the actual parameter value, and concatenate the dynamic parameter value with the attribute value of the cache key prefix to generate a Redis cache key.
6. The AOP-based caching method according to claim 5, characterized in that: The specific steps of step S4 are as follows: S41. Use the Redis cache key to query the Redis cache service to check whether the same Redis cache key exists; If yes, go to step S42; If not, proceed to step S5; S42. Get the cache data matched by the same Redis cache key and return it to the front-end service, and then end.
7. The AOP-based caching method according to claim 6, characterized in that: The specific steps of step S5 are as follows: S51. Processing business requests according to the business processing logic that continues to be executed in the surrounding notification of the cache processing logic; S52. Query the database service for business data according to the business request; S53. Determine whether the query result is empty; If yes, go to step S54; If not, proceed to step S55; S54. Sending a business request to the front-end service fails, and the process ends; S55. Return the business data in the query results to the front-end service; S56. Add a Redis cache key for the queried business data, and add the business data and the corresponding Redis cache key to the Redis service.
8. An AOP-based cache system, characterized in that: include: Redis annotation definition and initialization module, which is used to define Redis cache annotation classes for business systems, configure AOP pointcuts, initialize Redis templates, and write cache processing logic with surround notifications as AOP interceptors; The business request cache check module is used to trigger the cache processing logic interception through the AOP cut-off point after the backend service receives the business request, and verify whether the business request is a cache business based on whether there is a Redis cache annotation class; The parameter parsing and cache key generation module is used to parse the parameters of the Redis cache annotation class in the business request and generate the Redis cache key when the business request is a cache business; The cache query module is used to query the Redis cache service using the Redis cache key, check whether there is matching cache data, and return the matching cache data to the front-end service; The database query and cache write module is used to query the business data from the database service according to the surround notification of the cache processing logic when there is no matching cache data in the Redis service, return the queried business data to the front-end service, and set the Redis cache key for the business data and add it to the Redis cache service.
9. An electronic device, characterized in that: The method comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the AOP-based caching method as claimed in any one of claims 1 to 7 when executing the program.
10. A storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the AOP-based caching method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Data cache type switching method and device, equipment, medium and program product
CN121455848A