Cache processing method and apparatus, electronic device, and storage medium
By introducing local caching and configuring maintenance information in the application server, the problem of low efficiency in obtaining data from external data sources is solved, achieving efficient data acquisition and resource conservation, and improving system performance and user experience.
Patent Information
- Application Number
- CN202410862896.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-28
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2044-06-28
AI Technical Summary
In existing technologies, application servers obtain data from external data sources inefficiently and consume a lot of resources, resulting in inefficient data acquisition.
Introduce local caching in the application server and configure maintenance information through the cache manager to achieve automatic maintenance of the local cache, including data refresh, invalidation management and data update, to ensure the real-time and validity of the data.
It improved data acquisition efficiency, reduced resource consumption, optimized local cache management, and enhanced system performance and user experience.
Smart Images

Figure CN118869814B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of Internet technology, and in particular to a caching method, apparatus, electronic device, and storage medium. Background Technology
[0002] With the rapid development of internet technology, users are increasingly reliant on obtaining various information through the internet. To meet user needs, various applications have emerged, allowing users to more conveniently access various information, such as videos and audio, through application clients.
[0003] When an application client starts, it needs to request data from the application server. The application server then retrieves the required data from an external data source and returns it to the application client. However, in this approach, the application server needs to obtain data from the external data source via network requests, resulting in low data retrieval efficiency and high resource consumption. Summary of the Invention
[0004] The purpose of this invention is to provide a caching method, apparatus, electronic device, and storage medium to improve data acquisition efficiency, reduce resource consumption, and maintain local cache. The specific technical solution is as follows:
[0005] In a first aspect of this invention, a caching method is provided, applied to an application server, the method comprising:
[0006] Inject the local cache into the cache manager and configure the maintenance information of the local cache;
[0007] Upon receiving a cache data retrieval request initiated by the application client, the target data corresponding to the cache data retrieval request is retrieved from the local cache;
[0008] Upon successful retrieval from the local cache, the target data is returned to the application client.
[0009] If the retrieval fails from the local cache, the target data is retrieved from an external data source, stored in the local cache, and returned to the application client.
[0010] Perform maintenance operations on the local cache based on the maintenance information.
[0011] Optionally, the local cache is injected into the cache manager, and the maintenance information of the local cache is configured, including: loading the inherited class of the data loader using automatic injection, injecting the local cache into the cache manager, and configuring the maintenance information of the local cache through custom annotations.
[0012] Optionally, the local cache comprises a plurality of cache keys, and the maintenance information comprises refresh time of each cache key; and the maintenance operation on the local cache according to the maintenance information comprises: for a cache key reaching the refresh time, obtaining data corresponding to the cache key reaching the refresh time from the external data source, and storing the data corresponding to the cache key reaching the refresh time into the cache key reaching the refresh time.
[0013] Optionally, the local cache comprises a plurality of cache keys, and the maintenance information comprises refresh time and batch refresh condition of each cache key; and the maintenance operation on the local cache according to the maintenance information comprises: after monitoring that the batch refresh condition is met, for a cache key reaching the refresh time, obtaining data corresponding to the cache key reaching the refresh time from the external data source by using a batch refresh queue, and storing the data corresponding to the cache key reaching the refresh time into the cache key reaching the refresh time.
[0014] Optionally, the local cache comprises a plurality of cache keys, and the maintenance information comprises invalidation time of each cache key; and the maintenance operation on the local cache according to the maintenance information comprises: deleting data in a cache key reaching the invalidation time.
[0015] Optionally, the maintenance information comprises a cache interception class, and the cache interception class is configured with an intercepted external data source, an intercepted notification mode and an intercepted cache mode; and the maintenance operation on the local cache according to the maintenance information comprises: listening to data update notification of the intercepted external data source according to the intercepted notification mode; and after listening to the data update notification, obtaining update data from the intercepted external data source according to the intercepted cache mode, and storing the update data into the local cache.
[0016] Optionally, the storing of the target data into the local cache comprises: when a size of the target data exceeds a preset threshold, compressing the target data to obtain compressed target data, and storing the compressed target data into the local cache; and the obtaining of the target data corresponding to the cache data obtaining request from the local cache comprises: when the target data corresponding to the cache data obtaining request is compressed target data, decompressing the compressed target data to obtain the target data.
[0017] Optionally, the method further comprises: generating a performance index of the local cache; and the performance index comprises at least one of the following: local cache hit rate, local cache access time consumption, local cache occupancy rate, loading success rate of the external data source, loading time consumption of the external data source, and thread pool state of the batch refresh queue.
[0018] In a second aspect of the embodiments of the present application, a cache processing device is provided for an application server, the device comprising:
[0019] a configuration module configured to inject a local cache into a cache manager and configure maintenance information of the local cache;
[0020] a first obtaining module configured to, after receiving a cache data obtaining request initiated by an application client, obtain target data corresponding to the cache data obtaining request from the local cache;
[0021] a returning module configured to, when the first obtaining module successfully obtains the target data from the local cache, return the target data to the application client;
[0022] a second obtaining module configured to, when the first obtaining module fails to obtain the target data from the local cache, obtain the target data from an external data source, store the target data into the local cache, and return the target data to the application client;
[0023] a maintenance module configured to perform a maintenance operation on the local cache according to the maintenance information.
[0024] Optionally, the configuration module is specifically configured to load an inherited class of a data loader by using an automatic injection mode, inject the local cache into the cache manager, and configure the maintenance information of the local cache by using a self-defined annotation.
[0025] Optionally, the local cache comprises a plurality of cache keys, and the maintenance information comprises refresh times of the cache keys; the maintenance module comprises a first maintenance unit configured to, for a cache key reaching a refresh time, obtain data corresponding to the cache key reaching the refresh time from the external data source, and store the data corresponding to the cache key reaching the refresh time into the cache key reaching the refresh time.
[0026] Optionally, the local cache comprises a plurality of cache keys, and the maintenance information comprises refresh times and batch refresh conditions of the cache keys; the maintenance module comprises a second maintenance unit configured to, after monitoring that the batch refresh conditions are met, for a cache key reaching a refresh time, obtain data corresponding to the cache key reaching the refresh time from the external data source by using a batch refresh queue, and store the data corresponding to the cache key reaching the refresh time into the cache key reaching the refresh time.
[0027] Optionally, the local cache comprises a plurality of cache keys, and the maintenance information comprises invalidation times of the cache keys; the maintenance module comprises a third maintenance unit configured to delete data in a cache key reaching an invalidation time.
[0028] Optionally, the maintenance information comprises a cache interception class, the cache interception class being configured with an intercepted external data source, an intercepted notification mode and an intercepted cache mode; the maintenance module comprises: a fourth maintenance unit, configured to listen to a data update notification of the intercepted external data source according to the intercepted notification mode; and after listening to the data update notification, acquire update data from the intercepted external data source according to the intercepted cache mode, and store the update data into the local cache.
[0029] Optionally, the second acquisition module is specifically configured to compress the target data to obtain compressed target data when the size of the target data exceeds a preset threshold, and store the compressed target data into the local cache; and the first acquisition module is specifically configured to decompress the compressed target data to obtain the target data when the target data corresponding to the cache data acquisition request is the compressed target data.
[0030] Optionally, the apparatus further comprises a generation module configured to generate a performance index of the local cache; wherein the performance index comprises at least one of a local cache hit rate, a local cache access time consumption, a local cache occupancy rate, a loading success rate of an external data source, a loading time consumption of the external data source, and a thread pool state of a batch refresh queue.
[0031] In still another aspect of the embodiments of the present application, an electronic device is further provided, comprising a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory complete communication with each other through the communication bus; the memory is used for storing a computer program; and the processor is used for executing the program stored in the memory to implement the cache processing method described above.
[0032] In still another aspect of the embodiments of the present application, a computer readable storage medium is further provided, the computer readable storage medium storing instructions, when the instructions are run on a computer, causing the computer to implement the cache processing method described above.
[0033] In still another aspect of the embodiments of the present application, a computer program product containing instructions is further provided, when the instructions are run on a computer, causing the computer to implement the cache processing method described above.
[0034] The cache processing method, device, electronic device and storage medium provided by the embodiments of the present application can inject a local cache into a cache manager, so that when a cache data acquisition request initiated by an application client is received, target data corresponding to the cache data acquisition request can be acquired from the local cache, and when the acquisition from the local cache is successful, the target data is returned to the application client, and when the acquisition from the local cache fails, the target data is acquired from an external data source, the target data is stored in the local cache, and the target data is returned to the application client. Therefore, the application server can acquire target data from the local cache without acquiring the target data from the external data source, so that the data acquisition efficiency can be improved and resource consumption can be reduced. In addition, the maintenance information of the local cache is configured, so that the application server can perform a maintenance operation on the local cache according to the maintenance information, and the optimization of the local cache is realized. BRIEF DESCRIPTION OF DRAWINGS
[0035] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings needed to be used in the embodiments or the prior art description will be briefly introduced.
[0036] Figure 1 A step flow chart of a cache processing method in the embodiments of the present application.
[0037] Figure 2 A schematic diagram of a cache processing process in the embodiments of the present application.
[0038] Figure 3 A step flow chart of another cache processing method in the embodiments of the present application.
[0039] Figure 4 A structural block diagram of a cache processing device in the embodiments of the present application.
[0040] Figure 5 A structural block diagram of an electronic device in the embodiments of the present application. DETAILED DESCRIPTION
[0041] The technical solutions in the embodiments of the present application will be described below with reference to the drawings in the embodiments of the present application.
[0042] In the embodiment of the present application, the related data of an application is accessed through a local cache. The local cache refers to a memory cache area created in an application program for storing frequently used or important data. When the data is needed, it is directly read from the local cache without reading from an external data source (database or disk, etc.), so the reading and writing speed is very fast. Compared with distributed cache or database, the advantage of the local cache is fast reading and writing speed, no network transmission, and less resource consumption. For example, for an online video playing application, a large amount of data is preloaded and stored in the local cache, so when a user opens a playing page, the page can be quickly presented to the user in a very short time. This process not only greatly improves the user experience and enhances the user retention rate, but also reduces the dependence on external data sources, reduces the burden on the application server, improves the system performance, and reduces the situation of slow loading of the playing page due to network anomalies or long response time.
[0043] In the embodiment of the present application, the data maintenance of the local cache is further considered, and the maintenance information of the local cache is configured, so that the maintenance operation of the local cache can be automatically performed according to the maintenance information, and the optimization of the local cache is realized. The following will be specifically introduced.
[0044] The cache processing method in the embodiment of the present application can be applied to an application server.
[0045] Figure 1 The step flow chart of the cache processing method in the embodiment of the present application.
[0046] As shown in Figure 1 , the cache processing method can include the following steps:
[0047] Step 101, injecting the local cache into the cache manager and configuring the maintenance information of the local cache.
[0048] In the embodiment of the present application, any applicable local cache can be selected, such as ehcache, GuavaCache, Caffeine, etc. Exemplarily, Caffeine is a high-performance and highly scalable local cache, so the local cache in the embodiment of the present application can be selected as Caffeine.
[0049] In the embodiment of the present application, the local cache can be initialized when the application server starts or at other applicable time. In the local cache initialization process, the local cache is injected into the cache manager, and the maintenance information of the local cache is configured.
[0050] Exemplarily, an inheritance class of a data loader can be loaded by using an automatic injection mode, a local cache is injected into a cache manager, and maintenance information of the local cache is configured by using a custom annotation. The cache manager is located in an application server and is used to store and manage the local cache.
[0051] Exemplarily, an inheritance class mechanism of a DataLoader (data loader) can be loaded by using a springboot (springboot is a tool for developing an application quickly based on a spring framework) automatic injection mode, a local cache is automatically injected into a CacheManager (cache manager, or cache factory), and the local cache is called by a QYCacheService method (cache service method). In the QYCacheService method, a method (such as a get method, an getAll method, and the like) for obtaining local cache data, a method (such as a loadData method and the like) for downloading data from an external data source, a method (such as a set method and the like) for writing data into the local cache, and the like can be registered.
[0052] When the local cache is injected, the name of the injected local cache and a plurality of cache keys included in the local cache need to be defined. The local cache can include a plurality of cache items, and a cache item refers to a single data item stored in the local cache. The cache items store corresponding data (such as an identifier of a certain movie as a cache item, and the like). Each cache item has a corresponding cache key (key), and the cache key (key) is used to uniquely identify the cache item. The data stored in the cache item is the value (value) of the cache item.
[0053] Exemplarily, the maintenance information of the local cache can include but is not limited to a refresh time of each cache key included in the local cache, an invalidation time of each cache key included in the local cache, a batch refresh condition, a cache interception class, a size of the local cache, and the like. In the cache interception class, an intercepted external data source, an intercepted notification mode, and an intercepted cache mode are configured. Details will be introduced below.
[0054] In step 102, after receiving a cache data obtaining request initiated by an application client, target data corresponding to the cache data obtaining request is obtained from the local cache.
[0055] When cache data needs to be obtained, the application client can initiate a cache data obtaining request to the application server. After receiving the cache data obtaining request, the application server obtains target data corresponding to the cache data obtaining request from the local cache.
[0056] Exemplarily, the cache data acquisition request can contain information such as a request identifier, and the application server can query the name and cache key of the local cache corresponding to the cache data acquisition request from the service configuration information according to the request identifier, and then acquire the target data corresponding to the cache data acquisition request from the local cache injected with the cache manager according to the name and cache key of the local cache corresponding to the cache data acquisition request.
[0057] Exemplarily, the get method or the getAll method in the QYCacheService method can be called, and the name and cache key of the local cache corresponding to the cache data acquisition request are transmitted into the get method or the getAll method as parameters, so as to acquire the target data corresponding to the cache data acquisition request from the local cache.
[0058] Step 103: When the acquisition from the local cache is successful, the target data is returned to the application client.
[0059] When the acquisition of the target data from the local cache is successful, the application server can return the target data to the application client, so that the application client loads the target data.
[0060] Step 104: When the acquisition from the local cache fails, the target data is acquired from an external data source, the target data is stored in the local cache, and the target data is returned to the application client.
[0061] When the acquisition of the target data from the local cache fails, the application server can automatically acquire the target data from an external data source.
[0062] Exemplarily, the loadData method in the QYCacheService method can be called, and the name and cache key of the local cache corresponding to the cache data acquisition request are transmitted into the loadData method as parameters, so as to acquire the target data corresponding to the cache data acquisition request from an external data source.
[0063] After the target data is acquired from the external data source, the application server stores the target data in the local cache.
[0064] Exemplarily, the set method in the QYCacheService method can be called, and the name and cache key of the local cache corresponding to the cache data acquisition request are transmitted into the set method as parameters, so as to store the target data in the local cache.
[0065] After the target data is acquired from the external data source, the application server returns the target data to the application client, so that the application client loads the target data.
[0066] In step 105, a maintenance operation is performed on the local cache according to the maintenance information.
[0067] In an optional embodiment, the refresh time of each cache key included in the local cache can be set in the maintenance information, and the data in the cache key is refreshed according to the refresh time of the cache key.
[0068] For example, the application server can monitor whether each cache key included in the local cache reaches the refresh time in real time. When it is monitored that a certain cache key reaches the refresh time, the data corresponding to the cache key reaching the refresh time is obtained from the external data source, and the data corresponding to the cache key reaching the refresh time is stored in the cache key reaching the refresh time. In this way, each cache key can be monitored and the data in each cache key can be refreshed in time, so as to ensure the real-time performance of the data.
[0069] For example, the loadData method in the QYCacheService method can be called, and the name of the local cache and the cache key reaching the refresh time are transmitted as parameters into the loadData method, so as to obtain the data corresponding to the cache key reaching the refresh time from the external data source. The set method in the QYCacheService method can be called, and the name of the local cache and the cache key reaching the refresh time are transmitted as parameters into the set method, so as to store the data corresponding to the cache key reaching the refresh time into the cache key reaching the refresh time.
[0070] The specific form of the refresh time can be set according to actual needs, and the present embodiment does not limit this.
[0071] In an optional embodiment, the refresh time and the batch refresh condition of each cache key included in the local cache can be set in the maintenance information, and the data in the cache key is refreshed according to the refresh time and the batch refresh condition of the cache key.
[0072] Exemplarily, the application server can monitor whether each cache key included in the local cache reaches the refresh time in real time, and monitor whether the batch refresh condition is met in real time. After monitoring that a certain cache key reaches the refresh time, if the batch refresh condition is not met at present, the cache key reaching the refresh time is recorded. After monitoring that the batch refresh condition is met, for the cache key reaching the refresh time, the batch refresh queue is used to obtain the data corresponding to the cache key reaching the refresh time from the external data source, and the data corresponding to the cache key reaching the refresh time is stored in the cache key reaching the refresh time. In this way, batch refresh can be implemented for multiple cache keys reaching the refresh time, thereby reducing the refresh times and saving system resources.
[0073] The specific form of the batch refresh condition can be set according to actual needs, and the embodiment does not limit this. Exemplarily, the batch refresh condition can include but is not limited to that the total number of cache keys reaching the refresh time reaches a preset number, the time length from the last batch refresh reaches a preset time length, and the like.
[0074] In an optional implementation, the invalidation time of each cache key included in the local cache can be set in the maintenance information, and the data in the cache key is invalidated and maintained through the invalidation time of the cache key.
[0075] Exemplarily, the application server can monitor whether each cache key included in the local cache reaches the invalidation time in real time. After monitoring that a certain cache key reaches the invalidation time, the data in the cache key reaching the invalidation time is deleted. In this way, each cache key can be monitored, and the data in the cache key reaching the invalidation time can be deleted in time, thereby ensuring the validity of the data.
[0076] Exemplarily, after the data in the cache key reaching the invalidation time is deleted, the data corresponding to the cache key reaching the invalidation time can be obtained from the external data source, and the data corresponding to the cache key reaching the invalidation time is stored in the cache key reaching the invalidation time.
[0077] The specific form of the invalidation time can be set according to actual needs, and the embodiment does not limit this.
[0078] In an optional implementation, a cache interception class can be set in the maintenance information, and the intercepted external data source, the intercepted notification mode, and the intercepted cache mode are configured in the cache interception class. Through these maintenance information, the data in the local cache can be automatically updated according to the change of the external data source.
[0079] Exemplarily, the external data source can send a data update notification after updating its own data. The application server can listen to the data update notification of the intercepted external data source according to the intercepted notification mode, and acquire the update data from the intercepted external data source according to the intercepted cache mode, and store the update data into the local cache after listening to the data update notification.
[0080] In the Data Loader mentioned in step 101, the cache interception class can be configured by customizing annotations, and a listening method (such as a Listener method, etc.) can be registered in the cache interception class. The intercepted external data source and the intercepted notification mode can be configured in the listening method, and the intercepted cache mode can be configured in the cache interception class, and the interception operation method can be configured in the intercepted cache mode. Therefore, the application server can call the registered listening method, listen to the data update notification of the intercepted external data source according to the intercepted notification mode configured in the listening method, and call the interception operation method in the configured intercepted cache mode to acquire the update data from the intercepted external data source and store the update data into the local cache after listening to the data update notification.
[0081] By setting the intercepted external data source, the application server can intercept the data update notification sent by the specified external data source.
[0082] The data update notification can include, but is not limited to, the name of the local cache and the cache key, etc. The intercepted notification mode can include, but is not limited to, the notification containing the set name of the local cache and the cache key, etc. Therefore, by setting the intercepted notification mode, the application server can intercept the data update notification containing the set name of the local cache and the cache key.
[0083] By setting the intercepted cache mode, the application server can update the data according to the set cache mode.
[0084] Exemplarily, the intercepted cache mode can be set to acquire the update data corresponding to the data update notification from the intercepted external data source in time and store the update data into the local cache after listening to the data update notification of the intercepted external data source according to the intercepted notification mode.
[0085] Exemplarily, the intercepted cache mode can be set to delete the data corresponding to the data update notification from the local cache after listening to the data update notification of the intercepted external data source according to the intercepted notification mode, so as to acquire the update data corresponding to the data update notification from the intercepted external data source again and store the update data into the local cache when acquiring the update data next time.
[0086] Exemplarily, the intercepted caching mode can be set to record the data update notification after listening to the data update notification of the intercepted external data source according to the intercepted notification mode, and when the user obtains data next time, if the obtained data is the update data corresponding to the data update notification, all or part of the data in the update data corresponding to the data update notification is obtained from the intercepted external data source according to requirements, and the all or part of the data is stored in the local cache.
[0087] Of course, other caching modes can also be set according to requirements, and the embodiment of the application does not limit this.
[0088] Figure 2 A schematic diagram of a caching process in an embodiment of the application is shown in FIG. 3. Figure 2 As shown in FIG. 3, when the application client queries the cached data, the local cache is queried first, the query result is returned to the application client when the local cache hits, the data is obtained from the external data source when the local cache misses, and the query result is returned to the application client, the data is obtained from the external data source to update the data in the local cache when the refresh time is reached or the invalidation time is reached, through the asynchronous refresh queue (i.e., the batch refresh queue described above).
[0089] Figure 3 A step flowchart of another caching processing method in an embodiment of the application is shown in FIG. 4.
[0090] As shown in FIG. 4, the caching processing method can include the following steps: Figure 3
[0091] Step 301, injecting the local cache into the cache manager, and configuring the maintenance information of the local cache.
[0092] Step 302, after receiving the cached data obtaining request initiated by the application client, obtaining the target data corresponding to the cached data obtaining request from the local cache.
[0093] Step 303, when the obtaining from the local cache is successful, returning the target data to the application client.
[0094] Step 304, when the obtaining from the local cache fails, obtaining the target data from the external data source, storing the target data into the local cache, and returning the target data to the application client.
[0095] In an optional embodiment, when the size of the target data exceeds a preset threshold during the process of storing the target data into the local cache, the target data is compressed to obtain compressed target data, and the compressed target data is stored into the local cache. Correspondingly, when the target data corresponding to the cache data acquisition request is compressed target data during the process of acquiring the target data corresponding to the cache data acquisition request from the local cache, the compressed target data is decompressed to obtain the target data. In this way, the compressed storage of the local cache data can be realized, thereby saving the space of the local cache.
[0096] The specific value of the preset threshold can be set according to actual needs, and the embodiment does not limit this.
[0097] Step 305: performing a maintenance operation on the local cache according to the maintenance information.
[0098] Step 306: generating a performance index of the local cache.
[0099] In the embodiment of the application, the performance index of the local cache is generated in real time, which can provide real-time cache data monitoring for the developer, so that the developer can better observe and understand the use of the cache data, and make optimization and adjustment accordingly.
[0100] Exemplarily, the performance index includes but is not limited to at least one of the following: local cache hit rate, local cache access time consumption, local cache occupancy rate, loading success rate of an external data source, loading time consumption of the external data source, thread pool state of a batch refresh queue.
[0101] For example, the local cache hit rate refers to the ratio of the number of successful times to the number of failed times when data is acquired from the local cache. By monitoring the local cache hit rate, the hit condition of the local cache can be understood in time, and the maintenance information (such as refresh time, invalidation time, etc.) of the local cache can be adjusted accordingly, so as to more reasonably maintain the local cache.
[0102] For example, by monitoring the local cache occupancy rate, the occupancy condition of the local cache can be understood in time, and the data in the local cache can be adjusted accordingly, such as clearing part of the data when the local cache occupancy rate is high, etc.
[0103] For example, by monitoring the thread pool state of the batch refresh queue, the number of threads simultaneously refreshed in the batch refresh queue, the number of threads waiting to be refreshed, etc. can be understood in time, and the refresh strategy of the batch refresh queue can be adjusted accordingly, such as adjusting the number of threads simultaneously refreshed, etc.
[0104] Figure 4A structure block diagram of a cache processing device in an embodiment of the present application. Figure 4 The cache processing device shown is applied to an application server.
[0105] As shown in the figure, the cache processing device can include the following modules: Figure 4
[0106] The configuration module 401 is configured to inject a local cache into a cache manager and configure maintenance information of the local cache.
[0107] The first acquisition module 402 is configured to, after receiving a cache data acquisition request initiated by an application client, acquire target data corresponding to the cache data acquisition request from the local cache.
[0108] The return module 403 is configured to, when the first acquisition module acquires the target data from the local cache successfully, return the target data to the application client.
[0109] The second acquisition module 404 is configured to, when the first acquisition module fails to acquire the target data from the local cache, acquire the target data from an external data source, store the target data into the local cache, and return the target data to the application client.
[0110] The maintenance module 405 is configured to perform a maintenance operation on the local cache according to the maintenance information.
[0111] Optionally, the configuration module 401 is specifically configured to load an inherited class of a data loader in an automatic injection manner, inject the local cache into the cache manager, and configure the maintenance information of the local cache by using a self-defined annotation.
[0112] Optionally, the local cache includes a plurality of cache keys, and the maintenance information includes refresh times of the cache keys; and the maintenance module 405 includes a first maintenance unit configured to, for a cache key that reaches a refresh time, acquire data corresponding to the cache key that reaches the refresh time from the external data source, and store the data corresponding to the cache key that reaches the refresh time into the cache key that reaches the refresh time.
[0113] Optionally, the local cache includes a plurality of cache keys, and the maintenance information includes refresh times and batch refresh conditions of the cache keys; and the maintenance module 405 includes a second maintenance unit configured to, after monitoring that the batch refresh conditions are met, for a cache key that reaches a refresh time, acquire data corresponding to the cache key that reaches the refresh time from the external data source by using a batch refresh queue, and store the data corresponding to the cache key that reaches the refresh time into the cache key that reaches the refresh time.
[0114] Optionally, the local cache comprises a plurality of cache keys, the maintenance information comprises invalidation time of each cache key; the maintenance module 405 comprises a third maintenance unit configured to delete data in a cache key reaching the invalidation time.
[0115] Optionally, the maintenance information comprises a cache interception class, the cache interception class is configured with an intercepted external data source, an intercepted notification mode and an intercepted cache mode; the maintenance module 405 comprises a fourth maintenance unit configured to listen to a data update notification of the intercepted external data source according to the intercepted notification mode; after listening to the data update notification, acquire update data from the intercepted external data source according to the intercepted cache mode, and store the update data into the local cache.
[0116] Optionally, the second acquisition module 404 is specifically configured to, when the size of the target data exceeds a preset threshold, compress the target data to obtain compressed target data, and store the compressed target data into the local cache; the first acquisition module 402 is specifically configured to, when the target data corresponding to the cache data acquisition request is compressed target data, decompress the compressed target data to obtain the target data.
[0117] Optionally, the apparatus further comprises a generation module configured to generate a performance index of the local cache; wherein the performance index comprises at least one of the following: local cache hit rate, local cache access time consumption, local cache occupancy rate, loading success rate of an external data source, loading time consumption of the external data source, thread pool state of a batch refresh queue.
[0118] In the embodiment, on one hand, by injecting the local cache into the cache manager, when receiving a cache data acquisition request initiated by an application client, the target data corresponding to the cache data acquisition request can be acquired from the local cache, when the acquisition from the local cache succeeds, the target data is returned to the application client, and when the acquisition from the local cache fails, the target data is acquired from an external data source, the target data is stored into the local cache, and the target data is returned to the application client, thus the application server can acquire the target data from the local cache, without acquiring from the external data source, thereby improving data acquisition efficiency and reducing resource consumption; on the other hand, by configuring maintenance information of the local cache, the application server can perform maintenance operation on the local cache according to the maintenance information, thereby optimizing the local cache.
[0119] The embodiment of the application further provides an electronic device, such as Figure 5As shown, the server comprises a processor 501, a communication interface 502, a memory 503 and a communication bus 504, wherein the processor 501, the communication interface 502 and the memory 503 communicate with each other through the communication bus 504.
[0120] The memory 503 is configured to store a computer program.
[0121] The processor 501 is configured to execute the program stored in the memory 503 to implement the following steps.
[0122] injecting the local cache into the cache manager and configuring maintenance information of the local cache;
[0123] after receiving a cache data acquisition request initiated by an application client, acquiring target data corresponding to the cache data acquisition request from the local cache;
[0124] when the acquisition from the local cache is successful, returning the target data to the application client;
[0125] when the acquisition from the local cache fails, acquiring the target data from an external data source, storing the target data into the local cache, and returning the target data to the application client;
[0126] performing a maintenance operation on the local cache according to the maintenance information.
[0127] Optionally, injecting the local cache into the cache manager and configuring maintenance information of the local cache comprises: loading an inherited class of a data loader by using an automatic injection mode, injecting the local cache into the cache manager, and configuring the maintenance information of the local cache by using a self-defined annotation.
[0128] Optionally, the local cache comprises a plurality of cache keys, and the maintenance information comprises refresh times of the cache keys; performing a maintenance operation on the local cache according to the maintenance information comprises: for a cache key that reaches a refresh time, acquiring data corresponding to the cache key that reaches the refresh time from the external data source, and storing the data corresponding to the cache key that reaches the refresh time into the cache key that reaches the refresh time.
[0129] Optionally, the local cache comprises a plurality of cache keys, and the maintenance information comprises refresh times of the cache keys and batch refresh conditions; performing a maintenance operation on the local cache according to the maintenance information comprises: after monitoring that the batch refresh conditions are met, for a cache key that reaches a refresh time, acquiring data corresponding to the cache key that reaches the refresh time from the external data source by using a batch refresh queue, and storing the data corresponding to the cache key that reaches the refresh time into the cache key that reaches the refresh time.
[0130] Optionally, the local cache comprises a plurality of cache keys, and the maintenance information comprises invalidation time of each cache key; and the maintenance operation performed on the local cache according to the maintenance information comprises deleting data in the cache key reaching the invalidation time.
[0131] Optionally, the maintenance information comprises a cache interception class, the cache interception class is configured with an intercepted external data source, an intercepted notification mode and an intercepted cache mode; and the maintenance operation performed on the local cache according to the maintenance information comprises: listening to a data update notification of the intercepted external data source according to the intercepted notification mode; and after the data update notification is listened to, acquiring update data from the intercepted external data source according to the intercepted cache mode, and storing the update data into the local cache.
[0132] Optionally, the storing of the target data into the local cache comprises: when the size of the target data exceeds a preset threshold, compressing the target data to obtain compressed target data, and storing the compressed target data into the local cache; and the acquiring of the target data corresponding to the cache data acquisition request from the local cache comprises: when the target data corresponding to the cache data acquisition request is compressed target data, decompressing the compressed target data to obtain the target data.
[0133] Optionally, the method further comprises: generating a performance index of the local cache; and the performance index comprises at least one of a local cache hit rate, a local cache access time consumption, a local cache occupancy rate, a loading success rate of an external data source, a loading time consumption of the external data source, and a thread pool state of a batch refresh queue.
[0134] The communication bus mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For the convenience of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or only one type of bus.
[0135] The communication interface is used for communication between the terminal and other devices.
[0136] The memory can include a random access memory (RAM) and can also include a non-volatile memory such as at least one disk memory. Optionally, the memory can also be at least one storage device located remotely from the aforementioned processor.
[0137] The aforementioned processor can be a general-purpose processor including a central processing unit (CPU), a network processor (NP), etc., and can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic, a discrete hardware component.
[0138] In yet another embodiment provided by the present application, a computer readable storage medium is also provided, which stores instructions therein, and when the instructions are run on a computer, the computer is caused to implement the cache processing method of any of the above embodiments.
[0139] In yet another embodiment provided by the present application, a computer program product containing instructions is also provided, and when the instructions are run on a computer, the computer is caused to implement the cache processing method of any of the above embodiments.
[0140] In the embodiments described above, all or some of the steps can be implemented by software, hardware or firmware, or any combination thereof. When implemented in software, all or some of the steps can be implemented in the form of one or more computer programs which are stored in a computer readable medium. The computer readable medium can include one or more of a computer readable storage medium and a computer readable signal medium. The computer readable storage medium can include one or more of a magnetic storage medium (e.g., one or more magnetic tapes), an electronic storage medium (e.g., one or more semiconductor memories or tapes), a mechanical storage medium (e.g., one or more recording chips), or the like. The computer readable signal medium can include a computer readable storage medium which is configured to transmit program code embedded in a modulated data signal. The modulated data signal is one example means for transmitting the program code (e.g., either modulated on a carrier or unmodulated). The program code embodied on the computer readable medium is for executing one or more of the steps in the embodiments described above on one or more of the computers in the system. The computer readable medium can be a computer program product.
[0141] It should be noted that, in the description above, the relative terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Also, the terms "comprises", "comprising", or any other variations thereof are intended to cover a non-exclusive inclusion, so that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but can include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without more limitations, an element defined by the phrase "comprising a..." does not exclude the existence of additional identical elements in the process, method, article, or apparatus that includes the element.
[0142] Each of the embodiments in the specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other. Each of the embodiments focuses on the difference from other embodiments. In particular, for the system embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the part of the description of the method embodiments.
[0143] The above merely provides the preferred embodiments of the application, and not intended to limit the protection scope of the application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the application shall fall within the protection scope of the application.
Claims
1. A caching method, characterized in that, Applied to an application server, the method includes: Inject the local cache into the cache manager and configure the maintenance information of the local cache; Upon receiving a cache data retrieval request initiated by the application client, the target data corresponding to the cache data retrieval request is retrieved from the local cache; Upon successful retrieval from the local cache, the target data is returned to the application client. If the retrieval fails from the local cache, the target data is retrieved from an external data source, stored in the local cache, and returned to the application client. Perform maintenance operations on the local cache based on the maintenance information; The maintenance information includes a cache interception class, which is configured with the external data source to be intercepted, the notification method for interception, and the caching method for interception. Performing maintenance operations on the local cache according to the maintenance information includes: listening for data update notifications from the external data source to be intercepted according to the notification method for interception; and upon receiving the data update notification, obtaining the updated data from the external data source to be intercepted according to the caching method for interception, and storing the updated data in the local cache.
2. The method according to claim 1, characterized in that, Inject the local cache into the cache manager and configure the maintenance information of the local cache, including: The data loader's inherited class is loaded using automatic injection, the local cache is injected into the cache manager, and the maintenance information of the local cache is configured through custom annotations.
3. The method according to claim 1, characterized in that, The local cache includes multiple cache keys, and the maintenance information also includes the refresh time of each cache key; Performing maintenance operations on the local cache based on the maintenance information further includes: For a cache key that has reached the refresh time, obtain the data corresponding to the cache key that has reached the refresh time from the external data source, and store the data corresponding to the cache key that has reached the refresh time into the cache key that has reached the refresh time.
4. The method according to claim 1, characterized in that, The local cache includes multiple cache keys, and the maintenance information also includes the refresh time and batch refresh conditions for each cache key; Performing maintenance operations on the local cache based on the maintenance information further includes: After detecting that the batch refresh condition is met, for the cache key that has reached the refresh time, the batch refresh queue is used to obtain the data corresponding to the cache key that has reached the refresh time from the external data source, and the data corresponding to the cache key that has reached the refresh time is stored in the cache key that has reached the refresh time.
5. The method according to claim 1, characterized in that, The local cache includes multiple cache keys, and the maintenance information also includes the expiration time of each cache key; Performing maintenance operations on the local cache based on the maintenance information further includes: Delete the data in the cache key that has reached its expiration time.
6. The method according to claim 1, characterized in that, Storing the target data into the local cache includes: when the size of the target data exceeds a preset threshold, compressing the target data to obtain compressed target data, and storing the compressed target data into the local cache; Obtaining the target data corresponding to the cache data retrieval request from the local cache includes: when the target data corresponding to the cache data retrieval request is compressed target data, decompressing the compressed target data to obtain the target data.
7. The method according to claim 1, characterized in that, The method further includes: Generate the performance metrics for the local cache; The performance metrics include at least one of the following: local cache hit rate, local cache access time, local cache occupancy rate, external data source loading success rate, external data source loading time, and thread pool status of the batch refresh queue.
8. A cache processing device, characterized in that, The device, used in an application server, includes: A configuration module is used to inject the local cache into the cache manager and configure the maintenance information of the local cache; The first acquisition module is used to receive a cache data acquisition request initiated by the application client and then acquire the target data corresponding to the cache data acquisition request from the local cache. The return module is used to return the target data to the application client when the first acquisition module successfully acquires the data from the local cache. The second acquisition module is used to acquire the target data from an external data source when the first acquisition module fails to acquire the target data from the local cache, store the target data in the local cache, and return the target data to the application client. The maintenance module is used to perform maintenance operations on the local cache based on the maintenance information. The maintenance information includes a cache interception class, which is configured with the external data source to be intercepted, the notification method for interception, and the caching method for interception. The maintenance module includes a fourth maintenance unit, which is used to listen for data update notifications from the external data source to be intercepted according to the notification method for interception; after listening for the data update notification, it retrieves the updated data from the external data source to be intercepted according to the caching method for interception and stores the updated data in the local cache.
9. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, when executing a program stored in memory, implements the method as described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-7.
Citation Information
Patent Citations
Data caching processing method and system, calculation equipment and readable storage medium
CN113032432A
Cache refreshing method, device and system
CN113190570A