Data caching methods, apparatus, computer equipment, and readable media

By introducing the MybatisRedisPlugin plugin into a Spring Boot project, distributed cache management is automated, solving the data inconsistency problem caused by manual maintenance in distributed cache systems, improving system reliability and processing efficiency, and reducing developer dependence.

CN115237950BActive Publication Date: 2025-10-28BOE TECHNOLOGY GROUP CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202110440867.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-04-23
Publication Date
2025-10-28
Estimated Expiration
2041-04-23

AI Technical Summary

Technical Problem

In distributed caching systems, existing technologies rely on manual cache maintenance, leading to issues such as data inconsistency and abnormal data returns, which are particularly noticeable during project handover or when developers have limited technical skills.

Method used

By introducing the MybatisRedisPlugin plugin into a Spring Boot project, methods with the SelectByPrimaryKey annotation or the @RedisAdd annotation are automatically added to the distributed cache, and methods with the DeleteByPrimaryKey annotation or the @RedisDelete annotation are automatically deleted from the distributed cache. The local cache stores the mapping between the fully qualified name of the called method and the RedisKey variable name and prefix, reducing duplicate parsing and achieving automated cache management.

Benefits of technology

It improves data processing efficiency and system reliability, reduces reliance on developers, provides a flexible cache maintenance mechanism, and requires no modification to existing code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115237950B_ABST
    Figure CN115237950B_ABST
Patent Text Reader

Abstract

This invention provides a data caching method, belonging to the field of database technology, which can solve the problem of poor reliability caused by existing manual cache maintenance. The data caching method of this invention includes: receiving a data operation instruction for a database, obtaining the fully qualified name of the method called by the data operation instruction; querying a RedisKey variable name and its prefix in a local cache based on the fully qualified name; in response to finding the RedisKey variable name and its prefix in the local cache, determining the variable value corresponding to the RedisKey variable name, and obtaining the RedisKey variable based on the prefix and variable value; and performing the operation corresponding to the data operation instruction on the data corresponding to the RedisKey variable in a distributed cache. This invention can be used in the SSM framework, enabling automatic maintenance of data in the distributed cache, improving system reliability and data processing efficiency, and reducing dependence on program developers. This invention also provides a data caching device, a computer device, and a readable medium.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of database technology, and specifically relates to a data caching method, apparatus, computer device, and readable medium. Background Technology

[0002] With the maturity of technologies such as Spring Boot, Mybatis, and Redis (Remote Dictionary Server), more and more internet projects are migrating from a Spring + Struct2 + Hibernate architecture to a Spring + SpringMVC + Mybatis architecture, or directly using the SSM (Spring + SpringMVC + MyBatis) framework suite as their project architecture. On the other hand, in big data, high-concurrency, and distributed environments, traditional local caching technologies are gradually being replaced by distributed caching. Distributed caching solves the problem of inconsistencies between caches on different servers, improves overall cache utilization efficiency, and saves total cache space. This places higher demands on application developers.

[0003] Currently, when performing database queries, developers need to manually cache the retrieved data in a distributed cache so that subsequent queries can retrieve the data directly from the distributed cache. When the database changes, developers need to manually delete the corresponding data from the distributed cache based on the stored key. In projects with hundreds of tables, this caching maintenance often results in situations where data has been added to the distributed cache but forgotten to be deleted. Alternatively, due to varying skill levels among developers, code vulnerabilities can lead to distributed cache queries failing and returning abnormal data. This manual cache maintenance method is particularly problematic when the project is handed over to the next maintenance team or when the developers have limited technical skills. Summary of the Invention

[0004] The present invention aims to solve at least one of the technical problems existing in the prior art, and to provide a data caching method, apparatus, computer device and readable medium with high reliability, high efficiency and high flexibility.

[0005] In a first aspect, embodiments of the present invention provide a data caching method, the method comprising:

[0006] Receive data operation instructions for the database and obtain the fully qualified name of the method called by the data operation instructions;

[0007] Based on the fully qualified name, retrieve the RedisKey variable name and the prefix of the RedisKey variable from the local cache;

[0008] In response to finding the RedisKey variable name and the prefix of the fully qualified name in the local cache, the variable value corresponding to the RedisKey variable name is determined, and the RedisKey variable is obtained based on the prefix and the variable value;

[0009] In the distributed cache, perform operations corresponding to the data operation instructions on the data corresponding to the RedisKey variable.

[0010] Secondly, embodiments of the present invention also provide a data caching device, including a receiving module, an acquiring module, a first processing module, and a second processing module. The receiving module is used to receive a data operation instruction for a database and acquire the fully qualified name of the method called by the data operation instruction.

[0011] The acquisition module is used to query the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name;

[0012] The first processing module is configured to, in response to querying the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name in the local cache, determine the variable value corresponding to the RedisKey variable name, and obtain the RedisKey variable based on the prefix and the variable value;

[0013] The second processing module is used to perform operations corresponding to the data operation instructions on the data corresponding to the RedisKey variable in the distributed cache.

[0014] Thirdly, embodiments of the present invention also provide a computer device, comprising:

[0015] One or more processors;

[0016] A storage device on which one or more programs are stored;

[0017] When the one or more programs are executed by the one or more processors, the one or more processors implement the data caching method as described above.

[0018] Fourthly, embodiments of the present invention also provide a computer-readable medium having a computer program stored thereon, wherein the program, when executed, implements the data caching method as described above. Attached Figure Description

[0019] Figure 1 This is a schematic flowchart of the data caching method according to an embodiment of the present invention. Figure 1 ;

[0020] Figure 2This is a schematic flowchart of the data caching method according to an embodiment of the present invention. Figure 2 ;

[0021] Figure 3 This is a schematic diagram illustrating the process of manipulating data in a distributed cache based on data query instructions according to an embodiment of the present invention;

[0022] Figure 4 This is a schematic diagram illustrating the process of manipulating data in a distributed cache based on data modification instructions according to an embodiment of the present invention;

[0023] Figure 5 This is a flowchart illustrating the implementation of a double-delete mechanism in a distributed cache under a data modification instruction according to an embodiment of the present invention.

[0024] Figure 6 This is a flowchart illustrating the process of determining the prefix of the RedisKey variable under a data modification instruction according to an embodiment of the present invention.

[0025] Figure 7 This is a schematic diagram of the structure of the data caching device according to an embodiment of the present invention. Figure 1 ;

[0026] Figure 8 This is a schematic diagram of the structure of the data caching device according to an embodiment of the present invention. Figure 2 . Detailed Implementation

[0027] To enable those skilled in the art to better understand the technical solution of the present invention, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0028] Unless otherwise defined, the technical or scientific terms used in this disclosure shall have the ordinary meaning understood by one of ordinary skill in the art to which this disclosure pertains. The terms “first,” “second,” and similar terms used in this disclosure do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Similarly, the terms “an,” “a,” or “the,” and similar terms do not indicate a quantity limitation, but rather indicate the presence of at least one. The terms “including,” “comprising,” or “containing,” and similar terms mean that the element or object preceding the word encompasses the elements or objects listed following the word and their equivalents, without excluding other elements or objects. The terms “connected,” “linked,” or similar terms are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. The terms “upper,” “lower,” “left,” and “right,” etc., are used only to indicate relative positional relationships, and these relative positional relationships may change accordingly when the absolute position of the described objects changes.

[0029] This invention provides a data caching method, which is implemented via a plugin. To implement this invention, the following definitions apply:

[0030] 1. Add the Maven (project build management) dependency to the Pom.xml file of your Spring Boot project. The specific code is as follows:

[0031] <dependency>

[0032] <groupid> com.boe.middleware< / groupid>

[0033] <artifactid> MybatisRedisPlugin< / artifactid>

[0034] <version> 0.0.1-SNAPSHOT< / version>

[0035] < / dependency>

[0036] 2. All SelectByPrimaryKey calls or calls with the @RedisAdd annotation in a Spring Boot project will be automatically added to the distributed cache (Redis cache).

[0037] The RedisKey variable consists of two parts: the RedisKey prefix and the RedisKey variable value (which defaults to the database ID), separated by a colon ":", for example, User: 1.

[0038] 3. In a Spring Boot project, all methods that include DeleteByPrimaryKey or UpdateByPrimaryKey, or methods annotated with @RedisDelete, will automatically delete the distributed cache.

[0039] 4. If you want a certain default calling method to not operate on the distributed cache, you can disable this feature (plugin).

[0040] The SelectByPrimaryKey method can be called with @RedisAdd(ignore=true).

[0041] The `UpdateByPrimaryKey` and `DeleteByPrimaryKey` methods can be called with the `@RedisDelete(ignore=true)` annotation.

[0042] 5. If you wish to disable distributed caching globally, there are two methods:

[0043] Method 1: Add the @EnableMybatisRedis(false) annotation to the class that can become a bean to disable the automatic addition and deletion of distributed cache.

[0044] Method 2: Configure mybatis.redis.enable=false in the configuration file.

[0045] If either of these two settings is set to false, the plugin will not work.

[0046] 6. You can define the prefix of the distributed cache by setting the `redisKeyPrefix` attribute in the annotation. You can define the index corresponding to the Rediskey variable by setting the `redisKeyIndex` attribute. You can also specify the function to add the distributed cache by setting the `redisAddMethod` value in `RedisDelete`, so that the RedisKey can be directly parsed. The specific annotation definitions are as follows:

[0047] @RedisAdd(redisKeyPrefix="redis prefix",redisKeyIndex="variable name corresponding to the redis key number, defaults to id")

[0048] @RedisDelete(redisKeyPrefix="redis prefix",redisKeyIndex="variable name corresponding to the redis key number, defaults to id",

[0049] redisAddMethod = "the name of the Redis function to add, defaults to selectByPrimaryKey")

[0050] The embodiments of the present invention are configured as follows:

[0051] 1. Utilize the Spring SPI mechanism by adding a `spring.factories` file to the `Resource / META-INF` directory of the plugin project, specifying the auto-configuration classes to be loaded by the plugin. Here, `MybatisRedisAutoConfiguration` is the auto-configuration class, and `RedisCtrl` is a wrapper for the Redission client.

[0052] 2. Define the MybatisRedisAutoConfiguration class, which is the flag class for automatic configuration.

[0053] Add the @Conditional(MybatisRedisCondition.class) annotation to the class. By reading the configuration file and the @EnableMybatisRedis configuration, it determines whether to load this plugin.

[0054] Add `@Import({MybatisRedisPluginConfigManager.class, MybatisRedisAddInterceptor.class, MybatisRedisDelInterceptor.class})` to the class. Then, import these three classes into the Spring container.

[0055] 3. Define the MybatisRedisAddInterceptor class, which is the core class for adding distributed caching.

[0056] This class implements the Interceptor interface. It also includes the @Intercepts and @Signature annotations, specifying that the interceptor's intercept method will only be executed when a user calls a database query method.

[0057] 4. Define the MybatisRedisDelInterceptor class, which is the core class for deleting distributed cache.

[0058] This invention relates to two caches: a local cache and a distributed cache (Redis cache). Both the local and distributed caches store data in key-value format. The local cache records the correspondence between the fully qualified name of the called method, the RedisKey variable name, and the prefix of the RedisKey variable. The key is the fully qualified name of the currently executing called method, and the value is the prefix and the name of the RedisKey variable. The distributed cache records the correspondence between the value of the RedisKey variable and the data. The key is the value of the RedisKey variable, and the value is the data (i.e., the result of the database operation).

[0059] The data caching method provided in this embodiment of the invention, such as... Figure 1 As shown, the method includes the following steps:

[0060] Step 11: Receive data operation instructions for the database and obtain the fully qualified name of the method called by the data operation instructions.

[0061] In some embodiments, data manipulation instructions for a database may include data query instructions and data modification instructions. Data modification instructions may include data update instructions and data deletion instructions, and different data manipulation instructions correspond to different invocation methods.

[0062] In this step, the calling methods corresponding to the data manipulation instructions are determined, and the fully qualified names (fullName) of these calling methods are obtained from the database input parameters.

[0063] Step 12: Query the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name.

[0064] In this embodiment of the disclosure, the variable name and prefix of the RedisKey variable are stored in a local cache, which can eliminate the need for re-parse of the called method and improve processing efficiency.

[0065] Step 13: In response to finding the fully qualified RedisKey variable name and the prefix of the RedisKey variable in the local cache, determine the variable value corresponding to the RedisKey variable name, and obtain the RedisKey variable based on the prefix and the variable value.

[0066] If the fully qualified RedisKey variable name and prefix are found in the local cache, it means that the same calling method was called in a previous database operation. In this case, there is no need to parse the calling method again, but the corresponding RedisKey variable name and prefix can be directly retrieved.

[0067] In some embodiments, determining the variable value corresponding to the RedisKey variable name includes: using a Java emission mechanism to determine the variable value corresponding to the RedisKey variable name in the database input parameters.

[0068] Step 14: Perform the operation corresponding to the data operation instruction on the data corresponding to the RedisKey variable in the distributed cache.

[0069] In this step, if the data operation instruction is a data query operation, the query operation is performed on the data corresponding to the RedisKey variable in the distributed cache; if the data operation instruction is a data modification operation, the deletion operation is performed on the data corresponding to the RedisKey variable in the distributed cache.

[0070] The data caching method provided in this invention utilizes a local cache to store the RedisKey variable name and prefix corresponding to the called method. During database data processing, the re-parsing of the called method can be omitted, improving processing efficiency. During database data processing, the variable value corresponding to the RedisKey variable name is determined to obtain the RedisKey variable, and the data retrieved from the database is stored based on the RedisKey variable. While processing database data, the data in the distributed cache is automatically maintained, improving system reliability and data processing efficiency, and reducing dependence on program developers. This invention only requires developers to introduce relevant dependencies; no code or configuration modifications are needed, resulting in zero code intrusion. Furthermore, the maintenance function of the distributed cache can be enabled or disabled as needed, or only the query function of a specific called method can be disabled, making it flexible and convenient to use.

[0071] In some embodiments, after obtaining the fully qualified name of the method invoked by the data operation instruction (i.e., step 11) and before querying the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name (i.e., step 12), the following step is further included: Step 11', determining whether the fully qualified name is in the ignore list. Accordingly, querying the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name (i.e., step 12) includes: in response to the fully qualified name not being in the ignore list, querying the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name.

