Data caching method and device

By dynamically adjusting the cache data priority queue and sharding processing, the problems of high concurrency, high QPS lock competition and unsatisfactory cache hit rate in the existing technology are solved, and efficient cache management and performance improvement are achieved.

CN113094392BActive Publication Date: 2024-10-18BEIJING WODONG TIANJUN INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202010023174.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-01-09
Publication Date
2024-10-18
Estimated Expiration
2040-01-09

AI Technical Summary

Technical Problem

Existing caching strategies take a long time to handle read and write operations caused by lock competition under high concurrency and high QPS conditions, and cannot be dynamically adjusted to improve the cache hit rate. The hit rate is not ideal especially during peak and trough periods of access.

Method used

Adopt the method of dynamically adjusting the capacity of the cached data priority queue, calculate the priority through multiple cache queues with different priorities and adaptive access counting factors, dynamically adjust the storage and elimination strategy of cached data according to user access conditions, and adjust the cache Data is sharded to reduce lock contention.

Benefits of technology

It effectively improves the cache hit rate, reduces the time-consuming of read and write operations, solves the problem of lock competition, and maintains system performance under high concurrency and high QPS conditions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113094392B_ABST
    Figure CN113094392B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and device for data caching, and relates to the field of computer technology. A specific implementation of the method includes: querying data from a cache, the cache includes multiple cache queues with different priorities; if the data is not queried, the data is saved to the tail node of the cache queue with the lowest priority; if the cache capacity exceeds a preset lower limit threshold, the header data is taken from the head node of the cache queue with the lowest priority, and the priority of the header data is calculated; if the priority of the header data is less than the lowest priority, the header data is deleted from the cache. This implementation can dynamically adjust the capacity of the cache data priority queue according to the user's access to the cache data, effectively improving the cache hit rate in scenarios such as access peak and trough periods.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method and device for data caching. Background Art

[0002] Cache plays a very important role in today's computer systems. From CPUs (central processing units) to Internet websites, we all need to use cache to improve system performance. However, caches require relatively expensive hardware to implement, so their size is limited. Therefore, we need to use different strategies to eliminate infrequently used content and replace it with content that is more likely to be used, increase the cache hit rate, and thus improve the use of cache.

[0003] In order to achieve this goal, a variety of cache access and elimination strategies have been proposed. For example: FIFO (First Input First Output), a first-in-first-out algorithm, determines the storage time, and the data farthest from the current is eliminated first; LRU (The Least Recently Used), the least recently used algorithm, determines the time of recent use, and the farthest data is eliminated first; LFU (Least Frequentl8 Used), the least recently used algorithm, in a period of time, the data with the least number of uses is eliminated first; LRU-K, an extension of the LRU algorithm, expands the judgment standard of "recently used once" to "recently used K times"; 2Q (Two queues), similar to LRU-2, the difference is that one is a FIFO queue and the other is a LRU queue; LIRS (Low Inter-reference Recency Set), which determines which data is eliminated by two factors: the most recent access time of the LRU algorithm and the number of repeated blocks accessed between two consecutive accesses to the same block.

[0004] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:

[0005] In the existing cache strategies, each cache strategy has a certain focus, but the situations considered are all one-sided, which makes the cache adjustment, read and write operations, cache hit rate and other aspects not ideal. Summary of the invention

[0006] In view of this, an embodiment of the present invention provides a method and device for data caching, which can dynamically adjust the capacity of the cache data priority queue according to the user's access to the cached data, effectively improving the cache hit rate in scenarios such as access peak periods and trough periods.

[0007] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for data caching is provided.

[0008] A method for data caching, comprising: querying data from a cache, the cache comprising a plurality of cache queues with different priorities; if the data is not found, saving the data to the tail node of the cache queue with the lowest priority; if the cache capacity exceeds a preset lower limit threshold, taking out header data from the head node of the cache queue with the lowest priority and calculating the priority of the header data; if the priority of the header data is less than the lowest priority, deleting the header data from the cache.

[0009] Optionally, before the data is saved to the tail of the lowest priority cache queue, it also includes: judging whether the cache capacity reaches a preset upper limit threshold; if so, directly deleting the header data of the head node of the lowest priority cache queue.

[0010] Optionally, if the data is found in the cache, the priority of the data is calculated, and the data is moved to the tail node of the cache queue of the priority.

[0011] Optionally, the cache also stores an access count factor and a most recent access time of the data; and the priority of the data is determined according to the access count factor of the data.

[0012] Optionally, if the data is not found, the access count factor and the most recent access time of the data are set to default initial values.

[0013] Optionally, if the data is queried from the cache, calculating the priority of the data includes: if the data is queried from the cache, calculating the latest access count factor based on the access count factor and the most recent access time of the data stored in the cache; determining the priority of the data based on the latest access count factor.

[0014] Optionally, if the data is queried from the cache, the latest access count factor is calculated based on the access count factor and the most recent access time of the data stored in the cache, including: if the data is queried from the cache, first calculate the probability of an increase in the access count factor P = 1 / (access count factor*ln access count factor+1), then compare the probability P of an increase in the access count factor with a random number R, if P is greater than R, then increase the access count factor by 1, where R∈[0,1); update the access count factor; then calculate the latest access count factor based on the time difference between the current system time and the most recent access time of the data, the latest access count factor = access count factor - (system latest time - most recent access time of the data record) / T, where T is a time factor variable.

