Method for implementing Redis cache and distributed memory library access switching based on JAVA annotation
The access switching between Redis cache and distributed memory bank is achieved through custom JAVA annotations, which solves the problem of difficulty in managing and switching Redis cache and distributed memory bank in the existing technology, and realizes flexible database switching and simplified code management.
Patent Information
- Application Number
- CN202110772613.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-08
- Publication Date
- 2025-06-24
- Estimated Expiration
- 2041-07-08
AI Technical Summary
It is difficult for the existing technology to effectively manage and switch Redis caches and distributed memory banks, especially in scenarios where relational databases are needed. Spring Cache's key-value format limits the support for distributed memory banks.
By customizing JAVA annotations @DataSourceSwitch and @EnableDataSourceSwitching, access switching between Redis cache and distributed memory library is realized. When the business function is started, the Redis cache or distributed memory bank is accessed according to the startup class annotation, and the execution results in the distributed memory bank are mapped back through custom facets and interfaces.
It realizes flexible switching between Redis cache and distributed memory library, simplifies code management between different types of databases, reduces the need for secondary code development, and enriches the way products use cache.
Smart Images

Figure CN113608780B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of databases, and in particular to a method for implementing the access switching between Redis cache and distributed memory library based on JAVA annotations. Background Art
[0002] The integrated order center is a transaction-intensive system, with a relatively high order volume and requirements for processing efficiency. To improve performance, the system incorporates caching technologies to enhance processing efficiency, such as common caching technologies like EHCache, Redis, Conherence, Memcached, etc.
[0003] Regarding the use of caching, Spring Boot provides a powerful caching management tool, Spring Cache, which can easily manage the addition, deletion, modification, and query of caches through annotations such as @CachePut, @CacheEvict, and @Cacheable.
[0004] Since some users have encountered failures when using Redis, there are doubts about the stability of the Redis cache, and it is relatively difficult to maintain cache data. Therefore, some customers require the use of a distributed memory database as a caching technology. The distributed memory library is a relational database, and its usage is not the same as that of key-value-based caching technologies. Spring Cache currently only supports key-value format caching technologies for scenarios such as EHCache and Redis, and cannot manage relational databases like the distributed memory library. Moreover, the existing cache modules are all core product modules and cannot be independent to ensure the consistency of the core product code. Therefore, Spring Cache cannot meet the requirements. Summary of the Invention
[0005] In view of the above problems, the present invention provides a method for implementing the access switching between Redis cache and distributed memory library based on JAVA annotations. Through custom annotations, when accessing data in the distributed memory library, it is started through the annotation to achieve the switching from Redis cache to the distributed memory library. The execution result in the distributed memory library is mapped back through an interface. The annotation method is simple and flexible, providing a convenient and fast way for other functional modules to access the cache, reducing the workload of code version management between different types of databases, and at the same time reducing the secondary development of code for supporting personalized requirements during project implementation, enriching the way of using the cache in the product.
[0006] To achieve the above object, the present invention provides a method for implementing the access switching between Redis cache and distributed memory library based on JAVA annotations, including:
[0007] Add and customize the annotation @DataSourceSwitch for switching from Redis cache to distributed memory library;
[0008] Add and customize the annotation @EnableDataSourceSwitching for starting the annotation @DataSourceSwitch;
[0009] When the business function starts, judge whether to access the Redis cache or the distributed memory library according to the startup class annotation;
[0010] If the startup class annotation is the annotation @EnableCaching, enable the Redis cache annotation to access the data in the Redis cache;
[0011] If the startup class annotation is the annotation @EnableDataSourceSwitching, enable the distributed memory library annotation @DataSourceSwitch to access the data in the distributed memory library.
[0012] In the above technical solution, preferably, the method for implementing the access switching between the Redis cache and the distributed memory library based on JAVA annotation further includes:
[0013] Customize the aspect DataSourceSwitchInterceptor;
[0014] Then the enabling of the distributed memory library annotation @DataSourceSwitch to access the data in the distributed memory library specifically includes:
[0015] Parse the incoming parameters of the preset pointcut according to the preset rules, and obtain the parsed parameter values from the incoming parameters through Java reflection;
[0016] Execute the sql using the interface of the distributed memory library, and map the sql execution result to the entity passed into the pointcut through fastjson.
[0017] In the above technical solution, preferably, the customizing the annotation @DataSourceSwitch for switching from Redis cache to distributed memory library specifically includes:
[0018] Define the type of the distributed memory library accessed by the annotation @DataSourceSwitch, the parameters for executing the sql, the sql to be executed, and the return type of the sql execution result.
[0019] In the above technical solution, preferably, the distributed memory library is configured with corresponding memory library addresses, user names, passwords, connection pool types, and initialized minimum and maximum connection numbers.
[0020] In the above technical solution, preferably, the aspect and each annotation are customized based on spring aop.
[0021] Compared with the prior art, the beneficial effects of the present invention are as follows: Through the customized annotation, when accessing the data in the distributed memory library is required, it is started through the annotation, realizing the switch from the Redis cache to the distributed memory library. The execution result in the distributed memory library is mapped and transmitted back through the interface. The annotation method is simple and flexible, providing a convenient and fast way for other functional modules to access the cache, reducing the workload of code version management between different types of databases, and at the same time reducing the secondary development of code for supporting personalized requirements in project implementation, enriching the way of using the cache in the product. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] Figure 1 It is a schematic flowchart of a method for realizing the access switch between Redis cache and distributed memory library based on JAVA annotation disclosed in an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0023] To make the objectives, 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 with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are some, but not all, of the embodiments of the present invention. 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.
[0024] The following further describes the present invention in detail with reference to the drawings:
[0025] As Figure 1 shown, a method for realizing the access switch between Redis cache and distributed memory library based on JAVA annotation provided by the present invention includes:
[0026] Adding and customizing the annotation @DataSourceSwitch for switching from the Redis cache to the distributed memory library;
[0027] Adding and customizing the annotation @EnableDataSourceSwitching for starting the annotation @DataSourceSwitch;
[0028] When the business function is started, determine whether to access the Redis cache or the distributed memory library according to the annotation of the startup class;
[0029] If the startup class annotation is the annotation @EnableCaching, the Redis cache annotation is enabled to access the data in the Redis cache;
[0030] If the startup class annotation is the annotation @EnableDataSourceSwitching, the distributed in-memory database annotation @DataSourceSwitch is enabled to access the data in the distributed in-memory database.
[0031] In this embodiment, preferably, by virtue of the aspect function of Spring, custom annotations and aspect classes are defined based on spring aop. The annotation is added to the existing data access method and started through the annotation when accessing the data in the distributed in-memory database, so as to realize the switching from the Redis cache to the distributed in-memory database. The execution result in the distributed in-memory database is mapped back through the interface to realize the query function of the distributed in-memory database. The annotation method is simple and flexible, providing a convenient and fast way for other functional modules to access the cache, reducing the workload of code version management between different types of databases, and at the same time reducing the secondary development of code for supporting personalized requirements in the project implementation, enriching the way of using the cache in the product.
[0032] Specifically, the specific process of the above custom annotation @DataSourceSwitch for switching from the Redis cache to the distributed in-memory database includes:
[0033] Define the type of the distributed in-memory database accessed by the annotation @DataSourceSwitch, the parameters for executing the sql, the sql to be executed, and the return type of the sql execution result.
[0034] Among them, sqlType is the type of the distributed in-memory database accessed, with the value of query for query statements; sync for modification statements such as insert, update, and delete;
[0035] Key is the parameter value for executing the sql in the distributed in-memory database. For example, if there are two parameters for executing the sql, the configuration example key = "#param.mainProdId+'^'+#param.mainActionId";
[0036] returnType is the type of the execution result returned. For example: returnType = TdWoCmddisrule.class;
[0037] The sqlStr is the sql for distributed memory execution. For example, sqlStr = "select * from td_wo_cmddisrule where main_prod_id =? and main_action_id =? and state = '1'", and the parameter values are replaced with? marks.
[0038] Among them, the custom annotation @EnableDataSourceSwitching is used to start the above annotation @DataSourceSwitch. After adding @EnableDataSourceSwitching to the startup class, the annotation @DataSourceSwitch will take effect.
[0039] In the above embodiment, preferably, the method for implementing the Redis cache and distributed memory library access switching based on JAVA annotation further includes:
[0040] Customize the aspect DataSourceSwitchInterceptor;
[0041] Then enabling the distributed memory library annotation @DataSourceSwitch to access the data in the distributed memory library specifically includes:
[0042] According to the parameter key value passed in by the preset cut point, parse the mainProdId and mainActionId parameters according to the rule of #param.mainProdId + '^' + #param.mainActionId, obtain the original method input parameter from the cut point, and obtain the corresponding parsed parameter value from the passed-in parameter through Java reflection;
[0043] Then, execute the sql using the interface of the distributed memory library, and map the sql execution result List result set to the entity passed in by the cut point returnType and return.
[0044] In the above embodiment, preferably, add the configuration of the distributed memory library connection string, and the specific configuration includes the corresponding memory library address, username, password, connection pool type, and the initialized minimum and maximum number of connections. The configuration method is as follows:
[0045] dmdb:
[0046] driverClassName:com.sitech.dmdb.Driver
[0047] url:
[0048] jdbc:e-mobile: / / dmdb / promdb&readtime=10000&autcommit=false&conntime=3000
[0049] username:xxxxx
[0050] password:*****
[0051] type:com.zaxxer.hikari.HikariDataSource
[0052] initMethod:init
[0053] minimumIdle:1
[0054] maximumPoolSize:200
[0055] connection-test-query:SELECT 1FROM DUAL
[0056] According to the method for implementing the Redis cache and distributed memory library access switching based on JAVA annotation provided in the above embodiments, in the case where there are different types of databases between different provinces, it is possible to select whether to use the Redis cache or the distributed memory library according to the requirements. Without changing the core code, it simply and quickly realizes the access switching from the key-value type cache of Redis to the relational distributed memory library, reduces the workload of code version management between provinces, and ensures the code consistency of each province.
[0057] The above are only the preferred embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included within the protection scope of the present invention.
Claims
1. A method for implementing the switching between Redis cache and distributed memory library access based on JAVA annotation, characterized in that Including: Adding and customizing the annotation @DataSourceSwitch for switching from Redis cache to distributed memory library; Adding and customizing the annotation @EnableDataSourceSwitching for starting the annotation @DataSourceSwitch; When the business function starts, determining whether to access the Redis cache or the distributed memory library based on the annotation of the startup class; If the annotation of the startup class is the annotation @EnableCaching, enabling the Redis cache annotation to access the data in the Redis cache; Customizing the aspect DataSourceSwitchInterceptor. If the annotation of the startup class is the annotation @EnableDataSourceSwitching, enabling the distributed memory library annotation @DataSourceSwitch to access the data in the distributed memory library, specifically including: Parsing the incoming parameters of the preset pointcut according to the preset rules, and obtaining the parsed parameter values from the incoming parameters through Java reflection; Executing the sql using the interface of the distributed memory library, and mapping the sql execution result to the entity passed into the pointcut through fastjson.
2. The method for implementing the switching between Redis cache and distributed memory library access based on JAVA annotation according to claim 1, characterized in that The custom annotation @DataSourceSwitch for switching from Redis cache to distributed memory library specifically includes: Defining the type of the distributed memory library accessed by the annotation @DataSourceSwitch, the parameters for executing the sql, the sql to be executed, and the return type of the sql execution result.
3. The method for implementing Redis cache and distributed memory library access switching based on JAVA annotation according to claim 1, characterized in that The distributed memory library is configured with corresponding memory library address, username, password, connection pool type, and initialized minimum and maximum connection numbers.
4. The method for implementing the switching between Redis cache and distributed memory library access based on JAVA annotation according to claim 1, characterized in that The aspect and each annotation are customized based on spring aop.