A scalable cache access method, system, device and storage medium
By using AOP to access cache components and defining interfaces to extend different cache types, the cache encoding complexity problem is solved, and code simplicity and high concurrency adaptability are achieved.
Patent Information
- Application Number
- CN202210507385.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-10
- Publication Date
- 2025-08-19
- Estimated Expiration
- 2042-05-10
AI Technical Summary
In the microservice architecture, the use of multiple caching methods increases coding complexity, and similar code is required to be written in each place where caching is needed, which is difficult to meet the caching needs of different scenarios.
The cache components are connected to the project through AOP, a dynamic proxy object of the class is generated, and the interface is defined to extend different cache types, and the parameters are set using cache annotations to meet the needs of different scenarios.
Reduce repeated code writing, improve code reusability and simplicity, can be modified in a unified manner, meet the cache needs of different scenarios, and adapt to high concurrency scenarios.
Smart Images

Figure CN114860212B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to an expandable cache access method, system, device and storage medium. Background Art
[0002] Microservices architectures offer the advantage of being able to handle high-concurrency scenarios. Solutions for these scenarios include caching, rate limiting, and downgrades, with caching being a key approach. Cached queries directly read data from memory, eliminating the need for time-consuming SQL operations in the database. This improves query efficiency and enhances system concurrency.
[0003] In a microservices-based architecture, many modules are developed in Java. Java applications offer a wide range of caching options, including the popular Redis, Spring Cache, and Google Guava Cache. Different caches offer distinct characteristics. For example, Redis Cache, a third-party cache, offers cache consistency in clustered environments, persists cached data, and operates in a single thread, eliminating the need for locking. It's memory-based and boasts high throughput. Guava Cache and Spring Cache, on the other hand, are local caches that lack cache consistency in clustered environments and are non-persistent. Data is lost after downtime. However, these caches operate within the application, offering high call efficiency. Since they are both written in Java, they can be customized to meet specific requirements.
[0004] In typical projects, using multiple caches increases coding complexity, leading to a preference for a single cache type. Alternatively, some projects directly use native cache APIs, but this requires writing similar code for each cached area, which can be complex. The challenge is finding a unified cache solution that can meet the needs of diverse scenarios. Summary of the Invention
[0005] One of the objectives of the present invention is to provide an extensible cache access method that can reduce the repeated writing of cache-related codes and meet the usage requirements of different scenarios.
[0006] In order to solve the above technical problems, this application provides the following technical solutions:
[0007] An extensible cache access method includes: integrating a cache component into a project through AOP, generating a dynamic proxy object of a class, and making the cache component effective when cache annotations are enabled; defining an interface within the cache component and extending different cache types through the interface.
[0008] The basic scheme principles and beneficial effects are as follows:
[0009] The idea of aspect-oriented programming can dynamically and uniformly add functions to the program without modifying the source code. In this way, when a certain function is needed in the middle of a project, you only need to modify the configuration file and add a class, without changing the code of the already written class, which increases the reusability of the code. This solution is based on the idea of aspect-oriented programming. It connects the cache component to the project through AOP, and then turns on the cache annotation for the cache component to take effect. Otherwise, the cache component will not take effect. It can support horizontal expansion very well. During the project startup process, a dynamic proxy object of the target class is generated. This AOP-based approach greatly reduces the writing of duplicate code and can be modified in a unified manner, which brings great convenience to developers and makes the code more concise.
[0010] This solution uses cache extensibility to simplify code and greatly facilitate development. Different cache types have different implementation logic and corresponding advantages and disadvantages. Users can understand the advantages and disadvantages of each cache type and set cache type parameters according to their needs to select the appropriate cache to meet the needs of different scenarios.
[0011] Furthermore, the cache annotation includes several attributes, and the parameters of the cache annotation attributes are set. If the parameters of the attributes are not set, the default attribute parameters are used;
[0012] Attributes include: cache key, cache type, cache expiration time, whether to throw exceptions, and whether to synchronize operations.
[0013] Set different parameters for the properties of the cache annotation to meet the caching requirements of different scenarios.
[0014] Furthermore, the cache component also integrates Redis cache type and custom cache type;
[0015] Also includes the query step:
[0016] When receiving a user's query request, if the cached data is hit, the data is returned directly. If the cached data is not hit, the database query operation is performed according to different types of cache:
[0017] If the user selects the Redis cache type, after the user actively queries the database, if the data is successfully queried, the data will be synchronously set to the cache according to the properties of the cache annotation;
[0018] If the user selects a custom cache type, after the data in the cache expires, the required data is asynchronously queried and the data is set to the cache.
[0019] Furthermore, the query step specifically includes:
[0020] Get the user query request and parse it, and find the specified cache by setting the cache type;
[0021] If the corresponding cache type does not exist, or there is no corresponding cache implementation class, print an error log;
[0022] If the corresponding cache type exists, proceed to the next step in the specified cache:
[0023] If there is data in the cache and it has not expired, return the cached data directly;
[0024] If the corresponding data is not found, different processing methods are adopted according to the cache type:
[0025] If the cache type is Redis, the data in the cache is expired and has been removed. The database is queried synchronously and the corresponding real-time data is returned.
[0026] If it is a custom cache type, the data in the cache expires, but the data is still in the cache, the old data is returned, and the database is asynchronously queried, and then the database results of the query replace the expired data in the cache.
[0027] By printing specific error logs, the user is prompted that the selected cache type does not exist. At this time, the cache is in an invalid state, and a log warning will be generated for each request to prompt the user to take appropriate measures.
[0028] Custom cache can effectively prevent penetration. When a user request comes in, if the corresponding cache is set and the custom cache is specified, then each query will get data from the cache. If the cache data is empty, a thread will be started internally to query the database asynchronously. When dealing with high concurrency, there may be a large number of users accessing the same key, and the value corresponding to the key does not exist, which may cause multiple threads to query the database at the same time, causing excessive pressure on the database. Considering this situation, the database will be queried only if there is no corresponding data in the cache, and the query result will be set to the cache. If there is a corresponding cache, it will be returned directly, which can effectively cope with scenarios with high concurrency.
[0029] A second object of the present invention is to provide a scalable cache access system, comprising: a cache component;
[0030] The cache component is connected to the project through AOP. The cache component is used to generate dynamic proxy objects of the class. The cache component takes effect when the cache annotation is turned on.
[0031] The cache component includes at least one interface, which is used to extend different cache types.
[0032] The idea of aspect-oriented programming can dynamically and uniformly add functions to the program without modifying the source code. In this way, when a certain function is needed in the middle of a project, you only need to modify the configuration file and add a class, without changing the code of the already written class, which increases the reusability of the code. This solution is based on the idea of aspect-oriented programming. The cache component is connected to the project through AOP, and then the cache annotation is enabled for the cache component to take effect. Otherwise, the cache component will not take effect. It can support horizontal expansion very well. During the project startup process, a dynamic proxy object of the target class is generated. This AOP-based approach greatly reduces the writing of duplicate code and can be modified in a unified manner, which brings great convenience to developers and makes the code more concise.
[0033] This solution uses cache extensibility to simplify code and greatly facilitate development. Different cache types have different implementation logic and corresponding advantages and disadvantages. Users can understand the advantages and disadvantages of each cache type and set cache type parameters according to their needs to select the appropriate cache to meet the needs of different scenarios.
[0034] Furthermore, the cache annotation includes several attributes, and the parameters of the cache annotation attributes are set. If the parameters of the attributes are not set, the default attribute parameters are used;
[0035] Attributes include: cache key, cache type, cache expiration time, whether to throw exceptions, and whether to synchronize operations.
[0036] Set different parameters for the properties of the cache annotation to meet the caching requirements of different scenarios.
[0037] Furthermore, the cache component also integrates Redis cache type and custom cache type;
[0038] The cache component is used to receive user query requests and directly return data that hits the cache;
[0039] The cache component is used to perform database query operations based on different types of cache when no cached data is hit:
[0040] If the user selects the Redis cache type, after the user actively queries the database, if the data is successfully queried, the data will be synchronously set to the cache according to the properties of the cache annotation;
[0041] If the user selects a custom cache type, after the data in the cache expires, the required data is asynchronously queried and the data is set to the cache.
[0042] Furthermore, the cache component is used to obtain and parse the user query request and find the specified cache through the set cache type;
[0043] If the corresponding cache type does not exist, or there is no corresponding cache implementation class, the cache component is used to print an error log;
[0044] If the corresponding cache type exists, the cache component is used to perform the next operation in the specified cache:
[0045] If there is data in the cache and it has not expired, the cache component is used to directly return the cached data;
[0046] If the corresponding data is not found, the cache component is used to take different processing methods according to the different cache types:
[0047] If the cache type is Redis, the data in the cache expires and has been removed. The cache component is used to synchronously query the database and return the corresponding real-time data.
[0048] If it is a custom cache type, the data in the cache expires, but the data is still in the cache. The cache component is used to return the old data and asynchronously query the database, and then replace the expired data in the cache with the queried database results.
[0049] By printing specific error logs, the user is prompted that the selected cache type does not exist. At this time, the cache is in an invalid state, and a log warning will be generated for each request to prompt the user to take appropriate measures.
[0050] Custom cache can effectively prevent penetration. When a user request comes in, if the corresponding cache is set and the custom cache is specified, then each query will get data from the cache. If the cache data is empty, a thread will be started internally to query the database asynchronously. When dealing with high concurrency, there may be a large number of users accessing the same key, and the value corresponding to the key does not exist, which may cause multiple threads to query the database at the same time, causing excessive pressure on the database. Considering this situation, the database will be queried only if there is no corresponding data in the cache, and the query result will be set to the cache. If there is a corresponding cache, it will be returned directly, which can effectively cope with scenarios with high concurrency.
[0051] A third object of the present invention is to provide a scalable cache access device using the above-mentioned scalable cache access system.
[0052] A fourth object of the present invention is to provide a storage medium storing a computer program, wherein the computer program implements the steps of the above-mentioned scalable cache access method when executed by a processor. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] Figure 1 This is a schematic diagram of the structure of Example 2 based on AOP;
[0054] Figure 2 This is a schematic diagram of the application of the cache component in the project in Example 2;
[0055] Figure 3 This is a schematic diagram of the cache component workflow in Example 2;
[0056] Figure 4 This is a schematic diagram of a general cache workflow of a cache component in Example 2;
[0057] Figure 5 This is a schematic diagram of the anti-penetration cache workflow of the cache component in Example 2. DETAILED DESCRIPTION
[0058] The following is further described in detail through specific implementation methods:
[0059] Example 1
[0060] A scalable cache access method according to this embodiment includes the following steps:
[0061] The cache component is integrated into the project through AOP, generating dynamic proxy objects for the class. This allows the cache component to take effect only when the cache annotation is enabled. If no corresponding cache annotation is configured, the cache component will not generate specific cache objects. First, cache-related class files are loaded into the Java virtual machine through the classLoader. The Java language is object-oriented, and simply loading class files into memory will not allow execution; specific objects must be generated. However, not all class files loaded into memory require objects to be generated. This configuration determines whether cache objects should be generated, which can reduce memory consumption.
[0062] The cache annotation includes several attributes, which set the parameters of the cache annotation attributes. If the attribute parameters are not set, the customized default attribute parameters are used. By setting different parameters for the attributes of the cache annotation, it can meet the caching requirements of different scenarios. In this embodiment, the attributes include:
[0063] Cache key: In the cache of a memory-based database, storage is generally based on key-value pairs (key and value). By performing a hash operation on the key, the corresponding unique value can be quickly located and found.
[0064] Cache type: You can select different types of cache by setting this parameter;
[0065] Cache expiration time: When the expiration time t is set, the cached data will always exist within the time range of t. When the time range is greater than t, the cached data expires.
[0066] Whether to throw an exception: When an error is reported internally in the cache component, if the setting is to throw an exception, a runtime exception will be thrown; if the setting is not to throw an exception, after an internal error is reported, the exception will be captured, no runtime exception will be thrown, and an empty object will be returned.
[0067] Whether to operate synchronously: There are different ways to query database data. Synchronous means waiting for the query results before returning, while asynchronous means returning the results directly without waiting.
[0068] Define an interface within the cache component that allows you to extend different cache types. This extensible cache design simplifies code and greatly facilitates development. Different cache types have different implementation logic and corresponding advantages and disadvantages. Users can understand the advantages and disadvantages of each cache type and then set the cache type parameters according to their needs to select the appropriate cache.
[0069] In this embodiment, the cache component also integrates a Redis cache type and a custom cache type.
[0070] When receiving a user's query request, the data that hits the cache is directly returned. If the data does not hit the cache, the database query operation is performed according to the different cache types. If the user selects the Redis cache type, after the user actively queries the database, if the data is successfully queried, the data is synchronously set to the cache according to the properties of the cache annotation;
[0071] If the user selects a custom cache type, after the data in the cache expires, the required data is asynchronously queried through the thread pool and the data is set to the cache.
[0072] The query cache data specifically includes:
[0073] Obtain and parse the user query request, and find the specified cache through the set cache type; specifically, parse the parameters of the user request and select the corresponding cache by comparing the parsed parameters with the parameters inside the cache component;
[0074] If the corresponding cache type does not exist, or there is no corresponding cache implementation class, an error log is printed to prompt the user that the selected cache type does not exist. At this time, the cache is ineffective.
[0075] If the corresponding cache type exists, proceed to the next step in the specified cache:
[0076] If there is data in the cache and it has not expired, return the cached data directly;
[0077] Specifically, obtain the cache key passed in by the user through the query request;
[0078] Parse the cache key;
[0079] The corresponding key is passed into the corresponding cache, and based on the hash algorithm, a unique value is found and then returned to the user. Setting the cache value is a prerequisite for retrieving data from the cache. If the value is not set, then the data cannot be retrieved from the cache. The way to set the cache value is generally to set the value corresponding to the key after the user queries the data, serialize the value and save it to the cache. This key is very critical, and the corresponding value can be uniquely found through this key. Therefore, when setting the cache key parameter, the key to be cached must be unique in the entire cache.
[0080] If the corresponding value is not found, different processing methods are adopted according to the cache type.
[0081] If it is a Redis cache type, the data in the cache is expired and the expired data has been removed. The database is queried synchronously and the corresponding real-time data is returned.
[0082] If it is a custom cache type, if the data in the cache expires, the data is still in the cache, and the old data is directly returned; in this embodiment, the new data requires the user to query in the next step. In other words, the custom cache type is to obtain data from the cache every time it is queried. If there is no data in the cache, the data will be queried asynchronously and then set to the cache. If there is data in the cache and it has not expired, the cached data will be returned directly. If there is data in the cache, but the data has expired, the expired data will still be returned, and an asynchronous query will be performed on the database. The queried database results will then replace the expired data in the cache. This can effectively prevent penetration.
[0083] Based on the above method, this embodiment also provides a scalable cache access system, including: a cache component;
[0084] The cache component is connected to the project through AOP. The cache component is used to generate dynamic proxy objects of the class. The cache component takes effect when the cache annotation is turned on.
[0085] The cache annotation includes several attributes. Set the parameters of the cache annotation attributes. If the attribute parameters are not set, the default attribute parameters are used.
[0086] Attributes include: cache key, cache type, cache expiration time, whether to throw exceptions, and whether to synchronize operations.
[0087] The cache component includes at least one interface, which is used to extend different cache types.
[0088] In this embodiment, the cache component also integrates Redis cache type and custom cache type;
[0089] The cache component is used to receive user query requests and directly return data that hits the cache;
[0090] The cache component is used to perform database query operations based on different types of cache when no cached data is hit:
[0091] If the user selects the Redis cache type, after the user actively queries the database, if the data is successfully queried, the data will be synchronously set to the cache according to the properties of the cache annotation;
[0092] If the user selects a custom cache type, after the data in the cache expires, the required data is asynchronously queried and the data is set to the cache.
[0093] The cache component is used to obtain and parse user query requests and find the specified cache by setting the cache type;
[0094] If the corresponding cache type does not exist, or there is no corresponding cache implementation class, the cache component is used to print an error log;
[0095] If the corresponding cache type exists, the cache component is used to perform the next operation in the specified cache:
[0096] If there is data in the cache and it has not expired, the cache component is used to directly return the cached data;
[0097] If the corresponding data is not found, the cache component is used to take different processing methods according to the different cache types:
[0098] If it is a Redis cache type, the data in the cache expires and the expired data has been removed. The cache component is used to synchronously query the database and return the corresponding real-time data.
[0099] If it is a custom cache type, the data in the cache expires, but the data is still in the cache. The cache component is used to return the old data and asynchronously query the database, and then replace the expired data in the cache with the queried database results.
[0100] This embodiment also provides an expandable cache access device using the above system.
[0101] If the above-mentioned extensible cache access method is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a storage medium. Based on this understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing related hardware through a computer program. The computer program can be stored in a storage medium, and the computer program can implement the steps of the above-mentioned method embodiment when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form, etc. The readable medium may include: any entity or device that can carry the computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electrical carrier signal, telecommunication signal and software distribution medium, etc.
[0102] Example 2
[0103] like Figure 1 As shown, a project contains multiple independent business logics. Without affecting existing functionality, unified cross-cutting concerns are set for each business to horizontally expand the software's functionality. This is known as AOP. Generally speaking, web project development adheres to the three-tier principle: control layer (Controller) -> business layer (Service) -> data layer (Dao). This structure is vertical, with a specific layer being the horizontal layer. AOP is a method that can be applied to a specific horizontal module. The cache component used in this example is designed based on AOP principles.
[0104] There are many ways to implement AOP in different projects, including:
[0105] The Cglib library is implemented using the ASM open source package to load the class file of the proxy object class, generate a subclass by modifying its bytecode, and insert the slice into certain points and surfaces of the node code.
[0106] Customizing the class loader supplements the bytecode during class loading, inserting aspects into the bytecode to add functionality beyond business logic. The Java Agent mechanism provided by the JVM implements AOP by adding aspects when loading class bytecode. The JVM itself provides a dynamic proxy component that can be used to implement proxy patterns for any object, allowing aspect logic to be inserted during the proxy process. This can be achieved using Java's Proxy.newProxyInstance() and InvocationHandler.
[0107] AspectJ is a professional framework and platform for implementing AOP. Through AspectJ, any bytecode aspect can be implemented. The Spring framework fully supports AspectJ.
[0108] This embodiment is based on the dynamic proxy component provided by the JVM itself, and dynamically weaves the cache component we need into the place where caching is required, thereby enhancing the function.
[0109] like Figure 2 As shown in the figure, it includes: business layer, cache component and data layer. There are multiple cache types to choose from within the cache component.
[0110] The diagram clearly illustrates the working location of the cache component in this embodiment, operating between the business and data layers. As a crucial intermediary component, it caches hotspot data. This memory-based data query avoids frequent database disk I / O operations, providing fast data queries and improving system responsiveness. This provides a crucial safeguard for system security, particularly in highly concurrent systems.
[0111] like Figure 3 The following figure shows the detailed working principle of the cache component in this embodiment: using a custom cache annotation (pointcut), a cache annotation MCache is defined in the project. This cache annotation provides a series of optional properties. When using it, you need to set parameters for different properties. Finally, these parameters are passed into the cache component as parameters for different cache APIs or required parameters within the component.
[0112] For example, you can customize an interceptor by implementing the invoke method of MethodInterceptor, generating a Bean object, and injecting it into the Spring container to be managed by Spring.
[0113] When the user's query request comes in, the method with the cache annotation MCache will be intercepted. In the implemented interceptor method, the incoming parameters are judged to see whether the required point-cut method has the corresponding cache annotation MCache. The method methodInvocation.getMethod().isAnnotationPresent(MCache.class) is used to judge. If the method returns true, it means that the executed interception method has the corresponding cache annotation, which is the method that needs to be intercepted; if there is no corresponding cache annotation, no interception is required.
[0114] In the specific implementation of interception, it is necessary to parse the parameters of the cache annotation attributes, obtain the cache type selected by the user through annotation.CacheType, match the cache type required by the user with the customized cache in the component, and if a match is found, the logic of the corresponding cache implementation class will be entered. If there is no match, an error log will be printed to remind the user that the corresponding cache has no implementation class and the user needs to reselect a suitable cache type. In this way, routing of different caches is achieved.
[0115] After selecting a cache implementation class, users will perform a cache query. They first need to find the cache key, which can be obtained using annotation.getKey. The obtained key is then passed to the cache implementation class, where the cache uses a hashing algorithm to retrieve the corresponding unique value.
[0116] like Figure 4 As shown, in a specific implementation of this embodiment, the method for processing cache data expiration is as follows:
[0117] When the user customizes the cache expiration time, it means that after the corresponding set time t has passed since the cache was set, the cache is considered expired. How will the cache be handled after expiration? Different cache types have different cache expiration handling methods.
[0118] When using the integrated Redis cache type, cached data expires and is removed by the system, requiring a new cache before querying. This approach is relatively common, as cached data only experiences temporary inconsistencies within the set expiration timeframe, meeting the needs of most scenarios.
[0119] For another set of custom cache types, expired data is handled differently. When an expiration time is set, cached data is not deleted after the expiration time t has passed, but remains in the cache. When a user requests data, if the cached data is found to be expired, the expired data is returned to the user; if the cached data is not expired, the cached data is returned. Although this expiration handling method sacrifices the user's access to the latest data to some extent, each query is based on the cached data, significantly reducing system response time and improving system concurrency.
[0120] In a specific implementation of this embodiment, a method for processing cached data is further included. The specific processing method is as follows:
[0121] Setting a cache value is a prerequisite for retrieving data from the cache. This requires parameters such as the cache key, cache expiration time, and synchronization. These parameters are passed to the cache component upon user request. The specific cache content is retrieved from the database.
[0122] Key selection is crucial. In distributed projects, if you have a large number of caches, all stored in a single Redis cache, special care must be taken to ensure that the key is unique and can't be duplicated. This can cause serious issues. A good rule of thumb is to use a combination of the class name, method name, and key parameters from the request, then hash the resulting key. Custom keys are also possible, but to avoid errors, especially in distributed projects, it's best to include the corresponding module name in the key.
[0123] The expiration time of the cache is customized by the user. The user can consider the frequency of data changes based on the type of cached data. If the frequency of changes is high, a shorter expiration time can be set. If the frequency of changes is not very high, a longer expiration time can be appropriately set.
[0124] The data in the cache is typically set after a user has queried the database for data, and the queried data is not empty. The value corresponding to the cache key is then set synchronously. Another custom cache integrated with the cache component also occurs after the cached data is queried. This asynchronous database query is performed through a thread pool, and the query results are set to the cache.
[0125] In a specific implementation of the embodiment, a cache component exception handling method is also included. The specific handling method is as follows:
[0126] The cache component contains a series of logic, and exceptions are inevitable. When a user requests a request and enters the cache component through the business logic, various exceptions may be encountered when querying the cache, setting the cache, or querying the database. Exception handling can be customized, with the option to throw or catch the exception. When an exception occurs, the corresponding log information needs to be printed. Printing different levels of logs for alert processing based on the error type allows for quick problem identification.
[0127] In a specific implementation of the embodiment, a cache scalability design is also included:
[0128] Scalability is a good programming style in all software fields. It greatly facilitates developers during requirement iteration, modification, and update processes. In Java, interface-oriented programming allows for better functionality expansion, making code more concise and greatly facilitating development. The cache component of this embodiment fully leverages the advantages of interface-oriented programming, defining an interface that allows access to different types of caches. This example integrates a Redis cache and a custom cache, and can also easily integrate other caches.
[0129] In a specific implementation of the embodiment, the custom cache features are as follows:
[0130] like Figure 5 As shown, the custom cache can better prevent penetration. When the user's query request comes in, if the corresponding cache is set and the custom cache is specified, then each query will get data from the custom cache. If the custom cache data is empty, then a thread will be started internally to query the database asynchronously. When dealing with high concurrency, there may be a large number of users accessing the same key, and the value corresponding to the key does not exist, which may cause multiple threads to query the database at the same time, causing excessive pressure on the database. Taking this situation into account, the cache component in this embodiment locks the query request to avoid the situation where multiple threads query the database at the same time, and after locking, it will query the data in the cache again. If there is no corresponding data, it will query the database, and then set the query result to the cache. If there is a corresponding cache, it will be returned directly. In summary, the custom cache can well adapt to scenarios with low requirements for data consistency and high concurrency.
[0131] The above are only embodiments of the present invention. The invention is not limited to the fields involved in this implementation case. Common knowledge such as the known specific structures and characteristics in the scheme is not described in detail here. Ordinary technicians in the relevant field are aware of all common technical knowledge in the technical field to which the invention belongs before the application date or priority date, can obtain all existing technologies in the field, and have the ability to apply conventional experimental means before that date. Ordinary technicians in the relevant field can improve and implement this scheme in combination with their own abilities under the inspiration given by this application. Some typical known structures or known methods should not become obstacles for ordinary technicians in the relevant field to implement this application. It should be pointed out that for those skilled in the art, without departing from the structure of the present invention, some deformations and improvements can be made, which should also be regarded as the scope of protection of the present invention. These will not affect the effect of the implementation of the present invention and the practicality of the patent. The scope of protection required by this application shall be based on the content of its claims, and the specific implementation methods and other records in the specification can be used to interpret the content of the claims.
Claims
1. A scalable cache access method, characterized in that: include: Connect the cache component to the project through AOP, generate a dynamic proxy object of the class, and make the cache component effective when the cache annotation is turned on; define an interface inside the cache component and extend different cache types through this interface; The cache component also integrates Redis cache type and custom cache type; Also includes the query step: When receiving a user's query request, if the cached data is hit, the data is returned directly. If the cached data is not hit, the database query operation is performed according to different types of cache: If the user selects the Redis cache type, after the user actively queries the database, if the data is successfully queried, the data will be synchronously set to the cache according to the properties of the cache annotation; If the user selects a custom cache type, after the data in the cache expires, the required data is asynchronously queried and the data is set to the cache.
2. The scalable cache access method according to claim 1, wherein: The cache annotation includes several attributes, which are used to set the parameters of the cache annotation attributes. If the parameters of the attributes are not set, the default attribute parameters are used; Attributes include: cache key, cache type, cache expiration time, whether to throw exceptions, and whether to synchronize operations.
3. The scalable cache access method according to claim 2, wherein: The query step specifically includes: Get the user query request and parse it, and find the specified cache by setting the cache type; If the corresponding cache type does not exist, or there is no corresponding cache implementation class, print an error log; If the corresponding cache type exists, proceed to the next step in the specified cache: If there is data in the cache and it has not expired, return the cached data directly; If the corresponding data is not found, different processing methods are adopted according to the cache type: If the cache type is Redis, the data in the cache is expired and has been removed. The database is queried synchronously and the corresponding real-time data is returned. If it is a custom cache type, the data in the cache expires, but the data is still in the cache, the old data is returned, and the database is asynchronously queried, and then the database results of the query replace the expired data in the cache.
4. A scalable cache access system, characterized in that: include: Cache component; The cache component is connected to the project through AOP. The cache component is used to generate dynamic proxy objects of the class. The cache component takes effect when the cache annotation is turned on. The cache component includes at least one interface, which is used to extend different cache types; The cache component also integrates Redis cache type and custom cache type; The cache component is used to receive user query requests and directly return data that hits the cache; The cache component is used to perform database query operations based on different types of cache when no cached data is hit: If the user selects the Redis cache type, after the user actively queries the database, if the data is successfully queried, the data will be synchronously set to the cache according to the properties of the cache annotation; If the user selects a custom cache type, after the data in the cache expires, the required data is asynchronously queried and the data is set to the cache.
5. The scalable cache access system according to claim 4, wherein: The cache annotation includes several attributes, which are used to set the parameters of the cache annotation attributes. If the parameters of the attributes are not set, the default attribute parameters are used; Attributes include: cache key, cache type, cache expiration time, whether to throw exceptions, and whether to synchronize operations.
6. The scalable cache access system according to claim 5, wherein: The cache component is used to obtain and parse user query requests and find the specified cache through the set cache type; If the corresponding cache type does not exist, or there is no corresponding cache implementation class, the cache component is used to print an error log; If the corresponding cache type exists, the cache component is used to perform the next operation in the specified cache: If there is data in the cache and it has not expired, the cache component is used to directly return the cached data; If the corresponding data is not found, the cache component is used to take different processing methods according to the different cache types: If the cache type is Redis, the data in the cache expires and has been removed. The cache component is used to synchronously query the database and return the corresponding real-time data. If it is a custom cache type, the data in the cache expires, but the data is still in the cache. The cache component is used to return the old data and asynchronously query the database, and then replace the expired data in the cache with the queried database results.
7. A scalable cache access device, characterized in that: Use the scalable cache access system described in any one of claims 4 to 6.
8. A storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the scalable cache access method according to any one of claims 1 to 3 are implemented.
Citation Information
Patent Citations
Data access method and device
CN110968603A