[0015] Optionally, calculating the priority of the header data includes: calculating the latest access count factor based on the time difference between the current system time and the most recent access time of the data, the latest access count factor = access count factor - (system latest time - most recent access time of the data record) / T, where T is the time factor variable.

[0016] Optionally, the cache includes multiple cache shards, and data is cached in shards.

[0017] Optionally, the cache shard includes a data key value hash table and a priority data key set hash table, wherein the data key value hash table stores cached data, and the key is the key name of the cached data, and the value is the cache data node, and the cache data node includes the data value of the cached data, the most recent access time and an access count factor; the priority data key set hash table stores the key name of the cached data of the corresponding priority, the key is the priority, and the value is a hash queue of the key name of the cached data of the corresponding priority.

[0018] According to another aspect of an embodiment of the present invention, a device for data caching is provided.

[0019] A data caching device comprises: a data query module, used to query data from a cache, the cache comprising a plurality of cache queues with different priorities; a data storage module, used to store the data to the tail node of the cache queue with the lowest priority if the data is not found; a data acquisition module, used to retrieve header data from the head node of the cache queue with the lowest priority and calculate the priority of the header data if the cache capacity exceeds a preset lower limit threshold; and a data deletion module, used to delete the header data from the cache if the priority of the header data is less than the lowest priority.

[0020] According to yet another aspect of an embodiment of the present invention, an electronic device for data caching is provided.

[0021] An electronic device for data caching 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 provided in an embodiment of the present invention.

[0022] According to still another aspect of the embodiments of the present invention, a computer readable medium is provided.

[0023] A computer readable medium stores a computer program, which, when executed by a processor, implements a data cache method provided by an embodiment of the present invention.

[0024] An embodiment of the above invention has the following advantages or beneficial effects: by querying data from the cache, the cache includes multiple cache queues with different priorities; if the data is not queried, the data is saved to the tail node of the cache queue with the lowest priority; if the cache capacity exceeds the preset lower limit threshold, the head data is taken from the head node of the cache queue with the lowest priority, and the priority of the head data is calculated; if the priority of the head data is less than the lowest priority, the head data is deleted from the cache, and the use of an adaptive access count factor to reflect the user's access to the cache data is realized, and the priority of the cache is calculated according to the access count factor to put the data into different priority queues, thereby realizing the dynamic adjustment of the capacity of the cache data priority queue according to the user's access to the cache data, and effectively improving the cache hit rate in the peak access period, valley period and other scenarios. In addition, the present invention also dynamically slices the cache data according to the number of single-machine CPUs, effectively solving the performance problem of long read and write operations caused by lock competition under high concurrency and high QPS in the prior art.

[0025] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with specific implementation examples. BRIEF DESCRIPTION OF THE DRAWINGS

[0026] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0027] Figure 1 is a schematic diagram of main steps of a method for data caching according to an embodiment of the present invention;

[0028] Figure 2 It is a schematic diagram of a data cache process when a cache miss occurs in an embodiment of the present invention;

[0029] Figure 3 is a schematic diagram of a data cache process when a cache hit occurs in another embodiment of the present invention;

[0030] Figure 4 It is a schematic diagram of the principle of cache data shard storage according to an embodiment of the present invention;

[0031] Figure 5 It is a flow chart of implementing cache data shard storage according to an embodiment of the present invention;

[0032] Figure 6 It is a schematic diagram of the data storage structure in the cache sharding according to an embodiment of the present invention;

[0033] Figure 7 is a schematic diagram of main modules of a data caching device according to an embodiment of the present invention;

[0034] Figure 8 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0035] Fig. 9 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0036] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0037] Among the existing cache strategies, FIFO is the simplest cache elimination algorithm, but it eliminates cached data in the order of entry, without considering the actual usage of cached data, resulting in a low hit rate. LRU only considers the most recent access time of cached data, but ignores the number of accesses to cached data. In the case of high-concurrency system access, it is easy to cause the cache to be filled with a large number of data objects that have been recently accessed but have a low number of accesses. LFU only considers the number of historical accesses to cached data, but ignores the time of access to cached data, which is easy to cause a large number of data objects in the cache that have a high number of historical accesses but have not been accessed for a long time. LRU-K, 2Q, and LIRS are all optimizations based on LRU. They are more complex to implement and have a certain improvement in cache hit rate, but they do not consider the performance problem of long read and write operations caused by lock competition in a single machine under high concurrency and high QPS. At the same time, for the situation where there are peak and trough access periods, the cache cannot dynamically and adaptively adjust within a certain range to improve the cache hit rate.

[0038] In order to solve the above-mentioned problems existing in the prior art, the present invention provides a cache method and device for dynamically slicing cache data according to the number of single-machine CPUs, and adaptively adjusting the capacity according to changes in data access volume, thereby solving the performance problem of long read and write operations caused by lock competition under high concurrency and high QPS (Queries-per-second) conditions in the prior art, and the problem that the cache cannot be dynamically and adaptively adjusted within a certain range to improve the cache hit rate when there are access peaks and troughs.

