Data caching method, device, server and computer-readable storage medium
By acquiring the mutex when data reads the requirements and updating its failure time when successful, the cache breakdown problem in the prior art is solved, and the system response speed and concurrency capability are improved.
Patent Information
- Application Number
- CN202110879427.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-02
- Publication Date
- 2025-06-17
- Estimated Expiration
- 2041-08-02
AI Technical Summary
When the existing data caching method cannot find the cached data that needs to be queried, it needs to query it from the background system or database multiple times, resulting in a decrease in the system response speed and a problem of cache breakdown.
By applying for a mutex to obtain the target read data when the data is required, if successful, write the data to the cache and update the failure time of the mutex. If it fails, query and read the data in the cache space, and release the mutex after the failure time of the mutex.
It effectively avoids multiple processes querying the background system or database multiple times, improves the system's concurrency capability and response speed, and avoids cache breakdown.
Smart Images

Figure CN113590661B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the technical field of fintech, and in particular, to a data caching method, apparatus, server, and computer-readable storage medium. Background Art
[0002] With the continuous development of fintech, the application scope of financial servers is becoming more and more extensive, and the number of users is increasing. In financial application services, caching technology is usually introduced to solve the problem of the response speed of network requests and improve the user experience.
[0003] However, in the existing data caching method, when the cached data to be queried cannot be found, it is still necessary to query the corresponding data from the background system or database multiple times, resulting in a decrease in the system response speed and the problem of cache breakdown. Summary of the Invention
[0004] Embodiments of the present invention provide a data caching method, apparatus, server, and computer-readable storage medium to improve the system response speed and avoid cache breakdown.
[0005] In a first aspect, embodiments of the present invention provide a data caching method, including:
[0006] When a first process generates a data reading requirement, it applies to obtain a mutex lock for the target reading data; wherein, the initial expiration time of the mutex lock is a first preset time;
[0007] If the first process successfully obtains the mutex lock, it writes the target reading data into the cache space to perform a refresh operation on the target reading data, and updates the expiration time of the mutex lock to a second preset time after completing the refresh operation; wherein, the second preset time is greater than the first preset time;
[0008] If the first process fails to obtain the mutex lock, it performs an operation of querying and reading the target reading data in the cache space;
[0009] After reaching the expiration time of the mutex lock, the mutex lock is released.
[0010] In a second aspect, embodiments of the present invention further provide a data caching apparatus, including:
[0011] A mutex lock acquisition module, configured to apply to obtain a mutex lock for the target reading data when a first process generates a data reading requirement; wherein, the initial expiration time of the mutex lock is a first preset time;
[0012] A cache refresh module, configured to write the target read data into a cache space when the first process successfully acquires the mutex lock, so as to perform a refresh operation on the target read data, and update the expiration time of the mutex lock to a second preset time after the refresh operation is completed; wherein, the second preset time is greater than the first preset time;
[0013] A cache query module, configured to perform an operation of querying and reading the target read data in the cache space when the first process fails to acquire the mutex lock;
[0014] A mutex lock release module, configured to release the mutex lock after the expiration time of the mutex lock is reached.
[0015] In a third aspect, an embodiment of the present invention further provides a server, where the server includes:
[0016] One or more processors;
[0017] A storage device, configured to store one or more programs,
[0018] When the one or more programs are executed by the one or more processors, the one or more processors implement the data caching method as described in any embodiment of the present invention.
[0019] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored, and when the program is executed by a processor, it implements the data caching method as described in any embodiment of the present invention.
[0020] The data caching method provided by the embodiment of the present invention includes that when a first process generates a data read requirement, it applies to acquire a mutex lock for the target read data; if the first process successfully acquires the mutex lock, it writes the target read data into the cache space to perform a refresh operation on the target read data, and after the refresh operation is completed, it resets and extends the expiration time of the mutex lock; if the first process fails to acquire the mutex lock, it performs an operation of querying and reading the target read data in the cache space; after the expiration time of the mutex lock is reached, it releases the mutex lock. It can be seen that when the first process of the embodiment of the present invention successfully acquires the mutex lock and performs a refresh operation on the target read data, other processes cannot acquire the mutex lock; when other processes successfully acquire the mutex lock and perform a refresh operation on the target read data, the first process can directly query and read the target read data from the cache space. Moreover, the embodiment of the present invention adjusts the expiration time of the mutex lock at different stages, which is beneficial to the efficient operation of data caching. Therefore, compared with the prior art, the embodiment of the present invention avoids multiple processes from querying the corresponding data from the background system or database multiple times, improves the concurrency and response speed of the system, and is beneficial to avoiding cache breakdown. Description of the Drawings
[0021] Figure 1 It is a schematic flowchart of a data caching method provided in the first embodiment of the present invention;
[0022] Figure 2 It is a schematic flowchart of a method for generating a cache key provided in the second embodiment of the present invention;
[0023] Figure 3 It is a schematic flowchart of a method for writing target read data into a cache space provided in the third embodiment of the present invention;
[0024] Figure 4 It is a schematic flowchart of a method for reading data from a cache space provided in the third embodiment of the present invention;
[0025] Figure 5 It is a schematic flowchart of a data caching method provided in the fifth embodiment of the present invention;
[0026] Figure 6 It is a schematic structural diagram of a data caching device provided in the fifth embodiment of the present invention;
[0027] Figure 7 It is a schematic structural diagram of a server provided in the sixth embodiment of the present invention. Detailed implementation manners
[0028] The present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It can be understood that the specific embodiments described herein are only used to explain the present invention, rather than limiting the present invention. In addition, it should be noted that, for the sake of description, only parts related to the present invention rather than all structures are shown in the drawings.
[0029] Embodiment 1
[0030] Figure 1 It is a schematic flowchart of a data caching method provided in the first embodiment of the present invention. This embodiment is applicable to the application scenario of caching hot data in mobile banking transactions. When a large number of terminals need to access the data of a certain business service, the server often receives a large number of data read requests simultaneously. It is possible that the target read data of multiple data read requests is the same, and this target read data may be cached in the local memory of the server, may be cached in other locations, or may not be cached at all, but needs to be read from the data source into the cache and then respond to the data read request. The embodiments of the present invention are applicable to the situation where the server supports the terminal to read data. This method can be executed by a data caching device, which can be implemented by hardware and / or software and is generally integrated in an electronic device, such as a server that supports terminal data access services. Refer to Figure 1 and the data caching method includes the following steps:
[0031] S110. When the first process has a data reading requirement, it applies to obtain the mutex lock for the target read data; wherein, the initial expiration time of the mutex lock is the first preset time.
[0032] The first process is the process for data reading in the server. Exemplarily, for a financial client, when 1000 users access the fund list simultaneously, the fund list is the target read data. The client will initiate 1000 data reading requests to the server. After receiving the data reading requests, the server generates processes to perform data reading. Limited by process resources, some data reading requests may be able to generate corresponding processes for processing. The process can be specifically for data reading or may have a data reading requirement during the execution of other functions. Then when the process has a data reading requirement, it applies to obtain the mutex lock of the fund list, but only one process can obtain it successfully. The mutex lock is configured with an expiration time. Within the expiration time of the mutex lock, other processes that want to read the target read data cannot obtain the mutex lock of the fund list until the expiration time of the mutex lock expires or is actively released. The role of the mutex lock of the target read data is that after the first process successfully obtains the mutex lock, within the expiration time of the mutex lock, only the first process can perform the refresh operation on the target read data to ensure the integrity of the refresh operation.
[0033] Exemplarily, the initial expiration time of the mutex lock is the first preset time, and the first preset time is a relatively short time, especially a short time relative to the cache data refresh interval, such as 5s. Thus, when the first process obtains the mutex lock, if it fails to successfully refresh the target read data, the mutex lock will expire quickly, allowing other processes to obtain the mutex lock. It will not let the failed refresh of the first process occupy too much time.
[0034] S120. If the first process successfully obtains the mutex lock, it writes the target read data into the cache space to perform the refresh operation of the target read data, and after completing the refresh operation, updates the expiration time of the mutex lock to the second preset time; wherein, the second preset time is greater than the first preset time.
[0035] Taking the example of 1000 processes applying for the mutex lock of the fund list, when the first process successfully obtains the mutex lock of the fund list, it can refresh the fund list. And when the refresh is successful, the expiration time of the mutex lock is reset. Specifically, the expiration time is increased. Then, within the expiration time of the mutex lock, since no process can obtain the mutex lock of the fund list, the fund list is no longer refreshed until the expiration time of the mutex lock arrives and a new process obtains the mutex lock of the fund list for the next refresh. Within the expiration time of the mutex lock, the other 999 processes cannot obtain the mutex lock of the fund list and thus cannot refresh the fund list. Optionally, the second preset time is the preset minimum cache refresh time interval, which is a preset value of the system. Setting the second preset time in this way can prevent the just-refreshed cache data from being immediately refreshed again due to other processes obtaining the mutex lock. It needs to last at least the minimum cache refresh time interval before it may be refreshed due to other processes obtaining the mutex lock. Exemplarily, define the variable refreshInterval to represent the minimum cache refresh time interval, with a default value of 60s, that is, the second preset time is 60s; the first preset time is less than 60s, for example, 5s. The second preset time can also be set longer according to specific requirements.
[0036] Optionally, the refresh operation can specifically be that the first process reads the target read data from the data source in the non-cache space and writes it into the cache space. Among them, the non-cache space can be other systems such as the background system, databases, etc. Exemplarily, the method with an annotation can be executed to obtain the data to be cached. For example, it is annotated with @CacheProperty. Since within the expiration time of the mutex lock, only one process can read the target read data from the non-cache space, it avoids multiple processes reading the non-cache data source simultaneously, which is beneficial to solving the problem of cache breakdown.
[0037] Among them, although there may be target read data in the cache space, when the first process occupies the mutex lock, it still reads from the non-cache space. The reason for this setting is that after a period of time, the data in the data source may have been updated, and it cannot be guaranteed that the data in the cache space is the latest, so it can only be read from the data source again.
[0038] S130. If the first process fails to obtain the mutex lock, perform the operation of querying and reading the target read data in the cache space.
[0039] Among them, the failure of the first process to obtain the mutex lock indicates that the mutex lock is occupied by other processes, and other processes may be performing data refreshing operations. Still taking the example of 1000 processes applying for the mutex lock of the fund list, when the first process fails to obtain the mutex lock of the fund list, one of the other 999 processes may obtain the mutex lock of the fund list and can perform the refreshing operation on the fund list. At this time, regardless of whether the mutex lock of the fund list is successfully obtained by other processes, the first process can still perform the operation of querying and reading the fund list in the cache space. If the first process performs the querying and reading operations before the fund list is refreshed, the fund list queried and read by the first process is the data before the refresh; if the first process performs the querying and reading operations after the fund list is refreshed, the fund list queried and read by the first process is the data after the refresh. In short, after the first process fails to obtain the mutex lock, it can still query and read the target read data, which is beneficial to improving the system response speed.
[0040] S140. After reaching the expiration time of the mutex lock, release the mutex lock.
[0041] Among them, when setting the expiration time of the mutex lock, there is a process from small to large. Specifically, before the first process successfully refreshes the target read data, the expiration time of the mutex lock is set to be smaller; when the first process successfully refreshes the target read data, the expiration time of the mutex lock is set to be larger. The reason for this setting is that if the expiration time of the mutex lock is set to be larger at the beginning, even if the first process fails to perform the refresh operation, it will always occupy the mutex lock, and other processes cannot obtain the mutex lock again. Therefore, the embodiment of the present invention is beneficial to avoiding the invalid occupation time of the mutex lock and improving the system response speed.
[0042] It can be seen that when the first process in this embodiment successfully obtains the mutex lock and performs the refresh operation on the target read data, other processes cannot obtain the mutex lock; when other processes successfully obtain the mutex lock and perform the refresh operation on the target read data, the first process can directly query and read the target read data from the cache space. Moreover, this embodiment adjusts the expiration time of the mutex lock at different stages, which is beneficial to the efficient operation of data caching. Therefore, compared with the prior art, this embodiment avoids multiple processes from querying the corresponding data from the background system or database multiple times, improves the concurrency and response speed of the system, and is beneficial to avoiding cache breakdown.
[0043] Based on the above technical solutions, optionally, during the process of the first process refreshing or reading the target read data, it further includes: when the second process fails to obtain the mutex lock of the target read data, it performs the operation of querying and reading the target read data in the cache space. Among them, the target read data of the second process is the same as that of the first process. The second process and the first process can be initiated simultaneously, and the second process can also be initiated before or after the first process.
[0044] Exemplarily, taking the mutex for applying to obtain the fund list as an example, when the first process successfully obtains the mutex of the fund list and refreshes the fund list, the second process can perform the operation of querying and reading the fund list in the cache space. When the first process fails to obtain the mutex of the fund list and performs the operation of querying and reading the fund list in the cache space, the second process can also perform the operation of querying and reading the fund list in the cache space. During the process of reading the target read data, only individual processes can successfully obtain the mutex and refresh the cache at the refresh interval, while almost all processes obtain the target read data by querying and reading the cache space. Therefore, the system response speed of the data caching method provided by the embodiments of the present invention is relatively fast.
[0045] Based on the above technical solutions, optionally, before applying to obtain the mutex of the target read data, it further includes: if the cache expiration time of the target read data is not set or the cache expiration time is less than the preset minimum cache refresh time interval, reset the cache expiration time to a third preset time; wherein, the third preset time is greater than the minimum cache refresh time interval, that is, the cache expiration time is greater than the expiration time of the mutex. Preferably, the third preset time is twice the minimum cache refresh time interval, that is, the cache expiration time is twice the expiration time of the mutex.
[0046] Among them, the cache expiration time refers to the expiration time of the cached data in the cache space. Within the cache expiration time, all processes can perform the operation of querying and reading the target read data in the cache space. When the cache expiration time arrives, all processes cannot perform the operation of querying and reading the target read data in the cache space and need to wait for the cached data to be refreshed again and reset the cache expiration time. The embodiments of the present invention set the cache expiration time longer than the expiration time of the mutex to prevent other processes from being unable to query and read data from the cache space during the refresh, resulting in other processes waiting for the cached data to be successfully loaded. Therefore, the embodiments of the present invention are beneficial to avoiding other processes blocking and waiting for the cache to be refreshed.
[0047] Embodiment 2
[0048] This embodiment is a refinement based on the above - mentioned embodiment. Optionally, when the first process generates a data reading requirement, applying for and obtaining a mutex lock for the target reading data includes: when the first process generates a data reading requirement, generating a cache key for the target reading data, and applying for and obtaining a mutex lock for the target reading data according to the cache key. Among them, the cache system stores at least one piece of cached data with a cache key, and when the first process generates a data reading requirement, the value corresponding to the cache key is known. The cache key generated during data reading is the same as the cache key generated during data writing because the rules are the same, and the cache key serves as the unique identifier for refreshing, querying, and reading cached data.
[0049] Exemplarily, the cache space includes a first - level local memory and a second - level distributed cache space. The second - level distributed cache space can be, for example, a Remote Dictionary Server (Redis). At this time, define a variable rediskey to represent the cache key of the target reading data.
[0050] Specifically, Figure 2 FIG. is a schematic flow chart of generating a cache key provided for the second embodiment of the present invention. Refer to Figure 2 , on the basis of the above - mentioned technical solution, optionally, generating a cache key for the target reading data includes the following steps:
[0051] S210. Determine whether the cache key expression in the data reading requirement is a non - null value.
[0052] Exemplarily, the cache space includes a first - level local memory and a second - level distributed cache space. The second - level distributed cache space can be Redis, and use a variable redisKeyExpression to represent the cache key expression. Among them, the form of the non - null cache key expression can be declaring that the cache key expression redisKeyExpression includes a system name (such as a mobile phone), a customer number, or a business scenario (such as a card list), etc. Exemplarily, declare redisKeyExpression = "mobile:{masterId}:AccoutList", where the variable mobile represents a mobile phone, the variable masterId represents a customer number, and the variable AccoutList represents a card list.
[0053] If so, execute S220. Generate a cache key according to the object navigation graph language expression.
[0054] Among them, the object navigation graph language refers to the ognl language. Exemplarily, use a redisKey ognl expression to represent the object navigation graph language expression, that is, for the case where the cache key expression in the data reading requirement is a non - null value, use the redisKey ognl expression to generate a cache key.
[0055] If not, then execute S230 to determine whether the description of the cache key method name is a non - null value.
[0056] Among them, the variable redisKeyMethodName represents the description of the cache key method name.
[0057] If so, then execute S240 to call the cache key generation method to generate a cache key.
[0058] Among them, for the case where the variable redisKeyExpression is a null value and the variable redisKeyMethodName is a non - null value, call the cache key generation method to generate the cache key redis key.
[0059] If not, then execute S250 to use the universally unique identifier generated by concatenating the method signature and the serialized parameters as the cache key.
[0060] Among them, the Universally Unique Identifier (UUID) enables all elements in a distributed system to have unique identification information without the need for a central control end to specify the identification information. Each process can create a UUID that does not conflict with other processes. For the case where the variable redisKeyExpression is a null value and the variable redisKeyMethodName is a null value, generate the universally unique identifier UUID by concatenating the method signature and the serialized parameters.
[0061] S260. Return the cache key.
[0062] The steps of generating the cache key are implemented through S210 - S260. These steps can be used as a sub - routine. Exemplarily, the sub - routine for generating the cache key is represented by generateCacheKey, and the cache key value of the cached data generated by it is cacheKey. When the main program needs it, it can call the sub - routine named generateCacheKey.
[0063] Embodiment III
[0064] This embodiment is refined based on the above - mentioned embodiments. Optionally, writing the target read data into the cache space includes: writing the target read data into the secondary distributed cache space and writing it into the primary local memory.
[0065] Among them, the first-level cache uses local memory, with fast request caching speed and no excessive network overhead. The second-level cache uses distributed caching. Exemplarily, the second-level distributed cache can be Redis, for example. Redis supports multiple types of data structures, such as strings, hashes, lists, sets, sorted sets and range queries, bitmaps, hyperloglogs, and geospatial index radius queries. Redis has built-in replication, LUA scripts, LRU-driven events, transactions, and different levels of disk persistence, and provides high availability through Redis Sentinel and automatic partitioning.
[0066] The embodiment of the present invention adopts a structure of first-level local memory + second-level distributed cache. When querying data, the data can be first obtained from the first-level cache. If the data cannot be obtained from the first-level cache, the data is obtained from the second-level cache, thereby avoiding high-concurrency queries on the background system or database and improving the concurrency ability and response speed of the system.
[0067] Specifically, Figure 3 is a schematic flow diagram of writing target read data into a cache space provided in Embodiment 3 of the present invention. Refer to Figure 3 , based on the above technical solutions, optionally, writing the target read data into the second-level distributed cache space and writing it into the first-level local memory includes the following steps:
[0068] S310. Determine whether the value range of the obtained target read data is a non-null value.
[0069] Among them, the value range of the target read data being a null value means that the cached data is a null pointer null; correspondingly, the value range of the target read data being a non-null value means that the cached data is not a null pointer null.
[0070] If so, execute S320. Cache the target read data into the first-level local memory as the first-level cache data.
[0071] If not, execute S330. Set the value range of the target read data to a fixed string, and then execute S320. Cache the target read data into the first-level local memory as the first-level cache data.
[0072] Among them, the fixed string is a string representing empty data. For example, __EMPTY_DATA_INITIALIZED is used to represent the fixed string. The fixed string representing empty data can be converted into a null pointer null in subsequent steps.
[0073] S340. Cache the first-level cache data into the second-level distributed cache space as the second-level cache data.
[0074] The steps of writing the target read data into the cache space are implemented through S310 - S340. These steps can be used as a subroutine, represented by chahe2Level, which is the subroutine for writing the target read data into the cache space. When the main program needs it, the subroutine named chahe2Level can be called.
[0075] Optionally, corresponding to writing the target read data into the secondary distributed cache space and into the primary local memory, reading data from the cache space is respectively from the primary local memory and the secondary distributed cache space. Specifically, Figure 4 This is a schematic flowchart of a process for reading data from the cache space provided in Embodiment 3 of the present invention. Refer to Figure 4 Based on the above technical solutions, optionally, the operation of querying and reading the target read data in the cache space includes the following steps:
[0076] S410: Query the target read data from the primary local memory.
[0077] S420: Determine whether the target read data is queried from the primary local memory.
[0078] If the target read data is queried from the primary local memory, then execute S430: Return the target read data.
[0079] Otherwise, execute S440: Query the target read data from the secondary distributed cache space.
[0080] S450: Determine whether the target read data is queried and read from the secondary distributed cache space.
[0081] If the target read data is queried and read from the secondary distributed cache space, then execute S460: Store the target read data in the primary local memory, and then return the target read data.
[0082] Otherwise, execute S470: Return a null pointer null.
[0083] The steps of reading data from the cache space are implemented through S410 - S470. These steps can be used as a subroutine, represented by getCache2Level, which is the subroutine for reading data from the cache space. When the main program needs it, the subroutine named getCache2Level can be called.
[0084] Embodiment 4
[0085] Figure 5 This is a schematic flowchart of a data caching method provided in Embodiment 5 of the present invention. Refer to Figure 5, based on the above embodiments, optionally, when a process has a data reading requirement, the data caching method includes the following steps:
[0086] S510. Call the subroutine for generating a cache key. For example, call the subroutine named generateCacheKey.
[0087] S520. Determine whether the cache expiration time is not set or the cache expiration time is less than the minimum cache refresh time interval.
[0088] If so, execute S530. Reset the cache expiration time to twice the minimum cache refresh time interval.
[0089] Then execute S540. Apply for and obtain the mutex lock for the target read data.
[0090] Otherwise, directly execute S540. Apply for and obtain the mutex lock for the target read data.
[0091] S550. Determine whether the mutex lock is successfully obtained.
[0092] If so, execute S560. Read the target read data from the data source in the non-cache space.
[0093] S570. Call the subroutine for cache refresh. For example, call the subroutine named cache2Level.
[0094] S580. Determine whether the expiration time of the mutex lock is less than the minimum cache refresh time interval.
[0095] If so, execute S590. Update the expiration time of the mutex lock to the minimum cache refresh time interval.
[0096] Then execute S5A0. Return the target read data.
[0097] Otherwise, directly execute S5A0. Return the target read data.
[0098] If the mutex lock acquisition fails, then execute S5B0. Call the subroutine for querying the cache. For example, call the subroutine named getCache2Level.
[0099] S5C0. Determine whether the data obtained from the two-level cache is an empty string.
[0100] If so, execute S5D0. Return the null pointer null.
[0101] Otherwise, execute S5E0. Determine whether the data obtained from the two-level cache needs to be re-cached.
[0102] Among them, when the following conditions are met, re-caching is required:
[0103] (1) When the secondary distributed cache data is not initialized or invalid, wait for the data initialization to complete. Exemplarily, define a variable waitUntilCached to represent the secondary distributed cache data. When the variable waitUntilCached is not initialized or invalid, determine whether to wait for the data initialization to complete. If the variable value is false, there is no need to wait for the data initialization to complete; if the variable value is true, it is necessary to wait for the data initialization to complete.
[0104] (2) The data obtained from the two-level cache is a null pointer.
[0105] (3) The number of retries is greater than 0.
[0106] When and only when all three of the above conditions are met, it is necessary to re-cache and start over from S510.
[0107] Otherwise, execute S5A0 and return the target read data. That is, return the data obtained from the two-level cache.
[0108] In this embodiment, through S510 - S5E0, data caching is achieved. And as can be seen from the above steps, when a process performs a refresh or read operation on the target read data, other processes can directly query and read the same target read data from the cache space. Also, when a process performs a refresh operation on the target read data, other processes cannot perform the refresh operation on the target data. Therefore, compared with the prior art, the embodiment of the present invention does not need to use multiple processes to query the corresponding data from the background system or database multiple times, improving the concurrency ability and response speed of the system and helping to avoid cache breakdown.
[0109] Embodiment Five
[0110] Figure 6 The following is a schematic structural diagram of a data caching device provided by Embodiment Five of the present invention. This data caching device can be implemented by hardware and / or software and is generally integrated in an electronic device, such as a server that supports terminal data access services, etc. Refer to Figure 6 , the data caching device includes:
[0111] A mutex acquisition module 610, configured to apply for and acquire a mutex for the target read data when a first process generates a data read requirement; wherein, the initial invalidation time of the mutex is a first preset time;
[0112] A cache refresh module 620, configured to write the target read data into the cache space to perform a refresh operation on the target read data when the first process successfully acquires the mutex, and update the invalidation time of the mutex to a second preset time after the refresh operation is completed; wherein, the second preset time is greater than the first preset time;
[0113] A cache query module 630, configured to perform an operation of querying and reading target read data in a cache space when a first process fails to obtain a mutex lock.
[0114] A mutex lock release module 640, configured to release the mutex lock after the expiration time of the mutex lock is reached.
[0115] The data caching device provided by the embodiments of the present invention can execute the data caching method provided by any embodiment of the present invention, and has corresponding functional modules and beneficial effects for executing the method.
[0116] Based on the above technical solutions, optionally, the cache query module is further configured to perform an operation of querying and reading target read data in a cache space when a second process fails to obtain a mutex lock for the target read data.
[0117] Based on the above technical solutions, optionally, the second preset time is a preset minimum cache refresh time interval.
[0118] Based on the above technical solutions, optionally, the data caching device further includes a cache expiration time reset module, configured to reset the cache expiration time to a third preset time when the cache expiration time of the target read data is not set or the cache expiration time is less than the preset minimum cache refresh time interval; wherein, the third preset time is greater than the minimum cache refresh time interval.
[0119] Based on the above technical solutions, optionally, the third preset time is twice the minimum cache refresh time interval.
[0120] Based on the above technical solutions, optionally, the cache refresh module is further configured to enable the first process to read the target read data from a data source in a non-cache space and write it into the cache space.
[0121] Based on the above technical solutions, optionally, the data caching device further includes: a cache key generation module, configured to generate a cache key for the target read data when the first process generates a data reading requirement, and apply for obtaining a mutex lock for the target read data according to the cache key; wherein, the cache system stores at least one cache data having a cache key.
[0122] Based on the above technical solutions, optionally, the cache key generation module is further configured to generate a cache key according to the object navigation graph language expression when the cache key expression in the data reading requirement is a non-null value; when the cache key expression in the data reading requirement is a null value and the cache key method name description is a non-null value, call the cache key generation method to generate a cache key; when the cache key expression in the data reading requirement is a null value and the cache key method name description is a null value, use the universally unique identifier generated by concatenating the method signature and serializing the parameters as the cache key.
[0123] Based on the above technical solutions, optionally, the cache refresh module is further configured to write the target read data into the secondary distributed cache space and write it into the primary local memory.
[0124] Correspondingly, the cache query module is further configured to query the target read data from the primary local memory; if the target read data is queried from the primary local memory, return the target read data; otherwise, query the target read data from the secondary distributed cache space; if the target read data is queried and read from the secondary distributed cache space, store the target read data in the primary local memory and then return the target read data.
[0125] Based on the above technical solutions, optionally, the cache refresh module is further configured to cache the target read data into the primary local memory as the primary cache data when the value range of the target read data is a non-null value; when the target read data is a null pointer, set the value range of the target read data to a fixed string, and then cache the fixed string into the primary local memory as the primary cache data; cache the primary cache data into the secondary distributed cache space as the secondary cache data.
[0126] Embodiment Six
[0127] Figure 7 The following is a schematic structural diagram of a server provided by Embodiment Six of the present invention. Refer to Figure 7 , the server includes a processor 710, a memory 720, an input device 730, and an output device 740; the number of processors 710 in the server can be one or more, Figure 7 taking one processor 710 as an example; the processor 710, the memory 720, the input device 730, and the output device 740 in the server can be connected through a bus or other means, Figure 7 taking connection through a bus as an example.
[0128] The memory 720, being a computer-readable storage medium, can be used to store software programs, computer-executable programs, and modules, such as the program instructions / modules corresponding to the data caching method in the embodiments of the present invention (for example, the mutex acquisition module, the cache refresh module, the cache query module, and the mutex expiration time reset module). The processor 710 executes various functional applications and data processing of the server by running the software programs, instructions, and modules stored in the memory 720, thereby implementing the above-mentioned data caching method.
[0129] The memory 720 may mainly include a program storage area and a data storage area. Among them, the program storage area can store the operating system and application programs required for at least one function; the data storage area can store data created according to the use of the server, etc. In addition, the memory 720 may include high-speed random access memory and may also include non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, or other non-volatile solid-state storage devices. In some instances, the memory 720 may further include a memory remotely set relative to the processor 710, and these remote memories can be connected to the server through a network. Examples of the above network include but are not limited to the Internet, an enterprise intranet, a local area network, a mobile communication network, and their combinations.
[0130] The input device 730 can be used to receive input digital or character information and generate key signal inputs related to the function control of the server. The output device 740 may include display devices such as a display screen.
[0131] Embodiment Seven
[0132] Embodiment Seven of the present invention further provides a storage medium containing computer-executable instructions, and the computer-executable instructions are used to execute a data caching method when executed by a computer processor. The method includes:
[0133] When the first process generates a data reading requirement, it applies to acquire the mutex of the target reading data; wherein, the initial expiration time of the mutex is the first preset time;
[0134] If the first process successfully acquires the mutex, it writes the target reading data into the cache space to perform the refresh operation of the target reading data, and updates the expiration time of the mutex to the second preset time after completing the refresh operation; wherein, the second preset time is greater than the first preset time;
[0135] If the first process fails to acquire the mutex, it performs the operation of querying and reading the target reading data in the cache space;
[0136] After reaching the expiration time of the mutex, the mutex is released.
[0137] Of course, for a storage medium containing computer-executable instructions provided by an embodiment of the present invention, the computer-executable instructions are not limited to the method operations described above, and can also execute related operations in the data caching method provided by any embodiment of the present invention.
[0138] From the above description of the embodiments, those skilled in the art can clearly understand that the present invention can be implemented by means of software and necessary general-purpose hardware. Of course, it can also be implemented by hardware, but in many cases the former is a better implementation. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. The computer software product can be stored in a computer-readable storage medium, such as a floppy disk, a read-only memory (ROM), a random access memory (RAM), a flash memory (FLASH), a hard disk, or an optical disc of a computer, including several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in various embodiments of the present invention.
[0139] It should be noted that in the embodiments of the above data caching device, the various modules included are only divided according to functional logic, but are not limited to the above division, as long as the corresponding functions can be achieved; in addition, the specific names of the functional modules are only for the convenience of mutual distinction and do not limit the protection scope of the present invention.
[0140] Note that the above is only a preferred embodiment of the present invention and the applied technical principle. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and various obvious changes, re-adjustments, and substitutions can be made by those skilled in the art without departing from the protection scope of the present invention. Therefore, although the present invention has been described in detail through the above embodiments, the present invention is not limited to the above embodiments. Without departing from the concept of the present invention, more other equivalent embodiments can be included, and the scope of the present invention is determined by the scope of the appended claims.
Claims
1. A data caching method, characterized in that, Including: When a first process generates a data reading requirement, it applies to obtain a mutex lock for the target reading data; wherein, an initial expiration time of the mutex lock is a first preset time, and the mutex lock is used for the first process to perform a refresh operation on the target reading data within the expiration time of the mutex lock after the first process successfully obtains the mutex lock; If the first process successfully obtains the mutex lock, it writes the target reading data into a cache space to perform the refresh operation on the target reading data, and updates the expiration time of the mutex lock to a second preset time after completing the refresh operation; wherein, the second preset time is greater than the first preset time, and the second preset time is a preset minimum cache refresh time interval; If the first process fails to obtain the mutex lock, it performs an operation of querying and reading the target reading data in the cache space; After reaching the expiration time of the mutex lock, the mutex lock is released; Wherein, the first process writing the target reading data into the cache space includes: The first process reads the target reading data from a data source in a non-cache space and writes it into the cache space; Wherein, the first process applying to obtain the mutex lock for the target reading data when generating a data reading requirement includes: When the first process generates a data reading requirement, it generates a cache key for the target reading data and applies to obtain the mutex lock for the target reading data according to the cache key; wherein, the cache system stores at least one cache data with a cache key, and the cache key is a unique identifier for refreshing, querying, and reading the cache data; Generating the cache key for the target reading data includes: If the cache key expression in the data reading requirement is a non-empty value, the cache key is generated according to an object navigation graph language expression; If the cache key expression in the data reading requirement is an empty value and the cache key method name description is a non-empty value, a cache key generation method is called to generate the cache key; If the cache key expression in the data reading requirement is an empty value and the cache key method name description is an empty value, a universally unique identifier generated by concatenating method signatures and serializing parameters is used as the cache key.
2. The data caching method according to claim 1, characterized in that, During the process of the first process refreshing or reading the target reading data, it further includes: When a second process fails to obtain the mutex lock for the target reading data, it performs an operation of querying and reading the target reading data in the cache space.
3. The data caching method according to claim 1, characterized in that, Before applying to obtain the mutex lock for the target reading data, it further includes: If the cache expiration time of the target reading data is not set or the cache expiration time is less than the preset minimum cache refresh time interval, the cache expiration time is reset to a third preset time; wherein, the third preset time is greater than the minimum cache refresh time interval.
4. The data caching method according to claim 3, characterized in that, The third preset time is twice the minimum cache refresh time interval.
5. The data caching method according to claim 1, characterized in that: Writing the target reading data into the cache space includes: writing the target reading data into a secondary distributed cache space and into a primary local memory; Correspondingly, performing the operation of querying and reading the target reading data in the cache space includes: Query the target read data from the first-level local memory; If the target read data is queried from the first-level local memory, return the target read data; Otherwise, query the target read data from the second-level distributed cache space; If the target read data is queried and read from the second-level distributed cache space, store the target read data in the first-level local memory and then return the target read data.
6. The data caching method according to claim 5, characterized in that, The writing the target read data into the second-level distributed cache space and into the first-level local memory includes: If the value range of the target read data is obtained as a non-null value, cache the target read data in the first-level local memory as first-level cache data; If the target read data is obtained as a null pointer, set the value range of the target read data to a fixed string, and then cache the fixed string in the first-level local memory as first-level cache data; Cache the first-level cache data in the second-level distributed cache space as second-level cache data.
7. A data caching device, characterized in that, Includes: A mutex acquisition module, configured to, when a first process generates a data read requirement, apply to acquire a mutex for the target read data; wherein, an initial expiration time of the mutex is a first preset time, and the mutex is used for the first process to perform a refresh operation on the target read data within the expiration time of the mutex after the first process successfully acquires the mutex; A cache refresh module, configured to, when the first process successfully acquires the mutex, write the target read data into the cache space to perform a refresh operation on the target read data, and update the expiration time of the mutex to a second preset time after the refresh operation is completed; wherein, the second preset time is greater than the first preset time, and the second preset time is a preset minimum cache refresh time interval; A cache query module, configured to, when the first process fails to acquire the mutex, perform an operation of querying and reading the target read data in the cache space; A mutex release module, configured to release the mutex after the expiration time of the mutex is reached; Wherein, the cache refresh module is further configured for the first process to read the target read data from a data source outside the cache space and write it into the cache space; A cache key generation module, configured for the first process to generate a cache key for the target read data when generating a data read requirement, and apply to acquire a mutex for the target read data according to the cache key; wherein, the cache system stores at least one cache data with a cache key, and the cache key is a unique identifier for refreshing, querying, and reading the cache data. The cache key generation module is further configured to generate the cache key according to the object navigation graph language expression when the cache key expression in the data reading requirement is a non-null value; call the cache key generation method to generate the cache key when the cache key expression in the data reading requirement is a null value and the cache key method name description is a non-null value; and use the universally unique identifier generated after serializing the method signature and splicing parameters as the cache key when the cache key expression in the data reading requirement is a null value and the cache key method name description is a null value.
8. A server, characterized in that, The server includes: One or more processors; A storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the data caching method according to any one of claims 1-6.
9. A computer-readable storage medium, on which a computer program is stored, characterized in that, When the program is executed by the processor, it implements the data caching method according to any one of claims 1-6.
Citation Information
Patent Citations
Data processing method, related equipment and computer readable storage medium
CN112035496A