[0072] In some embodiments, after determining whether the fully qualified name is in the ignore list (i.e., step 11'), the data caching method further includes the following step: step 12', in response to the fully qualified name being in the ignore list, performing the operation corresponding to the data operation instruction in the database.

[0073] In other words, after obtaining the fully qualified name of the method invoked by the data operation instruction, the system first checks the ignore list instead of the local cache. If the method invoked in this data operation is in the ignore list, the distributed cache is not processed, and the database operation is performed directly using the executor. If the method invoked in this data operation is not in the ignore list, both the distributed cache and the database are processed. By setting an ignore list, data processing efficiency can be further improved. The methods in the ignore list (i.e., the fully qualified names of the invoked methods) can be added and modified by the user, making it more flexible and convenient.

[0074] In some embodiments, such as Figure 2As shown, after querying the RedisKey variable name and its prefix in the local cache based on the fully qualified name (i.e., step 12), the data caching method may further include the following steps:

[0075] Step 21: In response to the fact that the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name are not found in the local cache, retrieve the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name.

[0076] In this step, the RedisKey variable name and its prefix corresponding to the fully qualified name were not found in the local cache. This indicates that the same calling method was not invoked in previous database operations. Therefore, this call needs to parse the calling method to obtain the corresponding RedisKey variable name and its prefix. The process of obtaining the RedisKey variable name and prefix will be explained in detail later for both data query and data modification commands.

[0077] Step 22: In response to determining that the data corresponding to the RedisKey variable needs to be operated on in the distributed cache, store the RedisKey variable name and the prefix of the RedisKey variable in the local cache.

[0078] In this step, when the data operation command is a data query command, the `@RedisAdd` annotation (i.e., the first annotation) is parsed to obtain the RedisKey prefix, Redis variable name, and `ignore` attribute. If the value of the `ignore` attribute is false, or if the calling method corresponding to the `@RedisAdd` annotation is obtained, or if the calling method corresponding to the `@RedisAdd` annotation is not obtained but a preset calling method (the `SelectByPrimaryKey` method) is defined, then the data corresponding to the RedisKey variable needs to be operated on in the distributed cache.

[0079] When the data operation command is a data modification command, the `@RedisDelete` annotation (i.e., the second annotation) is parsed to obtain the RedisKey prefix, Redis variable name, `ignore` attribute, and the method call attribute (`redisAddMethod` attribute). If the value of the `ignore` attribute is false, or if the method corresponding to the `@RedisDelete` annotation is obtained, or if the method corresponding to the `@RedisDelete` annotation is not obtained but a preset call method (`DeleteByPrimaryKey` method or `UpdateByPrimaryKey` method) is defined, then the data corresponding to the RedisKey variable needs to be operated on in the distributed cache.

[0080] Step 23: Determine the variable value corresponding to the RedisKey variable name, and obtain the RedisKey variable based on the prefix and the variable value.

[0081] In this step, you can determine the variable value corresponding to the RedisKey variable name in the database input parameters.

[0082] Step 24: Perform the operation corresponding to the data operation instruction on the data corresponding to the RedisKey variable in the distributed cache.

[0083] In some embodiments, such as Figure 2 As shown, after retrieving the RedisKey variable name and its prefix corresponding to the fully qualified name from the local cache in response to the lack of such a query (step 21), the method further includes the following steps:

[0084] Step 22': In response to determining that no operation is needed on the data corresponding to the RedisKey variable in the distributed cache, the fully qualified name is added to the ignore list. It should be noted that in this step, the operation corresponding to the data manipulation command also needs to be executed in the database, and the operation result is returned.

[0085] The following are combined with Figure 3 and Figure 4 The document provides a detailed explanation of the processes for manipulating data in a distributed cache based on data query instructions and the processes for manipulating data in a distributed cache based on data modification instructions.

[0086] When the data manipulation instruction is a data query instruction, such as Figure 3 As shown, the step of performing the operation corresponding to the data operation instruction on the data corresponding to the RedisKey variable in the distributed cache (i.e., step 14) includes the following steps:

[0087] Step 31: Query the data corresponding to the RedisKey variable in the distributed cache.

[0088] Step 32: If the data corresponding to the RedisKey variable is not found in the distributed cache, proceed to step 33; if the data corresponding to the RedisKey variable is found in the distributed cache, proceed to step 34.

[0089] Step 33: Query the corresponding data from the database and store the data in the distributed cache.

[0090] In this step, if the data corresponding to the RedisKey variable is not found in the distributed cache, the executor is directly invoked to retrieve the query result from the database, cache the query result in the distributed cache, and return the query result (i.e., the retrieved data). After the corresponding data is retrieved from the database, it is stored in the distributed cache according to the RedisKey variable so that subsequent queries can directly retrieve the data from the distributed cache without having to query the database again, thus improving data processing efficiency.

[0091] Step 34: Return the retrieved data.

[0092] In this step, if the data corresponding to the RedisKey variable is found in the distributed cache, the query result (i.e., the retrieved data) is returned directly, saving the step of querying the database again and improving data processing efficiency.

[0093] When the data manipulation instruction is a data modification instruction, such as Figure 4 As shown, the step of performing the operation corresponding to the data operation instruction on the data corresponding to the RedisKey variable in the distributed cache (i.e., step 14) includes the following steps:

[0094] Step 41: Query the data corresponding to the RedisKey variable in the distributed cache.

[0095] Step 42: If the data corresponding to the RedisKey variable is found in the distributed cache, proceed to step 43; if the data corresponding to the RedisKey variable is not found in the distributed cache, proceed to step 44.

[0096] Step 43: Perform a deletion operation on the data in the distributed cache and execute the corresponding operation of the data modification instruction in the database.

[0097] Data modification commands include data update commands and data deletion commands. In this step, regardless of whether it is a data update command or a data deletion command, if the data corresponding to the RedisKey variable is found in the distributed cache, the data stored in the distributed cache will be deleted. However, in the database, they are treated differently. If it is a data update command, the corresponding data will be updated in the database. If it is a data deletion command, the corresponding data will be deleted in the database.

[0098] Step 44: Perform the operation corresponding to the data modification instruction on the data in the database.

[0099] In this step, if the data corresponding to the RedisKey variable is not found in the distributed cache, the data modification command is executed directly in the database. That is, if it is a data update command, the corresponding data is updated in the database; if it is a data deletion command, the corresponding data is deleted in the database.

[0100] If concurrent queries target the same data between deleting data from the distributed cache and deleting data from the database, a new cache will be created in the distributed cache. This means the deleted data is cached again in the distributed cache, but the data has already been deleted from the database, leading to incorrect data returns. To avoid this problem, in some embodiments, for data modification instructions, such as... Figure 5 As shown, in response to finding the data corresponding to the RedisKey variable in the distributed cache and performing the operation corresponding to the data modification instruction in the database (i.e., step 43), or in response to not finding the data corresponding to the RedisKey variable in the distributed cache and performing the operation corresponding to the data modification instruction in the database (i.e., step 44), the data caching method may further include the following steps:

[0101] Step 45: Query the data corresponding to the RedisKey variable in the distributed cache.

[0102] Step 46: If the data corresponding to the RedisKey variable is found in the distributed cache, proceed to step 47; otherwise, end the process.

[0103] Step 47: Delete the data in the distributed cache.

[0104] After executing the data modification command in the database, the corresponding data is queried again in the distributed cache. If the deleted data still exists, it is deleted again from the distributed cache to implement a double-delete mechanism, ensuring the correctness of the data cached in the distributed cache. Since deleting data in the distributed cache may fail, but deleting data in the database cannot be rolled back, this embodiment of the invention employs a double-delete mechanism. First, a deletion operation is performed on the data in the distributed cache to ensure that the old data has been removed. Then, after deleting the data in the database, a second deletion operation is performed on the data in the distributed cache to ensure consistency between the distributed cache and the database for the same data.

[0105] The following sections provide a detailed explanation of how to retrieve the RedisKey variable name and its prefix when the fully qualified name is not found in the local cache for data query and data modification commands.

[0106] When the data operation instruction is a data query instruction, obtaining the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name (i.e., step 21) includes: obtaining the RedisKey variable name and the prefix of the RedisKey variable according to the first annotation, wherein the calling method with the first annotation can store the returned data in the distributed cache; wherein, if the RedisKey variable name is empty, a preset character (e.g., database ID) is used as the RedisKey variable name; if the prefix of the RedisKey variable is empty, the calling method corresponding to the fully qualified name is determined, and the class name of the return value object of the calling method is determined, and the class name is used as the prefix of the RedisKey variable. It should be noted that if the RedisKey variable name is not empty, then the RedisKey variable name is the RedisKey variable name obtained corresponding to the fully qualified name. If the prefix of the RedisKey variable is not empty, then the prefix of the RedisKey variable is the prefix of the RedisKey obtained corresponding to the fully qualified name.

[0107] When the data operation instruction is a data modification instruction, obtaining the RedisKey variable name and prefix corresponding to the fully qualified name (i.e., step 21) includes: obtaining the RedisKey variable name, the RedisKey variable prefix, and the calling method attribute (i.e., the RedisAddMethod attribute) according to the second annotation. The calling method with the second annotation can delete the returned data in the distributed cache. If the RedisKey variable name is empty, a preset character is used as the RedisKey variable name. If the RedisKey variable prefix is ​​empty, the RedisKey variable prefix is ​​determined according to the calling method attribute, the first annotation, or the preset calling method (i.e., the SelectByPrimaryKey method). It should be noted that if the RedisKey variable name is not empty, then the RedisKey variable name is the obtained RedisKey variable name corresponding to the fully qualified name. If the RedisKey variable prefix is ​​not empty, then the RedisKey variable prefix is ​​the obtained RedisKey variable prefix corresponding to the fully qualified name.

[0108] In other words, compared to data query commands, data modification commands require calling a method attribute (RedisAddMethod attribute) to obtain the prefix of the RedisKey variable. The RedisAddMethod attribute allows you to customize the function (i.e., call the method) for adding distributed cache.

[0109] In some embodiments, such as Figure 6As shown, for data modification instructions, determining the prefix of the RedisKey variable based on the calling method attribute, the first annotation, or the preset calling method includes the following steps:

[0110] Step 51: If the property of the called method is not empty, proceed to step 52; if the property of the called method is empty, proceed to step 53.

[0111] If the value of the RedisAddMethod attribute is not empty, the class name of the return object of the method called corresponding to the RedisAddMethod attribute is used as the prefix of the RedisKey variable. If the value of the RedisAddMethod attribute is empty, the method called corresponding to the @RedisAdd annotation is retrieved.

[0112] Step 52: Determine the class name of the return value object of the calling method corresponding to the calling method attribute, and use the class name as the prefix of the RedisKey variable.

[0113] Step 53: Obtain the calling method corresponding to the first annotation.

[0114] In this step, the method call with the @RedisAdd annotation is obtained through Java reflection.

[0115] Step 54: If the calling method corresponding to the first annotation is obtained, proceed to step 55; if the calling method corresponding to the first annotation is not obtained, proceed to step 56.

[0116] If a method call corresponding to the @RedisAdd annotation is found, and there are multiple such methods, the class name of the return value object of the first method call is used as the prefix for the RedisKey variable. If no method call corresponding to the @RedisAdd annotation is found, the class name of the return value object of the SelectByPrimaryKey method is used as the prefix for the RedisKey variable, so that the prefix of the RedisKey variable is set to the default value.

[0117] Step 55: Determine the class name of the return value object of the first method call in the calling method, and use the class name as the prefix of the RedisKey variable.

[0118] In this step, the value of the `redisKeyPrefix` variable set by the first `@RedisAdd` annotation can be used as the prefix for the `RedisKey` variable. If the `redisKeyPrefix` variable is empty, the class name of the return value of the called method is obtained through Java reflection and used as the prefix for the `RedisKey` variable.

[0119] Step 56: Determine the class name of the return value object of the preset calling method, and use the class name as the prefix of the RedisKey variable.

[0120] It should be noted that if the SelectByPrimaryKey method is not defined, it means that there is no method to automatically add the distributed cache, and the operation corresponding to the data modification command will be executed directly in the database.

[0121] The embodiments of the present invention utilize custom annotations (@RedisAdd and @RedisDelete annotations) to specify methods for adding or deleting caches, or methods that require distributed caching, providing high flexibility.

[0122] The data caching method provided in this invention is based on Mybatis, Spring Boot, and Redis technologies. By leveraging the interceptor extension functionality of Mybatis, an automatic caching Mybatis plugin is implemented. When querying the database, the retrieved data is automatically added to the distributed cache; when the database is modified, the corresponding data in the distributed cache is automatically deleted.

[0123] Traditional Mybatis two-level caching technology deletes all data in the distributed cache when modifying a single piece of data in the database. This affects the efficiency of subsequent data retrieval and has poor practicality. In contrast, this invention allows operations on the distributed cache to be performed on a single piece of data, making it highly practical.

[0124] Based on the same technical concept, embodiments of the present invention also provide a data caching device, such as... Figure 7 As shown, the data caching device includes a receiving module 101, an acquisition module 102, a first processing module 103, and a second processing module 104. The receiving module 101 is used to receive data operation instructions for the database and acquire the fully qualified name of the method called by the data operation instructions.

[0125] The acquisition module 102 is used to query the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name.

[0126] The first processing module 103 is configured to, in response to querying the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name in the local cache, determine the variable value corresponding to the RedisKey variable name, and obtain the RedisKey variable based on the prefix and the variable value.

[0127] The second processing module 104 is used to perform operations corresponding to the data operation instructions on the data corresponding to the RedisKey variable in the distributed cache.

[0128] In some embodiments, such as Figure 8 As shown, the data caching device further includes a judgment module 105, which is used to determine whether the fully qualified name is in the ignore list after the first processing module 103 obtains the fully qualified name of the method called by the data operation instruction and before querying the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name.

[0129] The first processing module 103 is configured to, in response to the fully qualified name not being in the ignore list, query the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name.

[0130] In some embodiments, the first processing module 103 is further configured to, in response to the fully qualified name being in the ignore list, perform the operation corresponding to the data operation instruction in the database.

[0131] In some embodiments, the first processing module 103 is further configured to: after querying the RedisKey variable name and the prefix of the RedisKey variable in the local cache according to the fully qualified name, in response to not finding the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name in the local cache, obtain the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name; in response to determining that it is necessary to operate on the data corresponding to the RedisKey variable in the distributed cache, store the RedisKey variable name and the prefix of the RedisKey variable in the local cache; determine the variable value corresponding to the RedisKey variable name, and obtain the RedisKey variable according to the prefix and the variable value.

[0132] In some embodiments, the first processing module 103 is further configured to, in response to not finding the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name in the local cache, after obtaining the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name, add the fully qualified name to the ignore list in response to determining that it is not necessary to operate on the data corresponding to the RedisKey variable in the distributed cache.

[0133] In some embodiments, the data operation instruction is a data query instruction, and the second processing module 104 is used to query the data corresponding to the RedisKey variable in the distributed cache, and in response to the fact that the data corresponding to the RedisKey variable is not found in the distributed cache, query the corresponding data from the database and store the data in the distributed cache.

[0134] In some embodiments, the data operation instruction is a data query instruction. The first processing module 103 is used to obtain the RedisKey variable name and the prefix of the RedisKey variable according to the first annotation. The calling method with the first annotation can store the returned data in the distributed cache. If the RedisKey variable name is empty, a preset character is used as the RedisKey variable name. If the prefix of the RedisKey variable is empty, the calling method corresponding to the fully qualified name is determined, and the class name of the return value object of the calling method is determined, and the class name is used as the prefix of the RedisKey variable.

[0135] In some embodiments, the data operation instruction is a data modification instruction, and the second processing module 104 is configured to: query the data corresponding to the RedisKey variable in the distributed cache; in response to finding the data corresponding to the RedisKey variable in the distributed cache, perform a deletion operation on the data in the distributed cache and perform the operation corresponding to the data modification instruction in the database; and in response to not finding the data corresponding to the RedisKey variable in the distributed cache, perform the operation corresponding to the data modification instruction on the data in the database.

[0136] In some embodiments, the second processing module 104 is further configured to: after performing the operation corresponding to the data modification instruction on the data in the database in response to finding the data corresponding to the RedisKey variable in the distributed cache; or after performing the operation corresponding to the data modification instruction on the data in the database in response to not finding the data corresponding to the RedisKey variable in the distributed cache; and delete the data in the distributed cache in response to finding the data corresponding to the RedisKey variable in the distributed cache.

[0137] In some embodiments, the data operation instruction is a data modification instruction. The first processing module 103 is used to obtain the RedisKey variable name, the prefix of the RedisKey variable, and the calling method attribute according to the second annotation. The calling method with the second annotation can delete the returned data in the distributed cache. If the RedisKey variable name is empty, a preset character is used as the variable name of the RedisKey variable. If the prefix of the RedisKey variable is empty, the prefix of the RedisKey variable is determined according to the calling method attribute, the first annotation, or the preset calling method.

[0138] In some embodiments, the first processing module 103 is configured to: in response to the invocation method attribute not being empty, determine the class name of the return value object of the invocation method corresponding to the invocation method attribute, and use the class name as the prefix of the RedisKey variable; in response to the invocation method attribute being empty, obtain the invocation method corresponding to the first annotation, determine the class name of the return value object of the first invocation method, and use the class name as the prefix of the RedisKey variable; in response to the invocation method attribute being empty and no invocation method corresponding to the first annotation being obtained, determine the class name of the return value object of the preset invocation method, and use the class name as the prefix of the RedisKey variable.

[0139] This invention also provides a computer device, which includes one or more processors and a storage device; wherein the storage device stores one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the data caching method provided in the foregoing embodiments.

[0140] This disclosure also provides a computer-readable medium having a computer program stored thereon, wherein the computer program, when executed, implements the data caching method provided in the foregoing embodiments.

[0141] It is understood that the above embodiments are merely exemplary implementations used to illustrate the principles of the present invention, and the present invention is not limited thereto. For those skilled in the art, various modifications and improvements can be made without departing from the spirit and essence of the present invention, and these modifications and improvements are also considered to be within the scope of protection of the present invention.

Claims

1. A data caching method, characterized in that, The method includes: Receive data operation instructions for the database and obtain the fully qualified name of the method called by the data operation instructions; Based on the fully qualified name, retrieve the RedisKey variable name and the prefix of the RedisKey variable from the local cache; In response to finding the RedisKey variable name and its prefix corresponding to the fully qualified name in the local cache, the variable value corresponding to the RedisKey variable name is determined, and the RedisKey variable is obtained based on the prefix and the variable value; wherein, in response to not finding the RedisKey variable name and its prefix corresponding to the fully qualified name in the local cache, and the data operation instruction is a data modification instruction and the prefix is ​​empty, the prefix of the RedisKey variable is determined based on the calling method attribute, the first annotation, or the preset calling method; In the distributed cache, perform operations corresponding to the data operation instructions on the data corresponding to the RedisKey variable.

2. The method as described in claim 1, characterized in that, After obtaining the fully qualified name of the method called by the data operation instruction, and before querying the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name, the method further includes: determining whether the fully qualified name is in the ignore list; The step of querying the RedisKey variable name and its prefix in the local cache based on the fully qualified name includes: In response to the fact that the fully qualified name is not in the ignore list, the RedisKey variable name and the prefix of the RedisKey variable are retrieved from the local cache based on the fully qualified name.

3. The method as described in claim 2, characterized in that, After determining whether the fully qualified name is in the ignore list, the method further includes: In response to the fully qualified name being in the ignore list, the operation corresponding to the data operation instruction is performed in the database.

4. The method as described in claim 2, characterized in that, After retrieving the RedisKey variable name and its prefix from the local cache based on the fully qualified name, the method further includes: In response to the fact that the RedisKey variable name and the prefix of the fully qualified name are not found in the local cache, the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name are obtained. In response to determining that it is necessary to operate on the data corresponding to the RedisKey variable in the distributed cache, the RedisKey variable name and the prefix of the RedisKey variable are stored in the local cache; Determine the variable value corresponding to the RedisKey variable name, and obtain the RedisKey variable based on the prefix and the variable value; In the distributed cache, perform operations corresponding to the data operation instructions on the data corresponding to the RedisKey variable.

5. The method as described in claim 4, characterized in that, After obtaining the RedisKey variable name and the prefix corresponding to the fully qualified name in response to the fact that the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name are not found in the local cache, the method further includes: In response to determining that no operation is needed on the data corresponding to the RedisKey variable in the distributed cache, the fully qualified name is added to the ignore list.

6. The method as described in claim 1 or 4, characterized in that, The data operation instruction is a data query instruction. The step of performing the operation corresponding to the data operation instruction on the data corresponding to the RedisKey variable in the distributed cache includes: Query the data corresponding to the RedisKey variable in the distributed cache; In response to the fact that the data corresponding to the RedisKey variable is not found in the distributed cache, the corresponding data is retrieved from the database and stored in the distributed cache.

7. The method as described in claim 4, characterized in that, The data operation instruction is a data query instruction, and obtaining the RedisKey variable name and the prefix of the RedisKey variable corresponding to the fully qualified name includes: The RedisKey variable name and prefix are obtained based on the first annotation. The calling method with the first annotation can store the returned data in the distributed cache. If the RedisKey variable name is empty, a preset character is used as the RedisKey variable name. If the prefix of the RedisKey variable is empty, the calling method corresponding to the fully qualified name is determined, and the class name of the return value object of the calling method is determined, and the class name is used as the prefix of the RedisKey variable.

8. The method as described in claim 1 or 4, characterized in that, The data operation instruction is a data modification instruction. The step of performing the operation corresponding to the data operation instruction on the data corresponding to the RedisKey variable in the distributed cache includes: Query the data corresponding to the RedisKey variable in the distributed cache; In response to finding the data corresponding to the RedisKey variable in the distributed cache, a deletion operation is performed on the data in the distributed cache, and the operation corresponding to the data modification instruction is performed in the database; In response to the fact that the data corresponding to the RedisKey variable is not found in the distributed cache, the operation corresponding to the data modification instruction is performed on the data in the database.

9. The method as described in claim 8, characterized in that, In response to finding data corresponding to the RedisKey variable in the distributed cache, and after executing the operation corresponding to the data modification instruction on the data in the database; or, in response to not finding data corresponding to the RedisKey variable in the distributed cache, and after executing the operation corresponding to the data modification instruction on the data in the database, the method further includes: Query the data corresponding to the RedisKey variable in the distributed cache; In response to finding the data corresponding to the RedisKey variable in the distributed cache, the data is deleted from the distributed cache.

10. The method as described in claim 4, characterized in that, The data operation instruction is a data modification instruction, and obtaining the RedisKey variable name and prefix corresponding to the fully qualified name includes: The RedisKey variable name, the prefix of the RedisKey variable, and the calling method attribute are obtained according to the second annotation. The calling method with the second annotation can delete the returned data in the distributed cache. If the RedisKey variable name is empty, a preset character is used as the variable name of the RedisKey variable.

11. The method as described in claim 1, characterized in that, The step of determining the prefix of the RedisKey variable based on the calling method attribute, the first annotation, or the preset calling method includes: In response to the fact that the called method attribute is not empty, the class name of the return value object of the called method corresponding to the called method attribute is determined, and the class name is used as the prefix of the RedisKey variable; In response to the fact that the called method attribute is empty, the called method corresponding to the first annotation is obtained, the class name of the return value object of the first called method in the called method is determined, and the class name is used as the prefix of the RedisKey variable; In response to the fact that the calling method attribute is empty and the calling method corresponding to the first annotation is not obtained, the class name of the return value object of the preset calling method is determined, and the class name is used as the prefix of the RedisKey variable.

12. A data caching device, characterized in that, It includes a receiving module, an acquiring module, a first processing module, and a second processing module. The receiving module is used to receive data operation instructions for the database and acquire the fully qualified name of the method called by the data operation instructions. The acquisition module is used to query the RedisKey variable name and the prefix of the RedisKey variable in the local cache based on the fully qualified name; The first processing module is configured to, in response to finding the RedisKey variable name and the prefix of the fully qualified name in the local cache, determine the variable value corresponding to the RedisKey variable name, and obtain the RedisKey variable based on the prefix and the variable value; wherein, in response to not finding the RedisKey variable name and the prefix of the fully qualified name in the local cache, and the data operation instruction is a data modification instruction and the prefix is ​​empty, determine the prefix of the RedisKey variable based on the calling method attribute, the first annotation, or the preset calling method; The second processing module is used to perform operations corresponding to the data operation instructions on the data corresponding to the RedisKey variable in the distributed cache.

13. A computer device, comprising: one or more processors; A storage device on which one or more programs are stored; When the one or more programs are executed by the one or more processors, the one or more processors implement the data caching method as described in any one of claims 1-11.

14. A computer-readable medium having a computer program stored thereon, wherein, When the program is executed, it implements the data caching method as described in any one of claims 1-11.

Citation Information

Patent Citations

  • Automatic data caching method and system based on redis

    CN112506971A