[0039] Figure 1 FIG. 1 is a schematic diagram of the main steps of the data caching method according to an embodiment of the present invention. Figure 1 As shown, the data caching method of the embodiment of the present invention mainly includes the following steps S101 to S104.

[0040] Step S101: querying data from a cache, where the cache includes a plurality of cache queues with different priorities;

[0041] Step S102: If no data is found, the data is saved to the tail node of the cache queue with the lowest priority;

[0042] Step S103: if the cache capacity exceeds the preset lower limit threshold, the header data is taken out from the head node of the cache queue with the lowest priority, and the priority of the header data is calculated;

[0043] Step S104: If the priority of the header data is lower than the lowest priority, the header data is deleted from the cache.

[0044] Normally, when cached data needs to be accessed or stored, the cache data node corresponding to the data is first queried from the cache according to the data key name. If there is a hit, it means that the data is not in the cache. In this case, empty is directly returned and the data is saved in the cache. If there is a hit, the data's most recent access time, access count factor, priority and other information are obtained from the cache data node, and then the data's latest access count factor is calculated, and the data's latest priority is determined, and then the data is placed in the cache queue corresponding to the latest priority.

[0045] According to one embodiment of the present invention, before the data is stored at the tail of the cache queue with the lowest priority, the method further includes:

[0046] Determine whether the cache capacity has reached a preset upper limit threshold;

[0047] If it is reached, the header data of the head node of the cache queue with the lowest priority will be deleted directly.

[0048] Generally, the cache will set an upper and lower threshold for capacity, such as 120% of the total available memory and 80% of the total available memory. By setting the upper and lower thresholds for cache capacity, data caching can be performed without affecting the normal operation of the system.

[0049] If the data is not found in the cache, the data needs to be newly saved in the cache. At this time, it is necessary to first determine whether the capacity of the current cache has reached the preset upper threshold. If it has, the data can no longer be cached. At this time, the head data of the head node of the lowest priority cache queue is directly deleted (according to the first-in-first-out characteristics of the queue, the data of the head node has not been operated for the longest time, so the head data of the head node needs to be deleted), and then the data can be saved in the cache. When caching the data, since the data is new to the cache, the access count factor and the most recent access time of the data can be set to the default initial value (for example: set the access count factor to 1, and the most recent access time to the current system time), and set the priority of the data to the lowest priority, and then save the data to the tail node of the lowest priority cache queue.

[0050] After the data is saved to the tail node of the lowest priority cache queue, it is also necessary to determine whether the cache capacity has exceeded the preset lower limit threshold. Since there may be multiple users operating the cache at the same time, a certain margin needs to be left in the cache each time. If the cache capacity exceeds the preset lower limit threshold, the header data is taken from the head node of the lowest priority cache queue and it is determined whether to delete the header data.

[0051] Specifically, the priority of the header data needs to be calculated. If its priority is less than the lowest priority, it needs to be deleted. When calculating the priority of the data, it is calculated based on the access count factor. By setting different ranges of access count factors to correspond to different priorities, the priority of the data can be calculated. For example: the access count factor value can be set to [128,1023] corresponding to high priority, and priority = 3, the access count factor value can be set to [16,127] corresponding to medium priority, and priority = 2, and the access count factor value can be set to [1,15] corresponding to low priority, and priority = 1.

[0052] Among them, the access count factor is a variable that changes with time, which is used to characterize the impact of the access time of the data in the cache on the cache data. Since the header data has not been hit, it is only necessary to calculate the latest access count factor based on the time difference between the current system time and the most recent access time of the data stored in the cache. The latest access count factor = access count factor - (system latest time - most recent access time of data record) / T, where T is the time factor variable. The value of T is an adaptive variable value. For example, when the value range of the access count factor is [1,15], the value of T is 1, when the value range of the access count factor is [16,127], the value of T is 4, and when the value range of the access count factor is [128,1023], the value of T is 8. In this way, the latest access count factor can be calculated and the latest priority of the header data can be determined. Since the initial priority of the header data is the lowest priority, if the calculated latest priority is less than the lowest priority, it means that the header data has not been accessed for a long time, so it can be cleared from the cache. Otherwise, if the latest calculated priority is still the lowest priority, it means that the data usage rate in the cache is high and there is no need to update the cache.

[0053] Figure 2 FIG. 1 is a schematic diagram of a data cache process when a cache miss occurs in an embodiment of the present invention. Figure 2 As shown, when storing or reading data in the cache, if the data does not exist in the cache, it means that the cache misses. At this time, if the cache data capacity reaches the upper limit threshold, the head data of the head node of the lowest priority set queue is directly eliminated. If the cache capacity is sufficient, the data node of the data (including data key name, data value, most recent access time, access count factor, etc.) is stored in the data table in the cache, and the most recent access time is set to the current time, the access count factor is set to the initial value 1, and the priority is set to the lowest priority 1. The data node key is stored at the end of the lowest priority cache queue. The cache capacity increases adaptively.

