Cache switching method based on configuration, electronic device, and storage medium

By customizing configuration files and rewriting the cache manager, combined with Spring's automatic registration mechanism, we achieved free switching of caching technologies, solved the problems of limited use of Spring annotations and high coupling of code operation methods, and reduced the development and time costs of cache switching.

CN115827049BActive Publication Date: 2026-05-12银雁科技服务集团股份有限公司
View PDF 1 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
银雁科技服务集团股份有限公司
Filing Date
2022-11-24
Publication Date
2026-05-12

AI Technical Summary

Technical Problem

In existing technologies, Spring annotation-based caching operations are limited to business methods and cannot be performed within the methods. Furthermore, switching caching technologies requires code modifications and service redeployment. The code operation methods are highly coupled, and switching caching technologies is costly, making it difficult to quickly meet changing requirements.

Method used

This paper provides a configuration-based cache switching method. The target cache manager can be selected through a custom configuration file. The AbstractCacheManager class is inherited and the getCache method is overridden. Combined with Spring's automatic registration mechanism, cache operation components are automatically loaded, enabling free switching of cache technologies.

Benefits of technology

It reduces the limitations of using Spring annotations in different scenarios and the development cost of cache switching, improves code readability and maintainability, and reduces the time cost of cache switching.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115827049B_ABST
    Figure CN115827049B_ABST
Patent Text Reader

Abstract

The embodiment of the application discloses a cache switching method based on configuration, which is suitable for spring annotation or code cache operation mode, and comprises the following steps: a configuration file is customized, wherein the configuration file comprises a cache definition type; a target cache manager is selected according to the configuration file, so as to generate a determined cache switching operation technical component; and the cache switching operation technical component is automatically loaded in a project, or a cache operation entrance is encapsulated, so that the code calls the cache switching operation technical component through the cache operation entrance. The cache free switching scheme based on configuration provided by the embodiment of the application is suitable for the spring annotation cache operation mode, reduces the limitation degree of the use scene, and also reduces the development cost of cache switching. For the code cache operation mode, the embodiment of the application can meet the cache switching demand, and can reduce the time cost of cache switching development.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and more specifically to a configuration-based cache switching method, electronic device, and storage medium. Background Technology

[0002] There are two main ways to use caching in project applications:

[0003] The first method: using the @Cacheable annotation provided by Spring;

[0004] The second method is to directly manipulate the cache through code, such as using RedisTemplate to manipulate the Redis cache.

[0005] However, the above two caching methods have the following advantages and disadvantages:

[0006] The first method uses caching annotations to manipulate the cache. Its advantages are ease of use; it only needs to be added to specific business methods, and different caching methods can be specified for different business methods. The disadvantages are that the annotation is loaded onto the business method, meaning it can only cache the overall result of the business method and cannot manipulate the cache within the business method itself, limiting its use cases. Furthermore, if a project needs to switch caching technologies, the code must first be modified, and then the service must be redeployed following the compilation, packaging, and deployment process.

[0007] The second approach involves manipulating the cache through code. Its advantages include high flexibility, allowing different caches to be operated on simultaneously within a single business method, thus satisfying more business scenarios. This is currently the most widely used method for cache operations. However, its disadvantages include high coupling, as cache operations are intertwined with business logic, and different caching technologies have different APIs, requiring separate coding for each technology. This makes switching caching technologies more costly, requiring rewriting all code and undergoing testing, compilation, packaging, and deployment before redeploying the service, hindering the ability to quickly meet changing requirements. Summary of the Invention

[0008] In view of the technical deficiencies mentioned in the background art, the purpose of this invention is to provide a configuration-based cache switching method, electronic device, and storage medium.

[0009] To achieve the above objectives, in a first aspect, embodiments of the present invention provide a configuration-based cache switching method applicable to Spring annotation-based cache operations. The method includes:

[0010] A custom configuration file, which includes cache definition types;

[0011] Select the target cache manager based on the configuration file to determine the cache operation technology components;

[0012] The caching operation technology component will be automatically loaded into the project.

[0013] As one specific implementation of this application, the target cache manager includes a Redis cache manager and a memory cache manager. Selecting the target cache manager according to the configuration file specifically includes:

[0014] Inherit from the AbstractCacheManager class and override the getCache method in the AbstractCacheManager class;

[0015] Select the target cache manager based on the configuration file and the overridden getCache method.

[0016] Optionally, as another specific implementation of this application, the target cache manager includes a DB cache manager, and the selection of the target cache manager according to the configuration file specifically includes:

[0017] Inherit from the AbstractCacheManager class and override the getCache method in the AbstractCacheManager class;

[0018] Define the database cache implementation class;

[0019] Select the target cache manager based on the configuration file, the overridden getCache method, and the cache implementation class.