[0054] Then determine whether the cache capacity is greater than the set lower limit threshold. If it is, obtain the head data of the head node of the current lowest priority cache queue. Since the cache does not hit, only the latest access count factor and its corresponding priority are calculated based on the time difference, and determine whether to eliminate the head data. If the count factor is <1 or the priority is less than the lowest priority, eliminate the head data of this head node, and the cache capacity is adaptively reduced. Among them, if the cache capacity is ≤ the lower limit threshold, no processing is required.

[0055] According to another embodiment of the present invention, if data is queried from the cache, the priority of the data is calculated and the data is moved to the tail node of the cache queue of the priority. If data is queried from the cache, it means that the data has been hit, and the access count factor of the data and the priority of the data also need to be updated. Among them, if data is queried from the cache, the latest access count factor is calculated based on the access count factor of the data stored in the cache and the most recent access time; the priority of the data is determined based on the latest access count factor.

[0056] If data is retrieved from the cache, the latest access count factor is calculated based on the access count factor of the data stored in the cache and the most recent access time, including:

[0057] If data is retrieved from the cache, the impact of the cache hit on the access count factor of the data is first calculated, that is, the probability of the access count factor increasing P = 1 / (access count factor * ln access count factor + 1), and then the probability of the access count factor increasing P is compared with the random number R. If P is greater than R, the access count factor is increased by 1, where R∈[0,1); wherein the random number is generated by, for example, Java's Math.random();

[0058] Afterwards, the access count factor of the data is updated;

[0059] Then, calculate the impact of the time difference on the access count factor of the data, and calculate the latest access count factor based on the time difference between the current system time and the last access time of the data saved in the cache. The latest access count factor = access count factor - (system latest time - last access time of data record) / T, where T is the time factor variable.

[0060] Since when data is queried from the cache, that is, a cache hit, there is no need to add new data to the cache at this time, so there is no need to determine whether the cache capacity exceeds the set upper threshold or lower threshold. However, according to another embodiment of the present invention, in order to make the cache hit rate as high as possible while ensuring the storage capacity, even if the cache hits, after saving the data to the cache queue of the corresponding priority, it is possible to further determine whether the cache capacity exceeds the set lower threshold, and when the cache capacity exceeds the set lower threshold, obtain the header data of the head node of the cache queue of the corresponding priority of the data, and calculate the priority of the header data to determine whether to downgrade the header data or delete it from the cache.

[0061] Figure 3 FIG. 1 is a schematic diagram of a data cache process when a cache hit occurs in another embodiment of the present invention. Figure 3As shown in the figure, when storing or reading data in the cache, if the data exists in the cache, it means that the cache hits. At this time, first obtain the latest access time, access count factor, priority and other information of the data from the cache data node, and then calculate the latest access count factor according to the adaptive calculation rule, and determine the latest priority according to the latest access count factor, so as to put the data into the tail node of the cache queue of the latest priority.

[0062] The adaptive calculation rules are as follows: The factors affecting the access count factor include two parts. One part is the impact of each access hit of the cached data on the increasing trend of the access count factor. The other part is the impact of the time difference between the most recent access time of the cached data and the current system time on the decreasing trend of the access count factor.

[0063] First, calculate the impact of access hits on the access count factor. The probability of access count factor increase = 1 / (access count factor * ln access count factor + 1). As the access count factor value increases, the probability of access count factor increase adaptively decreases. At the same time, calculate a random number [0,1), compare the probability of access count factor increase with the value of the random number, and if the probability of access count factor increase is greater than the random value, the access count factor value + 1. Then, update the access count factor according to the execution result of this step.

[0064] Then calculate the influence of the time difference on the access count factor, the unit of time is, for example, seconds, set a time factor variable T, the value of T is an adaptive variable value, when the value range of the access count factor is [1,15], the value of T is 1, when the value range of the access count factor is [16,127], the value of T is 4, when the value range of the access count factor is [128,1023], the value of T is 8. The latest access count factor = access count factor - (system latest time - most recent access time) / T, where the access count factor here is the access count factor updated according to the previous step, and the most recent access time is the most recent access time recorded in the cache.

[0065] The priority corresponding to the data is calculated according to the access counting factor. The value of the access counting factor is [128,1023] corresponding to high priority, priority = 3, the value of the access counting factor is [16,127] corresponding to medium priority, priority = 2, and the value of the access counting factor is [1,15] corresponding to low priority, priority = 1.

[0066] If the priority of the calculated data remains unchanged, the current data is moved to the end of the corresponding priority queue, and the most recent access time and access count factor of the current data node are updated. In this embodiment, after the current data is moved, it is also determined whether the cache capacity is greater than the lower limit threshold, and when the cache capacity is greater than the lower limit threshold, the header data of the head node of the current priority cache queue is obtained, and the most recent access time, access count factor and other information of the header data of the head key node are obtained. Since the header data is not hit, only the latest access count factor of the header data is calculated based on the time difference, and the latest priority is calculated. If the priority is reduced, it is removed from the current cache queue and added to the end of the cache queue with the reduced priority. If the priority is less than the lowest priority 1, the header data is directly eliminated, and the cache capacity is adaptively reduced.

[0067] If the priority of the data is increased after calculation, the current data is moved to the end of the queue with the increased priority, and the latest access time, access count factor, and priority of the current data node are updated. Then, when the cache capacity is greater than the lower limit threshold, the head data of the head node of the cache queue with the current priority (i.e., the increased priority) is taken, and the latest access time and access count factor of the head data of the head node are obtained. The latest access count factor is calculated only based on the time difference, and the latest priority is calculated. If the priority is reduced, the current cache queue is removed and added to the end of the cache queue with the reduced priority. If the priority is less than the lowest priority 1, the head data is directly eliminated and the cache capacity is adaptively reduced.

[0068] If the priority of the data is reduced after calculation, the current data is moved to the end of the queue with the reduced priority, and the latest access time, access count factor, and priority of the current data node are updated. Then, when the cache capacity is greater than the lower limit threshold, the head data of the head node of the cache queue with the current priority (i.e., the reduced priority) is taken, and the latest access time and access count factor of the head data of the head node are obtained. The latest access count factor is calculated only based on the time difference, and the latest priority is calculated. If the priority is reduced, the current cache queue is removed and added to the end of the cache queue with the reduced priority. If the priority is less than the lowest priority 1, the head data is directly eliminated, and the cache capacity is adaptively reduced.

[0069] According to another embodiment of the present invention, the cache includes a plurality of cache slices, and the data is cached in slices, wherein each cache slice caches the data according to the cache method described above.

[0070] According to another embodiment of the present invention, the cache shard includes, for example, a data key value hash table and a priority data key set hash table, wherein:

[0071] The data key-value hash table stores cache data, and the key is the key name of the cache data, and the value is the cache data node. The cache data node includes the data value of the cache data, the most recent access time, and the access count factor;

[0072] The priority data key set hash table stores the key names of the cache data of the corresponding priority, where the key is the priority and the value is the hash queue of the key names of the cache data of the corresponding priority.

[0073] The embodiments of the present invention provide a cache method and device for dynamically slicing cache data according to the number of CPUs of a single machine and adaptively adjusting the capacity according to changes in data access volume to improve the cache hit rate.

[0074] First, dynamically obtain the number of CPUs N of the current machine. By default, the number of shards is consistent with the number of CPUs (it can be inconsistent as long as they are positively linearly correlated). The cache is divided into N pieces, and the full cache data is processed in pieces. For the key name of the cache data, CRC32(key)%N is used to determine which cache shard to route the data to. Sharding can effectively increase the parallelism of cache read and write operations and reduce the time spent on synchronization waiting.

[0075] For each cache shard, for example, it is mainly composed of two parts: a data key value hash table and a priority data key set hash table. The data key value hash table stores cached data, where the key is the key of the cached data and the value is the cached data node. The cached data node content includes the most recent access time, access count factor, priority, data value, etc. The most recent access time is accurate to seconds, the access count factor ranges from 0 to 1023, and the priority ranges from 0 to 3. The priority data key set hash table stores key sets of cached data with high, medium, and low priorities. The key is the priority, and the value is a chained hash ordered set of cached data keys of the corresponding priority (for example: hash queue). The chained hash ordered set is divided into three ordered sets according to priority: a high priority ordered set corresponding to a priority of 3, a medium priority ordered set corresponding to a priority of 2, and a low priority ordered set corresponding to a priority of 1.

[0076] When accessing and storing cached data, the latest access count factor is calculated based on the cached data's most recent access time and whether it is a hit. Based on the value of the access count factor, the data key is placed at the end of the key set queue of the corresponding priority, and the access count factor of the head node is calculated to adaptively determine whether to eliminate cached data with low priority. When the data access volume is large, the cache parameters can be adaptively and dynamically adjusted to expand the capacity. When the data access volume is small, the cache parameters can be adaptively and dynamically adjusted to reduce the capacity, thereby effectively improving the cache hit rate.

[0077] For a single-machine cache, there is a lock operation before each read and write operation, and an unlock operation after the read and write operation is completed. Under low QPS (query rate per second), the time consumed by lock contention can be basically ignored, but under high QPS, a lot of time is consumed in waiting for lock operations, resulting in increased time consumption. For the situation where a large number of synchronous waiting operations lead to increased time consumption, the solution is to minimize the critical area. By introducing an efficient hash algorithm mechanism, the full amount of data is sharded, and the original cache is divided into multiple cache shards to increase concurrency and reduce query and write time.

[0078] 1) Shard routing rules

[0079] The key of the cached data is sharded using the CRC32 algorithm, and the system is divided into N cache shards. For each key, the cache shard to which it belongs is determined by the following formula: CacheId = CRC32 (key) % N. When accessing cached data, first obtain the shard where the data may exist according to the corresponding routing rules, and then query the data on the corresponding shard. When storing cached data, first obtain the shard corresponding to the data according to the shard routing rules, and then store the data on the corresponding shard. Sharding increases the parallelism of cache read and write operations and reduces the time spent on synchronization waiting.

[0080] Figure 4 FIG. 1 is a schematic diagram of the principle of sharding cache data storage in an embodiment of the present invention. Figure 4 As shown, the corresponding cache slice is obtained by calculating the key name of each cache data through CRC32(key)%N, and then the data is stored in the cache slice. Similarly, when it is necessary to find data from the cache, the same method is used.