[0020] Furthermore, as a specific implementation of this application, the caching operation technology component is automatically loaded into the project, specifically including:

[0021] Define an auto-configuration class and configure it in the spring.factories file;

[0022] The caching operation component is automatically loaded into the project using Spring's auto-registration mechanism.

[0023] Secondly, embodiments of the present invention also provide an electronic device, including a processor, an input device, an output device, and a memory, wherein the processor, the input device, the output device, and the memory are interconnected, wherein the memory is used to store a computer program, the computer program includes program instructions, and the processor is configured to invoke the program instructions to execute the steps of the method described in the first aspect above.

[0024] Thirdly, embodiments of the present invention provide a computer-readable storage medium storing a computer program, the computer program including program instructions, which, when executed by a processor, implement the steps of the method described in the first aspect above.

[0025] Fourthly, embodiments of the present invention provide another configuration-based cache switching method, applicable to code caching operations. This method includes:

[0026] A custom configuration file, which includes cache definition types;

[0027] Select the target cache manager based on the configuration file to generate the cache operation technology component;

[0028] The cache operation entry point is encapsulated so that code can call the cache operation technology component through the cache operation entry point.

[0029] As a specific implementation of this application, the cache operation entry point is encapsulated, specifically including:

[0030] The cache operation class is uniformly encapsulated to provide a unified entry point for cache operations.

[0031] Fifthly, embodiments of the present invention also provide another electronic device, including a processor, an input device, an output device, and a memory, wherein the processor, input device, output device, and memory are interconnected, wherein the memory is used to store a computer program, the computer program including program instructions, and the processor is configured to invoke the program instructions to execute the steps of the method described in the fourth aspect above.

[0032] The configuration-based cache switching solution provided in this invention reduces the limitations of use cases and lowers the development cost of cache switching for Spring annotation-based caching operations. For code-based caching operations, this invention can meet their cache switching requirements and reduce the time cost of cache switching development. Attached Figure Description

[0033] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the accompanying drawings used in the description of the specific embodiments or the prior art will be briefly introduced below.

[0034] Figure 1 This is a flowchart of the configuration-based cache switching method provided in the first embodiment of the present invention;

[0035] Figure 2 This is a flowchart of the configuration-based cache switching method provided in the second embodiment of the present invention;

[0036] Figure 3 This is a structural diagram of an electronic device provided in an embodiment of the present invention. Detailed Implementation

[0037] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0038] It should be understood that, when used in this specification and the appended claims, the terms "comprising" and "including" indicate the presence of the described features, integrals, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or collections thereof.

[0039] The inventive concept of this invention is to provide a cache switching scheme based on Spring's caching mechanism. Since Spring provides a top-level caching interface, the cache switching scheme in this embodiment can be embedded into the Spring caching mechanism simply by implementing its top-level operations. This allows the use of both existing Spring caching technologies and custom caching, such as database caching. Switching between the existing cache and the custom cache can be achieved simply by changing the values ​​of configuration parameters.

[0040] The original Spring caching support technology allows for the "flexible" use of different caching strategies by specifying the cacheNames and cacheManager parameters of the @Cacheable annotation.

[0041] The basic process of the cache switching scheme in this embodiment is as follows: first, a custom cacheManager is defined, and then a specific cache manager is selected based on the configuration of the configuration file to operate the corresponding cache, thereby realizing the free switching between different caches.

[0042] Please refer to Figure 1 The configuration-based cache switching method provided in the first embodiment of this invention is applicable to Spring annotation-based cache operations. As shown in the figure, the method includes:

[0043] S101, custom configuration file.

[0044] The configuration content of the configuration file (such as application.yml) determines which caching scheme the application uses. If no configuration is specified, the application's default Redis will be used as the caching scheme. The configuration file is configured as follows:

[0045] #Cache type definition: redis: Uses Redis for caching; caffeine: Uses Caffeine for in-memory caching; #db: Uses a database for caching. Redis is used by default.

[0046] usecache:redis

[0047] S102, Select the target cache manager according to the configuration file, thereby determining the cache operation technology components.

[0048] Specifically, as configured in step S101, the selectable cache managers include the Redis cache manager, memory cache manager, and DB cache manager. The selection based on the configuration file differs for each cache manager. These will be described in detail below:

[0049] I. Regarding Redis Cache Manager and Memory Cache Manager

[0050] The specific steps for selecting a target cache manager are: inherit the AbstractCacheManager class and override the getCache method in the AbstractCacheManager class;

[0051] Select the target cache manager based on the configuration file and the overridden getCache method.

[0052] It should be noted that the process of selecting the target cache manager based on the configuration file can be understood as customizing the cache manager, specifically as follows:

[0053] A custom cache manager, CustomCacheManager, is the core implementation for cache switching in the entire solution. This cache manager inherits from Spring's AbstractCacheManager class, and in this embodiment, the getCache method is overridden. This method selects the appropriate cache based on the query key. The process of overriding this selection method is as follows: first, a specific CacheManager is selected according to the configuration in step S101, and then the specific cache operation is handled by the specific CacheManager.

[0054] II. Regarding DB Cache Manager

[0055] The specific steps for selecting a target cache manager are as follows: inherit the AbstractCacheManager class and override the getCache method in the AbstractCacheManager class; use a timed database cache implementation class; and select a target cache manager based on the configuration file, the overridden getCache method, and the cache implementation class.

[0056] It's important to note that database caching is typically used in specific scenarios, such as small applications or those with low concurrency. To reduce service deployment complexity, decrease the number of deployed components, and minimize hardware resource usage, while ensuring high availability through dual-machine deployment, database caching is necessary to store common service data, such as user login information and verification codes. Additionally, database caching can be used to cache interface results for some special service interfaces with complex and time-consuming processing flows, where the calculation results do not change frequently.

[0057] The DB cache implementation consists of two steps:

[0058] A: Define a DB cache CacheManager: DbCacheManager. Its customization process is the same as that of a custom cache manager CustomCacheManager, that is, inherit from Spring's AbstractCacheManager class and override the getCache method.

[0059] B: Define the DB cache implementation class: DbCache. This class implements DB caching operations, including cache query, update, and delete operations.

[0060] S103, the caching operation technology component is automatically loaded into the project.

[0061] Understandably, steps S101 and S102 above enable a scheme for freely switching caches through configuration, i.e., determining the cache operation technology component. However, as a unified switching operation technology component, some automated configuration is still needed so that applications using this component can directly use it upon introduction without requiring additional configuration or code modifications.

[0062] Therefore, the following steps are required:

[0063] Define an auto-configuration class CustomCacheAutoConfiguration and configure it in the spring.factories file. With the help of Spring's auto-registration mechanism, the aforementioned cache manager will be automatically registered, thus automatically loading the cache operation component into the project.

[0064] As can be seen from the above description, the configuration-based cache switching solution provided in this embodiment of the invention reduces the limitations of use cases and the development cost of cache switching for Spring annotation-based cache operations.

[0065] Please refer to Figure 2 The configuration-based cache switching method provided in the second embodiment of the present invention is applicable to code caching operations. As shown in the figure, the method includes:

[0066] S201, Custom Configuration File.

[0067] The configuration file includes cache definition types.

[0068] S202, Select the target cache manager according to the configuration file, thereby determining the cache operation technology components.

[0069] The target cache manager includes a Redis cache manager and a memory cache manager. The selection of the target cache manager based on the configuration file specifically includes:

[0070] Inherit from the AbstractCacheManager class and override the getCache method in the AbstractCacheManager class;

[0071] Select the target cache manager based on the configuration file and the overridden getCache method.

[0072] It should be noted that for a more detailed description of steps S201 and S202, please refer to steps S101 and S102, which will not be repeated here.

[0073] S203, the cache operation entry point is encapsulated so that the code can call the cache operation technology component through the cache operation entry point.

[0074] Specifically, this step involves encapsulating a unified caching operation class, CacheUtil, to shield the different operations required by various caching technologies. After encapsulation, application code only needs to call methods in CacheUtil to perform caching operations, such as querying, updating, and deleting the cache. It doesn't need to know which caching technology the application uses; the unified method calls improve code readability and facilitate application code maintenance.

[0075] It should be noted that the specific caching operations are determined by the CacheUtil class based on the configuration in step S201, and the technical implementation logic is consistent with that in step S202.

[0076] Furthermore, to meet the needs of specific scenarios that require specifying the caching method, the CacheUtil class provides methods to specify the use of caching, which offers high flexibility.

[0077] As can be seen from the above description, the configuration-based cache switching scheme provided by the embodiments of the present invention can meet the cache switching requirements of code caching operation methods and reduce the time cost of cache switching development.

[0078] In summary, the cache switching scheme provided by this invention satisfies both caching operations for Spring annotation methods and caching operations for encoded methods. Furthermore, if the application needs to extend to other existing unsupported caching technologies using this scheme, it is relatively easy; only CustomCacheManager and CacheUtil need to be extended, significantly reducing development time and costs.

[0079] Accordingly, embodiments of the present invention also provide an electronic device. For example... Figure 3 As shown, the electronic device may include one or more processors 101, one or more input devices 102, one or more output devices 103, and a memory 104. The processors 101, input devices 102, output devices 103, and memory 104 are interconnected via a bus 105. The memory 104 stores a computer program, which includes program instructions. The processor 101 is configured to invoke the program instructions to execute, for example... Figure 1 Or such as Figure 2 The method steps described in the illustrated method embodiment are as follows.

[0080] It should be understood that, in this embodiment of the invention, the processor 101 may be a central processing unit (CPU), or it may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.