[0081] 2) Determine the number of shards

[0082] One way to improve cache performance is to determine the most reasonable number of shards, increase sufficient parallelism, and reduce synchronization waiting consumption. Therefore, the number of shards can be consistent with the number of CPUs. By using language functions (for example, you can use Java language Runtime.getRuntime().availableProcessors() to get the number of CPUs), you can dynamically get the number of available CPU processors. The default setting is that the number of shards N = the number of CPUs.

[0083] Figure 5 FIG. 1 is a flowchart of implementing the cache data sharding storage of an embodiment of the present invention. Figure 5As shown, first dynamically obtain the number of CPUs and set the number of cache shards N = the number of CPUs; then, perform shard routing according to the key name key of the cache data to cache the cache data shard to the corresponding cache shard CacheId = CRC32(key)% N; thereafter, read and write operations of the cache data can be performed in the corresponding cache shard.

[0084] 3) Cache adaptive dynamic adjustment method

[0085] For each cache shard, an upper and lower capacity threshold is set, and the capacity can be adaptively changed within a certain range to cope with changes in access volume. The cache shard includes multiple priority queues (taking three priority queues as an example), and each data node calculates the priority based on historical records and puts it into the corresponding priority queue. Within the capacity threshold range, according to changes in data access volume, the capacity of each priority queue is adaptively adjusted according to priority, thereby improving the cache hit rate.

[0086] The cache shard consists of two parts: the data key value hash table and the priority data key set hash table.

[0087] The data key-value hash table stores cache data, with the key being the key name of the cache data and the value being the cache data node. The cache data node content includes the cache data's most recent access time, access count factor, priority, data value, etc. The most recent access time is accurate to seconds, the access count factor ranges from 0 to 1023, and the priority ranges from 0 to 3 (where 3 is the highest priority and 1 is the lowest priority. When the priority is less than the lowest priority, the priority is assigned a value of 0).

[0088] The priority data key set hash table only stores the key name key of the cache data of the corresponding priority. The key is the priority, and the value is the chained hash ordered set of the cache data key of the corresponding priority (for example: hash queue). The chained hash ordered set is divided into three ordered sets according to priority: a high priority ordered set corresponding to priority 3, a medium priority ordered set corresponding to priority 2, and a low priority ordered set corresponding to priority 1.

[0089] Figure 6 Schematic diagram of data storage structure in cache shards in an embodiment of the present invention. Figure 6 As shown, the data in the cache shard of the embodiment of the present invention mainly includes a data key value hash table and a priority data key set hash table. Among them, each data node in the data key value hash table stores the most recent access time, access count factor, priority, data value, etc. of the cached data; the priority data key set hash table mainly stores a priority ordered hash set (for example: the priority hash queue in the embodiment of the present invention), which may include a high priority hash queue, a medium priority hash queue, and a low priority hash queue.

[0090] For each cache slice, the process of performing cache data read and write operations thereon can be implemented according to steps S101 to S104 in the aforementioned embodiment.

[0091] Figure 7 Schematic diagram of the main modules of the data cache device according to an embodiment of the present invention. Figure 7 As shown, the data caching device 700 according to the embodiment of the present invention mainly includes a data query module 701 , a data storage module 702 , a data acquisition module 703 and a data deletion module 704 .

[0092] A data query module 701 is used to query data from a cache, wherein the cache includes a plurality of cache queues with different priorities;

[0093] The data saving module 702 is used to save the data to the tail node of the cache queue with the lowest priority if the data is not found;

[0094] The data acquisition module 703 is used to fetch the header data from the head node of the cache queue with the lowest priority and calculate the priority of the header data if the cache capacity exceeds a preset lower limit threshold;

[0095] The data deletion module 704 is configured to delete the header data from the cache if the priority of the header data is lower than the lowest priority.

[0096] According to an embodiment of the present invention, the data caching device 700 may further include a cache determination module (not shown in the figure) for:

[0097] Before storing the data at the end of the cache queue with the lowest priority, determining whether the cache capacity reaches a preset upper threshold;

[0098] If it is reached, the header data of the head node of the cache queue with the lowest priority is directly deleted.

[0099] According to another embodiment of the present invention, if the data is found in the cache, the priority of the data is calculated, and the data is moved to the tail node of the cache queue of the priority.

[0100] According to another embodiment of the present invention, the cache also stores the access count factor and the most recent access time of the data; and the priority of the data is determined based on the access count factor of the data.

[0101] According to yet another embodiment of the present invention, if the data is not found, the access count factor and the most recent access time of the data are set to default initial values.

[0102] According to an embodiment of the present invention, if the data is queried from the cache, the data acquisition module 703 may also be used to:

[0103] If the data is found in the cache, the latest access count factor is calculated according to the access count factor of the data stored in the cache and the most recent access time;

[0104] The priority of the data is determined according to the latest access count factor.

[0105] According to an embodiment of the present invention, if the data is queried from the cache, the data acquisition module 703 may also be used to calculate the latest access count factor according to the access count factor and the most recent access time of the data stored in the cache:

[0106] If the data is retrieved from the cache, first calculate the probability P of the access count factor increasing = 1 / (access count factor * ln access count factor + 1), then compare the probability P of the access count factor increasing with the random number R, if P is greater than R, then increase the access count factor by 1, where R∈[0,1);

[0107] Updating the access count factor;

[0108] Then, the latest access count factor is calculated based on the time difference between the current system time and the most recent access time of the data. The latest access count factor = access count factor - (system latest time - most recent access time of data record) / T, where T is the time factor variable.

[0109] According to another embodiment of the present invention, when calculating the priority of the header data, the data acquisition module 703 may also be used to:

[0110] The latest access count factor is calculated based on the time difference between the current system time and the most recent access time of the data. The latest access count factor = access count factor - (system latest time - most recent access time of data record) / T, where T is the time factor variable.

[0111] According to yet another embodiment of the present invention, the cache includes a plurality of cache slices, and data is cached in slices.

[0112] According to another embodiment of the present invention, the cache shard includes a data key value hash table and a priority data key set hash table, wherein:

[0113] The data key value hash table stores cache data, and the key is the key name of the cache data, and the value is the cache data node, and the cache data node includes the data value of the cache data, the most recent access time and the access count factor;

[0114] The priority data key set hash table stores the key names of the cache data of the corresponding priority, where the key is the priority and the value is a hash queue of the key names of the cache data of the corresponding priority.

[0115] According to the technical solution of the embodiment of the present invention, by querying data from the cache, the cache includes multiple cache queues with different priorities; if the data is not queried, the data is saved to the tail node of the cache queue with the lowest priority; if the cache capacity exceeds the preset lower limit threshold, the head data is taken from the head node of the cache queue with the lowest priority, and the priority of the head data is calculated; if the priority of the head data is less than the lowest priority, the head data is deleted from the cache, and the use of an adaptive access count factor to reflect the user's access to the cache data is realized, and the priority of the cache is calculated according to the access count factor to put the data into different priority queues, thereby realizing the dynamic adjustment of the capacity of the cache data priority queue according to the user's access to the cache data, and effectively improving the cache hit rate in the peak access period, valley period and other scenarios. In addition, the present invention also dynamically shards the cache data according to the number of single-machine CPUs, effectively solving the performance problem of long read and write operations caused by lock competition under high concurrency and high QPS in the prior art.

[0116] Figure 8 An exemplary system architecture 800 is shown to which the method for data caching or the apparatus for data caching according to the embodiments of the present invention may be applied.

[0117] like Figure 8 As shown, system architecture 800 may include terminal devices 801, 802, 803, a network 804, and a server 805. Network 804 is used to provide a medium for communication links between terminal devices 801, 802, 803 and server 805. Network 804 may include various connection types, such as wired, wireless communication links, or optical fiber cables, etc.

[0118] Users can use terminal devices 801, 802, and 803 to interact with server 805 through network 804 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 801, 802, and 803, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only examples).

[0119] The terminal devices 801 , 802 , and 803 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, and the like.

[0120] The server 805 may be a server that provides various services, such as a backend management server (only an example) that provides support for shopping websites browsed by users using the terminal devices 801, 802, and 803. The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results (such as target push information, product information - only an example) to the terminal device.

[0121] It should be noted that the data caching method provided in the embodiment of the present invention is generally executed by the server 805 , and accordingly, the data caching device is generally set in the server 805 .

[0122] It should be understood that Figure 8 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to the implementation requirements.

[0123] Reference below Fig. 9 , which shows a schematic diagram of the structure of a computer system 900 of a terminal device or server suitable for implementing an embodiment of the present invention. Fig. 9 The terminal device or server shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0124] like Fig. 9 As shown, the computer system 900 includes a central processing unit (CPU) 901, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 902 or a program loaded from a storage part 908 into a random access memory (RAM) 903. In the RAM 903, various programs and data required for the operation of the system 900 are also stored. The CPU 901, the ROM 902, and the RAM 903 are connected to each other via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.

[0125] The following components are connected to the I / O interface 905: an input section 906 including a keyboard, a mouse, etc.; an output section 907 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 908 including a hard disk, etc.; and a communication section 909 including a network interface card such as a LAN card, a modem, etc. The communication section 909 performs communication processing via a network such as the Internet. A drive 910 is also connected to the I / O interface 905 as needed. A removable medium 911, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 910 as needed, so that a computer program read therefrom is installed into the storage section 908 as needed.

[0126] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 909, and / or installed from the removable medium 911. When the computer program is executed by the central processing unit (CPU) 901, the above-mentioned functions defined in the system of the present invention are executed.

[0127] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0128] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0129] The units or modules involved in the embodiments of the present invention may be implemented in software or hardware. The units or modules described may also be provided in a processor. For example, they may be described as follows: a processor includes a data query module, a data storage module, a data acquisition module, and a data deletion module. The names of these units or modules do not, in some cases, constitute limitations on the units or modules themselves. For example, the data query module may also be described as a "module for querying data from a cache."

[0130] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: querying data from a cache, the cache includes multiple cache queues with different priorities; if the data is not queried, the data is saved to the tail node of the cache queue with the lowest priority; if the cache capacity exceeds the preset lower limit threshold, the header data is taken from the head node of the cache queue with the lowest priority, and the priority of the header data is calculated; if the priority of the header data is less than the lowest priority, the header data is deleted from the cache.