[0081] Input device 102 may include a keyboard, etc., and output device 103 may include a display (LCD, etc.), a speaker, etc.

[0082] The memory 104 may include read-only memory and random access memory, and provides instructions and data to the processor 101. A portion of the memory 104 may also include non-volatile random access memory. For example, the memory 104 may also store device type information.

[0083] In specific implementations, the processor 101, input device 102, and output device 103 described in the embodiments of the present invention can execute the implementation methods described in the embodiments of the configuration-based cache switching method provided in the embodiments of the present invention, which will not be repeated here.

[0084] It should be noted that for a more detailed description of the workflow of the electronic device, please refer to the aforementioned method embodiment section, which will not be repeated here.

[0085] In addition, corresponding to the aforementioned method embodiments and electronic devices, this embodiment of the invention provides a computer-readable storage medium storing a computer program, the computer program including program instructions, which, when executed by a processor, implement a configuration-based cache switching method.

[0086] The computer-readable storage medium can be an internal storage unit of the electronic device described in any of the foregoing embodiments, such as a system hard drive or memory. The computer-readable storage medium can also be an external storage device of the system, such as a plug-in hard drive, Smart Media Card (SMC), Secure Digital (SD) card, or Flash Card. Furthermore, the computer-readable storage medium can include both internal storage units and external storage devices. The computer-readable storage medium is used to store the computer program and other programs and data required by the system. The computer-readable storage medium can also be used to temporarily store data that has been output or will be output.

[0087] The aforementioned storage media include: USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks or optical disks, and other media that can store program code.

[0088] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in the present invention, and these modifications or substitutions should all be covered within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.

Claims

1. A configuration-based cache switching method, characterized in that, This method applies to Spring annotation-based caching operations, including: A custom configuration file, which includes cache definition types; Select the target cache manager based on the configuration file to determine the cache operation technology components; The caching operation technology component will be automatically loaded into the project; The target cache manager includes a Redis cache manager and a memory cache manager. The selection of the target cache manager based on the configuration file specifically includes: Inherit from Spring's AbstractCacheManager class and override the getCache method in the AbstractCacheManager class. The process of overriding the getCache method in the AbstractCacheManager class is as follows: select a specific CacheManager according to the custom configuration file, and hand over the specific caching operation to the specific CacheManager for processing. Select the target cache manager based on the configuration file and the overridden getCache method.

2. The cache switching method as described in claim 1, characterized in that, The target cache manager includes a DB cache manager. The selection of the target cache manager based on the configuration file specifically includes: Inherit from Spring's AbstractCacheManager class and override the getCache method in the AbstractCacheManager class. The process of overriding the getCache method in the AbstractCacheManager class is as follows: select a specific CacheManager according to the custom configuration file, and hand over the specific caching operation to the specific CacheManager for processing. Define the database cache implementation class; Select the target cache manager based on the configuration file, the overridden getCache method, and the cache implementation class.

3. The cache switching method as described in claim 1 or 2, characterized in that, The caching operation technology component is automatically loaded into the project, specifically including: Define an auto-configuration class and configure it in the spring.factories file; The caching technology component is automatically loaded into the project using Spring's auto-registration mechanism.

4. An electronic device, characterized in that, The electronic device includes a processor, an input device, an output device, and a memory, which are interconnected. The memory is used to store a computer program, which includes program instructions. The processor is configured to invoke the program instructions to execute the steps of the method as described in claim 3.

5. A computer-readable storage medium storing a computer program, the computer program comprising program instructions, characterized in that, When the program instructions are executed by the processor, they implement the steps of the method as described in claim 3.

6. A configuration-based cache switching method, characterized in that, This method is applicable to code caching operations, including: A custom configuration file, which includes cache definition types; Select the target cache manager based on the configuration file to determine the cache operation technology components; The cache operation entry point is encapsulated so that code can call the cache operation technology component through the cache operation entry point; the target cache manager includes a Redis cache manager and a memory cache manager, and the target cache manager is selected according to the configuration file, specifically including: Inherit from Spring's AbstractCacheManager class and override the getCache method in the AbstractCacheManager class. The process of overriding the getCache method in the AbstractCacheManager class is as follows: select a specific CacheManager according to the custom configuration file, and hand over the specific caching operation to the specific CacheManager for processing. Select the target cache manager based on the configuration file and the overridden getCache method.

7. The cache switching method as described in claim 6, characterized in that, The entry point for cache operations is encapsulated, specifically including: The cache operation class is uniformly encapsulated to provide a unified entry point for cache operations.

8. An electronic device, characterized in that, The electronic device includes a processor, an input device, an output device, and a memory, which are interconnected. The memory stores a computer program, which includes program instructions. The processor is configured to invoke the program instructions to execute the steps of the method as described in claim 7.