[0131] According to the technical solution of the embodiment of the present invention, by querying data from the cache, the cache includes multiple cache queues with different priorities; if the data is not queried, the data is saved to the tail node of the cache queue with the lowest priority; if the cache capacity exceeds the preset lower limit threshold, the head data is taken from the head node of the cache queue with the lowest priority, and the priority of the head data is calculated; if the priority of the head data is less than the lowest priority, the head data is deleted from the cache, and the use of an adaptive access count factor to reflect the user's access to the cache data is realized, and the priority of the cache is calculated according to the access count factor to put the data into different priority queues, thereby realizing the dynamic adjustment of the capacity of the cache data priority queue according to the user's access to the cache data, and effectively improving the cache hit rate in the peak access period, valley period and other scenarios. In addition, the present invention also dynamically shards the cache data according to the number of single-machine CPUs, effectively solving the performance problem of long read and write operations caused by lock competition under high concurrency and high QPS in the prior art.

[0132] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A method for data caching, characterized in that: include: Querying data from a cache, wherein the cache includes a plurality of cache queues with different priorities; If the data is not found, the data is saved to the tail node of the cache queue with the lowest priority; If the cache capacity exceeds the preset lower limit threshold, the header data is taken out from the head node of the cache queue with the lowest priority, and the priority of the header data is calculated; If the priority of the header data is lower than the lowest priority, deleting the header data from the cache; The cache also stores the access count factor and the most recent access time of the data; and the priority of the data is determined according to the access count factor of the data; If the data is found in the cache, the latest access count factor is calculated according to the access count factor and the most recent access time of the data stored in the cache; the priority of the data is determined according to the latest access count factor, and the data is moved to the tail node of the cache queue of the priority; Among them, if the data is queried from the cache, the latest access count factor is calculated according to the access count factor and the most recent access time of the data stored in the cache, including: if the data is queried from the cache, first calculate the probability of increase of the access count factor P = 1 / (access count factor*lnaccess count factor+1), then compare the probability of increase of the access count factor P with a random number R, if P is greater than R, then increase the access count factor by 1, where R∈[0,1); update the access count factor; then calculate the latest access count factor according to the time difference between the current system time and the most recent access time of the data, the latest access count factor = access count factor-(system latest time-most recent access time of the data record) / T, where T is the time factor variable.

2. The method according to claim 1, characterized in that Before storing the data at the tail of the cache queue with the lowest priority, the method further includes: Determining whether the cache capacity reaches a preset upper limit threshold; If it is reached, the header data of the head node of the cache queue with the lowest priority is directly deleted.

3. The method according to claim 1, characterized in that If the data is not found, the access count factor and the most recent access time of the data are set to default initial values.

4. The method according to claim 1, characterized in that Calculating the priority of the header data includes: The latest access count factor is calculated based on the time difference between the current system time and the most recent access time of the data. The latest access count factor = access count factor - (system latest time - most recent access time of data record) / T, where T is the time factor variable.

5. The method according to claim 1, characterized in that The cache includes multiple cache slices, and caches data in slices.

6. The method according to claim 5, characterized in that The cache shard includes a data key value hash table and a priority data key set hash table, wherein: The data key value hash table stores cache data, and the key is the key name of the cache data, and the value is the cache data node, and the cache data node includes the data value of the cache data, the most recent access time and the access count factor; The priority data key set hash table stores the key names of the cache data of the corresponding priority, where the key is the priority and the value is a hash queue of the key names of the cache data of the corresponding priority.

7. A data caching device, characterized in that: include: A data query module, used to query data from a cache, wherein the cache includes a plurality of cache queues with different priorities; A data saving module, used for saving the data to the tail node of the cache queue with the lowest priority if the data is not found; A data acquisition module, configured to fetch the header data from the head node of the cache queue with the lowest priority and calculate the priority of the header data if the cache capacity exceeds a preset lower threshold; a data deleting module, configured to delete the header data from the cache if the priority of the header data is lower than the lowest priority; The cache also stores the access count factor and the most recent access time of the data; and the priority of the data is determined according to the access count factor of the data; If the data is found in the cache, the latest access count factor is calculated according to the access count factor and the most recent access time of the data stored in the cache; the priority of the data is determined according to the latest access count factor, and the data is moved to the tail node of the cache queue of the priority; Among them, if the data is queried from the cache, the latest access count factor is calculated according to the access count factor and the most recent access time of the data stored in the cache, including: if the data is queried from the cache, first calculate the probability of increase of the access count factor P = 1 / (access count factor*lnaccess count factor+1), then compare the probability of increase of the access count factor P with a random number R, if P is greater than R, then increase the access count factor by 1, where R∈[0,1); update the access count factor; then calculate the latest access count factor according to the time difference between the current system time and the most recent access time of the data, the latest access count factor = access count factor-(system latest time-most recent access time of the data record) / T, where T is the time factor variable.

8. An electronic device for data caching, characterized in that: include: 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 method according to any one of claims 1 to 6.

9. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Metadata read buffering method, device and apparatus

    CN109359062A

  • Metadata cache management method and device

    CN110232049A