A method and apparatus for data caching

By employing a data storage structure with preset time precision and synchronization lock control in the cached data, the orderly updating and eviction of cached data is achieved, solving the problems of low cache hit rate and thread blocking in existing technologies, and improving the server's processing capacity.

CN116088862BActive Publication Date: 2026-01-23TUS CLOUD CONTROL (BEIJING) TECH LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310193419.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-02-23
Publication Date
2026-01-23
Estimated Expiration
2043-02-23

AI Technical Summary

Technical Problem

Existing cache eviction mechanisms, such as FIFO, LRU, and LFU algorithms, result in low cache hit rates, are unable to effectively handle periodic or sporadic data access, and suffer from thread blocking issues, affecting server throughput.

Method used

A data storage structure based on preset time precision and synchronization lock control are adopted. The cache data structure is established by preprocessing the data to be cached to establish the same cache data structure as the preset data storage structure. The cache data is replaced and evicted under the same time precision. The partition number-Map-Map data structure is used to perform ordered updates and evictions of cache data.

Benefits of technology

It improves the hit rate of cached data reads, avoids thread blocking, and increases server throughput and cached data update efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116088862B_ABST
    Figure CN116088862B_ABST
Patent Text Reader

Abstract

The embodiment of the specification discloses a method for data caching, adopts preset time precision, sequentially establishes a first cache data structure and a second cache data structure which have the same structure as a preset data storage structure, and replaces the first cache data structure stored in a first storage medium with the second cache data structure according to the preset time precision, and stores the replaced first cache data structure into a second storage medium; the algorithm logic for storing cache data of different time periods in the same time precision, the same data storage structure and the partition is established to perform update and elimination on the cache data, more ordered update and elimination on the cache data are realized, and the hit rate during reading of the cache data is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer information processing technology, and specifically to a method and apparatus for data caching. Background Technology

[0002] Caching primarily aims to improve data retrieval speed. Software caching refers to writing data into memory containers (list, map, set, etc.) for storage. Existing memory cache eviction mechanisms, such as the FIFO (First In, First Out) algorithm, can be understood as a queue-like algorithm. The first piece of data to arrive is considered to have the lowest probability of being accessed in the future. Therefore, when the allocated space is exhausted and new data needs to be added, the earliest arriving data is evicted first. This algorithm logic lacks any additional logic for orderly updating and eviction of cached data, resulting in a low hit rate.

[0003] Therefore, it is necessary to establish an advanced algorithm to update and evict cached data, thereby improving the hit rate of cached data reading. Summary of the Invention

[0004] This specification provides a method and apparatus for data caching, which is used to update and evict cached data, thereby improving the hit rate of cached data reading.

[0005] The embodiments in this specification adopt the following technical solutions:

[0006] This specification provides a method for data caching, including:

[0007] Based on a preset time precision, the first data to be cached is preprocessed according to a preset data storage structure to obtain a first cache data structure that is identical to the structure represented by the preset data storage structure; the preset data storage structure includes a first structure for storing object data types of data to be cached and a second structure for storing object data of data to be cached.

[0008] The first cache data structure is stored in the first storage medium;

[0009] Get the second set of data to be cached;

[0010] Based on the preset time precision, the second data to be cached is preprocessed according to the preset data storage structure to obtain a second cached data structure that is the same as the structure represented by the preset data storage structure.

[0011] Replace the first cache data structure in the first storage medium with the second cache data structure;

[0012] The replaced first cache data structure is stored in the second storage medium.

[0013] Preferably, the data caching method provided in this specification may further include:

[0014] Receive a cached data query request; the cached data query request contains data parameters of the data to be queried; the data parameters include the object data type and / or object key-value pairs of the data.

[0015] Determine whether the data parameters exist in the second storage medium;

[0016] If it exists, the cache data structure of the second storage medium is traversed according to the data parameters to obtain the query result.

[0017] This specification also provides an apparatus for data caching, comprising:

[0018] The first acquisition module is used to acquire the first data to be cached.

[0019] The first preprocessing module is used to preprocess the first data to be cached based on a preset time precision and a preset data storage structure to obtain a first cache data structure that is the same as the structure represented by the preset data storage structure; the preset data storage structure includes a first structure for storing object data types of the data to be cached and a second structure for storing object data of the data to be cached.

[0020] The first storage module is used to store the first cache data structure in the first storage medium;

[0021] The second acquisition module is used to acquire the second data to be cached.

[0022] The second preprocessing module is used to preprocess the second data to be cached based on the preset time precision and the preset data storage structure to obtain a second cached data structure that is the same as the structure represented by the preset data storage structure.

[0023] A replacement module is used to replace the first cache data structure in the first storage medium with the second cache data structure;

[0024] The second storage module is used to store the replaced first cache data structure in the second storage medium.

[0025] The above-described at least one technical solution used in the embodiments of this specification can achieve the following beneficial effects:

[0026] By acquiring first data to be cached, preprocessing the first data to be cached according to a preset time precision and a preset data storage structure, a first cache data structure identical to the structure represented by the preset data storage structure is obtained and stored in a first storage medium; acquiring second data to be cached, preprocessing the second data to be cached according to the same preset time precision and the preset data storage structure, a second cache data structure identical to the structure represented by the preset data storage structure is obtained, and replacing the first cache data structure in the first storage medium with the second cache data structure; storing the replaced first cache data structure in the second storage medium; this application, based on the above method, uses a preset time precision to sequentially establish a first cache data structure and a second cache data structure identical to the structure represented by the preset data storage structure, and replaces the first cache data structure stored in the first storage medium with the second cache data structure according to the preset time precision, storing the replaced first cache data structure in the second storage medium; an algorithm logic is established to update and evict cache data with the same time precision, the same data storage structure, and partitioned storage of cache data for different time periods, achieving a more orderly update and evictment of cache data and improving the hit rate when reading cache data. Attached Figure Description

[0027] To more clearly illustrate the technical solutions in the embodiments or prior art of this specification, the drawings used in the description of the embodiments or prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in this specification. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0028] Figure 1 This is a schematic diagram of a data caching method provided in the embodiments of this specification.

[0029] Figure 2 This is a schematic diagram of an update buffer data storage structure provided in the embodiments of this specification.

[0030] Figure 3 This is a schematic diagram of a time wheel data storage method with time precision that exists in the existing technology.

[0031] Figure 4 This is a schematic diagram of another time wheel data storage method with different time precision existing in the technology.

[0032] Figure 5 This is a schematic diagram of a time wheel data storage method with time precision provided in the embodiments of this specification.

[0033] Figure 6This is a schematic diagram of a data storage structure provided in the embodiments of this specification.

[0034] Figure 7 This is a flowchart of a method for caching data according to a preset type, provided in an embodiment of this specification.

[0035] Figure 8 This is another flowchart illustrating a data caching method provided in the embodiments of this specification.

[0036] Figure 9 This is a schematic diagram of a data caching device provided in an embodiment of this specification.

[0037] Figure 10 This is another schematic diagram of a data caching device provided in the embodiments of this specification. Detailed Implementation

[0038] To make the objectives, technical solutions, and advantages of one or more embodiments of this specification clearer, the technical solutions of one or more embodiments of this specification will be clearly and completely described below in conjunction with specific embodiments and corresponding drawings. Obviously, the described embodiments are merely some embodiments of this application, and not all embodiments. Based on the embodiments of this specification, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of this application.

[0039] It should be noted that all uses of "first" and "second" in the embodiments of the present invention are for the purpose of distinguishing two entities or parameters with the same name but different names. "First" and "second" are only for the convenience of expression and should not be construed as limiting the embodiments of the present invention. Subsequent embodiments will not explain this in detail.

[0040] In the field of computer information processing, the relevant concepts of locks, thread blocking, QPS and concurrency, and time wheels used in information processing are introduced below:

[0041] Locks are mechanisms used in computers to coordinate concurrent access to a resource by multiple processes or threads. Thread synchronization aims to protect a resource from damage caused by multiple threads (processes) accessing it, and this is achieved through locks. For example, with an exclusive lock, when one thread acquires the lock to access a resource, other threads will not be able to acquire the lock. Threads that fail to acquire the lock will wait; this waiting is called thread blocking.

[0042] QPS stands for Queries Per Second, which represents the number of queries a server performs per second. The result clearly shows the query processing volume of a computer server and determines its query rate per second. The number of response requests per second is the server's maximum throughput.

[0043] Concurrency refers to a large number of user requests accessing the system within a certain period of time. The concurrency level (concurrency degree) refers to the number of requests that the system can handle at the same time, and also reflects the system's load capacity.

[0044] In simple terms, a time wheel is a circular queue (usually implemented using an array), where each element (time slot) can store a list of scheduled tasks.

[0045] Data storage refers to temporary files generated during the processing of data streams or information that needs to be retrieved during processing. Data is recorded in a certain format on internal or external storage media of a computer. Data streams reflect the flowing data in the system and exhibit the characteristics of dynamic data; data storage reflects the static data in the system and exhibits the characteristics of static data.

[0046] Common storage media include hard drives, magnetic disks, and magnetic tapes. The data storage organization method varies depending on the storage medium. On magnetic tapes, data is accessed only in a sequential file manner; on magnetic disks, sequential access or direct access methods can be used depending on the usage requirements. The key to data storage methods lies in establishing the correspondence between the logical and physical order of records and determining the storage address to improve data access speed.

[0047] The technical solutions provided in the various embodiments of this specification are described in detail below with reference to the accompanying drawings.

[0048] In the existing technology, the FIFO (First In, First Out) algorithm is the simplest and fairest data eviction algorithm. Its logic is simple and clear and easy to implement. However, the hit rate of the data implemented by this algorithm is relatively low. That is to say, the data that comes in first will be eliminated first. There is no additional logic to extend the time that cached data stays in memory, so the hit rate is low.

[0049] In existing technologies, there is also a memory cache eviction mechanism called LRU (Least Recently Used). Although this algorithm is more intelligent than the FIFO algorithm, it is difficult to deal with periodic or sporadic access data. That is, when the access frequency of periodic or sporadic data suddenly increases, this algorithm identifies them as hot data and evicts the truly hot data, resulting in the problem of not being able to hit the real hot data; therefore, the hit rate is relatively low.

[0050] Another existing memory cache eviction mechanism, LFU (Least Frequently Used), is rarely used recently and suffers from similar problems. The difference between LFU and LRU is that LRU measures access frequency by time, while LFU measures it by the number of times accessed within a time period. If data is accessed infrequently within a certain period, it is considered to have the lowest probability of being accessed in the future. When the allocated space is exhausted and new data needs to be added, the data with the lowest access frequency within the time period will be evicted first. However, because LFU needs to record the access frequency of data, it requires additional space. If the access pattern changes, the data hit rate will drop sharply; therefore, the data hit rate is relatively low.

[0051] To address the shortcomings of existing technologies, this solution provides the following embodiments to achieve more orderly updates and evictions of cached data, thereby improving the hit rate when reading cached data.

[0052] Figure 1 This is a schematic flowchart illustrating a data caching method provided in an embodiment of this specification. From an algorithmic perspective, the execution entity of this process can be a server, computer, tablet computer, or other terminal device. Figure 1 As shown, the process may include the following steps:

[0053] Step 110: Obtain the first data to be cached.

[0054] In the embodiments of this specification, the first data to be cached can be a single data item or a data set consisting of data to be cached within a preset time period, such as a set of cached data within 1 second; it can include cached data of one data type or multiple different types of cached data, such as perception data and event data; the cached data can be streaming data or non-streaming data, and no specific limitation is made in this invention.

[0055] Furthermore, the first or second data to be cached mentioned in this invention is only for distinguishing data acquired at different times, and does not differentiate the attributes of the data itself. That is, the data type or data volume of the first and second data to be cached can be the same. Among them, the data attributes (data parameters) include Timestamp, type (object data type), key (object key-value), and value (object).

[0056] In practical applications, when we use computers to browse the Internet, watch videos, or work, we generate a certain amount of cached data; hard drives also generate a lot of fragmentation due to frequent data storage and deletion, which means discontinuous data storage areas; for the method of obtaining cached data in this invention, existing cached data acquisition methods can be used, such as using DataView's rowfilter to filter cached data according to conditions to obtain the data to be cached; and the getInstance method of the QueueBufferFactory factory class, etc.

[0057] As an example, this invention uses the getInstance method of the QueueBufferFactory factory class to obtain the data to be cached.

[0058] Firstly, after obtaining the data to be cached, it is necessary to determine whether the data storage structure of the data is the same as the preset data storage structure described in this invention. If they are different, it is necessary to create a data structure that is the same as the structure represented by the preset data structure. If they are the same, the cached data in the preset time period can be used directly to update the cached data in the previous consecutive preset time period.

[0059] Step 120: Based on a preset time precision, preprocess the first data to be cached according to a preset data storage structure to obtain a first cache data structure that is identical to the structure represented by the preset data storage structure; the preset data storage structure includes a first structure for storing the object data type of the data to be cached and a second structure for storing the object data of the data to be cached.

[0060] In the embodiments of this specification, a time wheel with the same time precision is used to process the cached data. That is, the preset time precision is the same time precision. Users can set different time precisions of the same time precision according to their needs to obtain different storage effects.

[0061] The time precision of the time wheel is essentially the duration represented by each cell of the time wheel. Different precision time wheels mean that each cell of the time wheel represents a different duration. The data reading process is based on the time wheel processing the data in the reading queue. Simply put, the time wheel is a circular queue, and each time cell in the queue can store a list of timed tasks.

[0062] The preset data storage structure is a novel storage structure provided by this invention, namely a data structure of specific partition number-Map-Map; the first Map stores different object data types, namely N kinds of object data MapObj; the second Map stores object data, namely object data ConcurrentHashMap.

[0063] Based on the same time precision, cached data is stored in different partitions of the storage medium. Each partition is a "concurrent update buffer" divided into N storage areas to store cached objects; each partition has one partition number. A correspondence between object data types and object data is established for the cached data, stored in two different maps, thus creating a first cache data structure with a partition number-Map-Map data structure. The first Map stores different object data types, i.e., N types of object data (MapObj); the second Map stores object data, i.e., object data (ConcurrentHashMap). In other words, the preset data storage structure includes a first structure for storing the object data types of the data to be cached and a second structure for storing the object data itself.

[0064] Step 130: Store the first cache data structure in the first storage medium.

[0065] In the embodiments of this specification, the first storage medium refers to the storage space in the server. The storage space can be a storage space in the server memory or a separate space partitioned off from the hard disk to serve as the storage space of the first storage medium.

[0066] The first cache data structure includes a first structure for storing the object data type of the first cache data and a second structure for storing the object data of the first cache data; combined with the location information (partition number) of the first cache data structure in the first storage medium, the first cache data storage structure of partition number-object data type-object data is obtained.

[0067] Step 140: Obtain the second set of data to be cached.

[0068] In the embodiments of this specification, the data type of the second data to be cached can be the same as or different from the data type of the first data to be cached; the means of obtaining the second data to be cached can also be the same as the means of obtaining the first cached data, that is, the second data to be cached can be obtained through the getInstance method of the QueueBufferFactory factory class.

[0069] Step 150: Based on the preset time precision, preprocess the second data to be cached according to the preset data storage structure to obtain a second cache data structure that is the same as the structure represented by the preset data storage structure.

[0070] In the embodiments of this specification, the preset time precision is the same as the preset time precision in step 130, and the technical means of preprocessing the second data to be cached according to the preset data storage structure is the same as the technical means of preprocessing the first data to be cached according to the preset data storage structure in step 130, and will not be described again here.

[0071] Step 160: Replace the first cache data structure in the first storage medium with the second cache data structure.

[0072] In the embodiments of this specification, a synchronization lock can be used to control memory consistency, and the second cache data structure can be used to replace (synchronously update) the first cache data structure in the first storage medium.

[0073] In the field of computer science, synchronization locks are used to ensure that shared data is used by only one thread at a time. A simple implementation idea is to store a lock in the shared data. When no thread accesses it, the lock is empty. When the first thread accesses it, the lock stores the thread's identifier and allows the thread to access the shared data. Before the current thread releases the shared data, any other thread that wants to access the shared data must wait for the lock to be released.

[0074] Step 170: Store the replaced first cache data structure in the second storage medium.

[0075] Typically, a cache consists of multiple write (PUT) and multiple access (GET) endpoints, and internally includes an LRU counter and data eviction mechanism. During concurrent writes, accesses, and evictions, locks are generally used to control data consistency within the cache. However, in today's multi-SMP and NUMA server architectures, simple lock control for cache data consistency leads to threads competing for CPU resources, thereby reducing the efficiency of adding and accessing cache objects.

[0076] Among them, the consistency of data within the cache refers to "atomic consistency" or "strong consistency", which means that all cache operations are atomic. Once the operation of adding a cached object is completed, all operations that access the cached object will be able to read the object that was just written.

[0077] Lock control: Add lock control operations to the resources (memory structures) for operations such as "adding object type, adding cached object, and accessing cached object" to make these operations atomic.

[0078] CPU resource hoarding: When multiple threads operate on a single variable, only one thread will succeed at any given time, while the others will fail. The failing threads will retry (spinning). Spinning is essentially the unsuccessful threads hoarding CPU resources. Furthermore, spinning wastes significant CPU resources because the current thread is executing useless instructions.

[0079] During the "concurrent cache update" (data replacement process), the thread spins because it requisitions CPU resources. Since the current thread is running useless instructions, the spinning wastes a lot of CPU resources, resulting in low CPU resource utilization efficiency.

[0080] In the embodiments of this specification, based on a first time interval, it is detected whether a first cache data structure has been replaced, the first time interval being less than a preset time precision; if a replaced first cache data structure is detected, the read attribute of the second storage medium is set to unreadable by using a synchronization lock to control memory consistency; the object data and object data type in the first cache data structure are obtained, and the object data and object data type are stored in the second storage medium according to the preset time precision.

[0081] In the embodiments of this specification, by employing a preset time precision, a first cache data structure and a second cache data structure, identical in structure to the preset data storage structure, are sequentially established. The first cache data structure stored in the first storage medium is then replaced (updated) with the second cache data structure according to the preset time precision, and the replaced first cache data structure is stored in the second storage medium. An algorithm logic is established to update and evict cache data using the same time precision, the same data storage structure, and partitioned storage of cache data for different time periods. Because data structures are created with the same time precision and updated and evict data structures with the same time precision, the storage time of cached data in the second storage medium is increased, thus achieving a more orderly update and evictment of cached data and improving the hit rate when reading cached data.

[0082] In the real-time example of this specification, such as Figure 2 As shown, Figure 2 This is a schematic diagram of an update buffer data storage structure provided in the embodiments of this specification.

[0083] The methods described in steps 110 to 170 above correspond to Figure 2This document provides a storage structure for updating cached data. As an example, the server's synchronization thread creates a cached data structure every 1 millisecond. The data structure from the previous millisecond is defined as the first cached data structure, and the newly created cached structure is defined as the second cached data structure. The first cached data structure is replaced by a synchronization lock to ensure memory consistency. The second cached structure is stored in the lower left concurrent update buffer space (first storage medium). Simultaneously, the replaced first cached data structure is temporarily stored for processing by the cached data synchronization program. The object data and object data types from the replaced first cached data structure are stored in the lower right storage space (second storage medium).

[0084] Step 120: Based on a preset time precision, this may specifically include the following:

[0085] In the embodiments described in this specification, as examples of time wheels with different time precision existing in the prior art, refer to... Figures 3-5 .

[0086] like Figure 3 As shown, Figure 3 This is a schematic diagram of a time wheel data storage method with time precision that exists in the existing technology.

[0087] Figure 3 It's a time wheel with 12 time slots, taking 12 seconds to complete one rotation. To create a scheduled task that executes in 3 seconds, simply place it in the time slot with index 3. To create a scheduled task that executes in 9 seconds, simply place it in the time slot with index 9.

[0088] If you need to create a scheduled task that will execute in 15 seconds, you can introduce the concept of "repetitions / rounds". That is, the task is still placed in the time slot with index 3, but its repetition count is 2.

[0089] like Figure 4 As shown, Figure 4 This is a schematic diagram of another time wheel data storage method with different time precision existing in the technology.

[0090] Figure 4It's a time wheel with 20 time slots, divided into 3 layers, each with a different time precision. The first layer has a precision of 1 millisecond, the second layer has a precision of 20 milliseconds, and the third layer has a precision of 400 milliseconds. In practical applications, if we need to add a task A to store data 'a', which will execute in 350 milliseconds, and the current time is 0 seconds, this task will be placed in the second layer. This is because the time span of the second layer is 20 * 20 = 400. Since 400 milliseconds is greater than 350 milliseconds, 350 / 20 = 17. Therefore, data 'a' will be stored in the time slot with the value 17.

[0091] After the first layer has rotated 17 times, 340 milliseconds have passed, and the pointer on the second layer has now reached the 17th time slot. At this point, the task in the 17th slot of the second layer will be moved to the first layer.

[0092] If task A is scheduled to execute in 10 seconds, it will be moved to the 10th time slot in the first layer.

[0093] The data processing described above involves a data storage method with high time precision for the time wheel, and the coexistence of multiple time wheel data storage methods with different time precisions leads to complex data processing, thread blocking during updates, low server throughput, and unusable data reading.

[0094] In the embodiments of this specification, as an example, the present invention proposes a time wheel with a time precision of 1 millisecond for preprocessing cached data and concurrently updating (replacing) cached data. The time frequency of data processing and data updating is the same, ensuring data consistency and accuracy. Therefore, a data updating and eviction strategy with a time wheel of the same precision is provided, which avoids the problem of long cache data updating and reading time due to thread blocking, resulting in low server throughput.

[0095] Preferably, the time wheel is constructed according to the preset time precision; the time period of each segment in the time wheel is the same.

[0096] As an example, regarding the time precision of the time wheel, if the data source requires high data storage precision, the time wheel precision can be set to 1 millisecond; if the data source does not require high data storage precision, the time wheel precision can be set to 1 second or other time precisions. In this invention, regardless of the time precision used, each time wheel has the same precision, meaning data can be stored using the same precision time wheel. Compared to the first-in, first-out (FIFO) elimination strategy using different precision time wheels in existing technologies, this is simpler and more conducive to data processing.

[0097] Preferred, such as Figure 5 As shown, Figure 5 This is a schematic diagram of a time wheel data storage method with time precision provided in the embodiments of this specification.

[0098] Figure 5 It's a time wheel with 20 time slots, taking 20 milliseconds to complete one rotation. To create a new scheduled task B that executes in 15 milliseconds, simply place the task in the time slot with index 15. Similarly, to create a new scheduled task B that executes in 18 seconds, place it in the time slot with index 18.

[0099] If we need to create a scheduled task C that will execute in 26 seconds, we introduce the concept of "repetitions / rounds". This scheduled task C is still placed in the time cell with index 5, but its repetition count is 2.

[0100] It should be noted that this invention does not specifically limit the number of time windows in the time wheel; it can be 20, 12, or other numbers. Alternatively, it can be understood that the time window of the time wheel has L lengths, where L*1ms means that the time wheel can cache data for a maximum of L milliseconds.

[0101] Furthermore, the preset data storage structure mentioned in this invention is a completely new algorithm logic proposed relative to existing technologies.

[0102] Priority, such as Figure 6 As shown, Figure 6 This is a schematic diagram of a data storage structure provided in the embodiments of this specification.

[0103] Figure 6 Based on the same time precision, cached data is stored in different partitions of the storage medium. The partition is a "concurrent update buffer" that divides the storage space into N areas to store cached objects, and each partition has a partition number.

[0104] A mapping relationship between object data types and object data is established for cached data, and the data is stored in two different maps, thereby establishing a first cache data structure with a partition number-Map-Map data structure; the first map stores different object data types, i.e., N kinds of object data MapObj; the second map stores object data, i.e., object data Concurrent HashMap; that is, the preset data storage structure includes a first structure for storing the object data types of the data to be cached and a second structure for storing the object data of the data to be cached.

[0105] Preferred configuration: A fixed-length array of type Long[] is used to form a time wheel, where each element of the array represents a cell in the time wheel, and the precision of the time wheel is 1 millisecond. Time wheel storage: Each cell in the time wheel corresponds to a Map structure (data time series), storing the corresponding ObjectOfTimeQueue (object time queue). Each ObjectOfTimeQueue includes a nested Map-Map data structure to store data (N types of object data MapObj and object data HashMap). The first Map (N types of object data MapObj) stores different object data types, and the second Map (object data Hash Map) stores the object data.

[0106] The cached object data type ObjectType consists of a String type type and an Integer type mintimestamp. The type type defines the type name, and the mintimestamp defines the time wheel precision of the object of this type (an integer multiple of 1 millisecond).

[0107] It should be noted that the data attributes (data parameters) of cached data can include: Timestamp, type (object data type), key (object key-value), value (object), etc.; among them, the timestamp can be the time recorded and assigned by the computer when the cached data is generated; the object type is a correspondence between an object key-value and a time precision, and one object type corresponds to one time precision of the time wheel; the time precision is expressed in Int (integer), and the unit is milliseconds.

[0108] Data of different precisions all have timestamps. Data of different precisions are stored in the same way and only in the cell of the time wheel corresponding to the timestamp. Different precisions mean: the valid data from the beginning of the timestamp to the end of "timestamp + time precision".

[0109] The time precision of the time wheel for different types of object data is directly converted to the basic time wheel precision for data storage because all different time precisions are integer multiples of the basic time wheel (millisecond precision).

[0110] In step 130, storing the first cache data structure in the first storage medium specifically includes:

[0111] Based on the timestamp of the first data to be cached, a data value uniquely corresponding to the first data to be cached is determined; based on the data value, the partition number corresponding to the first cache data structure is determined; the partition number is the partition number of the partition medium in the first storage medium; based on the partition number, the first cache data structure is stored in the partition medium corresponding to the partition number.

[0112] Specifically, based on the timestamp of the data to be cached, a unique data value corresponding to the data to be cached can be determined; for example, through a global AtomicInteger, each object obtains a unique int count; when the cache starts, the initial value of AtomicInteger is 1. Before receiving cached data, the current value after auto-incrementing is obtained from AtomicInteger, which is the sequence number of the received cached data, such as 2.

[0113] Furthermore, the step of preprocessing the first data to be cached according to the preset data storage structure to obtain a first cache data structure that is identical to the structure represented by the preset data storage structure specifically includes: obtaining the object data type of the first data to be cached and storing the object data type in the first structure; obtaining the object data of the first data to be cached and storing the object data in the second structure; and establishing the first cache data structure that includes the partition number, the first structure, and the second structure.

[0114] Preferably, determining the partition number corresponding to the first cached data structure based on the data value specifically includes: dividing the data value by the number of partitions of the first storage medium to obtain a remainder value; and determining the remainder value as the partition number corresponding to the first cached data structure.

[0115] As an example: The four parameters of the cache object and the sequence number of the received cache data constitute the received cache data. The data partition is calculated by dividing the sequence number of the received cache data by the number of partitions on the storage medium, and the remainder is the partition number corresponding to the received cache data. The number of partitions is the partition value of the storage medium, such as 3 partitions.

[0116] For example, if the remainders of data 1 and data 2 obtained by % calculation are 1 and 2 respectively, then data 1 is stored in "partition-1" and data 2 is stored in "partition-2".

[0117] Temporary cache updates involve sequentially calling the put method of the partition, N types of object data, and object data in the "concurrent update buffer" based on the partition number, type (object data type), and key (object key value) in the received cache data, and storing the cached object into the "concurrent update buffer".

[0118] Furthermore, the first data to be cached is preprocessed according to a preset data storage structure to obtain a first cached data structure with the same structure as the preset data storage structure, specifically including:

[0119] Obtain the object data type of the first data to be cached, and store the object data type in the first structure; obtain the object data of the first data to be cached, and store the object data in the second structure; establish a first cache data structure containing the partition number, the first structure, and the second structure; the first cache data structure includes the correspondence between the object type of the first data to be cached and the object data of the first data to be cached.

[0120] Furthermore, such as Figure 6 As shown, Figure 6 The “concurrent update buffer” mentioned herein may refer to the first storage medium in this specification.

[0121] After the acquired data to be cached is processed through steps S110 to S130, a first cache data structure is obtained, which includes partition number-object data type-object data (partition number-map-map). This cache data structure is stored in a first storage medium, which can be the computer's cache space or a manually partitioned storage space.

[0122] Preferably, in step 110, obtaining the first cached data specifically includes: obtaining the first cached data that meets a preset type from the dataset of data to be cached.

[0123] like Figure 7 As shown, Figure 7 This is a flowchart of a method for caching data according to a preset type, provided in an embodiment of this specification.

[0124] Specifically, the first data to be cached that meets a preset type can be obtained from the dataset of data to be cached. Alternatively, the first data to be cached can be processed before preprocessing based on a preset time precision and a preset data storage structure to filter out cache data that meets the preset type; further according to Figure 7 The method shown processes the first batch of data to be cached that meets the preset type conditions. Figure 7 The methods for data processing and data updating (replacement) shown in the text are similar to those in the text. Figure 1 The methods for steps S110 to S170 are the same and will not be repeated here.

[0125] For example, the preset type can be a perception data type or an event data type. Before obtaining the first data to be cached, the data required by the user is obtained from the data set to be cached as the first data to be cached. The method disclosed in this invention is used to process the data, which can also achieve the technical effect of this invention.

[0126] Data that users do not need will be temporarily stored or directly removed from the cache. For temporarily stored cached data, when the demand changes, further filtering can be performed to obtain cached data that matches the demand.

[0127] The method described above for obtaining the first cached data that meets the preset type from the dataset of cached data can also be used to obtain the second cached data that meets the preset type from the dataset of cached data. The method for obtaining the second cached data that meets the preset type from the dataset of cached data will not be described again here.

[0128] In step 160, replacing the first cache data structure in the first storage medium with the second cache data structure specifically includes: replacing the first cache data structure in the first storage medium with the second cache data structure by controlling memory consistency through a synchronization lock.

[0129] In computer science, synchronization locks are used to ensure that shared data is used by only one thread at a time. A simple implementation involves storing a lock within the shared data. When no thread is accessing it, the lock is empty. When the first thread accesses the data, its identifier is stored in the lock, allowing it to access the shared data. Before the current thread releases the shared data, any other thread attempting to access it must wait for the lock to be released. Therefore, this application implements data updates by using synchronization locks to control memory consistency. Specifically, when a second cached data structure replaces a first cached data structure, no other thread will attempt to replace it. Only after the replacement of the first cached data structure by the second cached data structure is complete can the synchronization lock be released, allowing the next thread to execute its operation.

[0130] For example, the time slice allocated to each thread by the CPU is random, and in Java, many threads share a single resource, such as selling train tickets. Train tickets are a fixed number, but there are ticket windows everywhere, each window acting as a thread. All these threads share the resource of all the train tickets. If two threads use this resource simultaneously at a given time, they will obtain the same tickets (same seat number), causing inconvenience to passengers.

[0131] When we see that windows C and B both sold ticket number 10, and windows A and B sold tickets number 0 and -1 respectively, the reasons for this situation are: 1. When ticket = 10, after thread C retrieves ticket number 10, before the ticket number has been decremented by 1, thread B retrieves its ticket, and the ticket number is still 10; 2. When ticket = 1, thread C retrieves ticket number 1, and before the ticket number has been decremented by 1, threads A and B enter the if statement sequentially, at which point the ticket number is decremented by 1. Therefore, when threads A and B retrieve their tickets, they retrieve tickets number 0 and -1 respectively.

[0132] To avoid the above situation, we can do the following: when a thread wants to use the train ticket resource, we give it a lock, and after it finishes its task, we give the lock to another thread that wants to use the resource.

[0133] Preferably, in step 170, storing the replaced first cache data structure in the second storage medium specifically includes: detecting whether there is a replaced first cache data structure based on a first time interval, wherein the first time interval is less than the preset time precision.

[0134] If the first cached data structure that has been replaced is detected, the attribute of the second storage medium is set to unreadable by using a synchronization lock to control memory consistency.

[0135] Obtain the object data and object data type from the first cached data structure, and store the object data and object data type in the second storage medium according to the preset time precision.

[0136] As an example, the server's synchronization thread checks every 100 microseconds whether there is any temporarily stored concurrent update buffer data structure (first cache data structure) that needs to be replaced. When it detects data that needs to be synchronized, the synchronization thread controls the time wheel data of the second storage medium to be unreadable by controlling memory consistency through a synchronization lock. It also reads all object data and object data types in the temporarily stored concurrent update buffer data structure (first cache data structure), writes the object data and object data types according to the time wheel logic, and finally releases the synchronization lock and clears the temporarily stored concurrent update buffer data structure.

[0137] Among them, writing object data according to time wheel logic can write object data to the second storage medium with a preset time precision of 1 millisecond.

[0138] Specifically, a Begin_Ts timestamp is stored in the timewindow, corresponding to the 0th element of the timewindow. The difference (called offset) between the newly added data timestamp and Begin_Ts is j, which corresponds to the jth element of the timewindow.

[0139] In the time series data, the element number and ObjectOfTimeQueue key-value pairs are stored.

[0140] For example: If the timestamp of Begin_Ts is 1671585946247 and the timestamp of data 1 is 1671585946247, and the difference between timestamp and Begin_Ts is 0, then data 1 will be stored in an ObjectOfTimeQueue object data Hashmap with key 0 and value MapObj-1, which is "N types of object data" and has key "perception" and value "CHM1".

[0141] Preferably, the data eviction strategy in the second storage medium further includes:

[0142] Based on a second time interval, the server implements a cache data eviction policy on the cached data in the second storage medium, deleting object data that exceeds the second time interval from the data time-series storage of the second storage medium.

[0143] The cached data eviction policy is a strategy that evicts data stored in the storage medium before a preset time interval, in order to avoid the amount of data in the storage medium becoming too large and to ensure the storage efficiency and data throughput of the storage medium.

[0144] As an example, an eviction process begins every K seconds, removing data older than K seconds from the current data time series storage. Here, K seconds can be the default value initialized when the cache starts, or K can be less than 1 / 2*L, so the cached data will definitely be evicted before the time wheel is full.

[0145] When eviction occurs, a new Begin_Ts (timestamp of cached data at the time of eviction) is created, and the new data time series is stored. The difference M is obtained by subtracting Begin_Ts from newBegin_Ts. The elements (0 to M) are the data to be cleared. The new key value is obtained by subtracting M from the key of other data time series elements. The data is then transferred to the new data time series. Finally, Begin_Ts is replaced by newBegin_Ts.

[0146] Specifically, the key-value pairs from the (M+1)th digit to the current digit that need to be retained are read from the "current data time series". The new key is obtained by subtracting M from all keys, resulting in the "new key-value pairs", which are then stored in the "new data time series".

[0147] Preferably, the method provided in this specification may also include a method for expressing the reading of cached data. For example... Figure 8 The above, Figure 8 This is another flowchart illustrating a data caching method provided in the embodiments of this specification.

[0148] Step 210: Receive a cached data query request; the cached data query request contains data parameters of the data to be queried; the data parameters include the object data type and / or object key value of the data.

[0149] Among them, the object data type represents the type of the object data, i.e., type; the data types are divided into: integer type, long integer type, single-precision real number type, double-precision real number type, string type, etc.; perceptual data and event-type data can use one or more of the above data types.

[0150] In a data structure, an object's key represents the key value corresponding to the object's data. Specifically, in a data structure, a heap where the key of the root node (also called the heap top) is the smallest among all the keys of all nodes in the heap is called a min-heap; a heap where the key of the root node (also called the heap top) is the largest among all the keys of all nodes in the heap is called a max-heap. Here, the data structure refers to the nodes of the heap. A heap is essentially a tree structure. In a min-heap, the value of any node is smaller than the values ​​of all nodes in its subtrees. The value used for comparison between two nodes is the node's key. Essentially, as long as its key value is smaller than the key values ​​of its child nodes, and each node's key value is smaller than the key values ​​of its child nodes, then the root node's key value must be smaller than the key values ​​of all its descendant nodes.

[0151] Step 220: Determine whether the data parameters exist in the second storage medium.

[0152] Specifically, all cached data query requests acquire a shared read lock. The query parameters of the query request include the object data type and / or object key value, and may also include the difference between begintimestamp (start timestamp), endtimestamp (end timestamp), and Begin_Ts, which represents the start and end times of the key in the data time series. Data is read according to the preset time precision. Data stored after begintimestamp will be read and is valid at the begintimestamp time, but data stored before begintimestamp will not be read.

[0153] As an example, when the query request only contains the parameter "type" and the other query parameters are null, the query will retrieve all data of the "type" parameter within the last 2 seconds. This means that the "begintimestamp" parameter is padded with the current timestamp and the "endtimestamp" parameter is padded with the timestamp from 2 seconds ago, thus retrieving all data within that time period.

[0154] As an example, when the query request only contains the key as a query parameter and the other query parameters are null, the query will retrieve all data for the key within the last 2 seconds. That is, the begintimestamp (start timestamp) will be padded with the current timestamp and the endtimestamp (end timestamp) will be padded with the timestamp from 2 seconds ago, thus retrieving all data within that time period.

[0155] As an example, if the query parameters in the query request include type, begintimestamp, and endtimestamp, then all data of type within the range from begintimestamp to endtimestamp will be retrieved.

[0156] As an example, when the query parameters in the query request include key, begintimestamp, and endtimestamp, the query will retrieve all data for the key within the range from begintimestamp to endtimestamp.

[0157] As an example, if the query parameters in the query request include type and key, then the query will retrieve all data where type and key both exist within the last 2 seconds, with begintimestamp (start timestamp) supplemented with the current timestamp and endtimestamp (end timestamp) supplemented with the timestamp from 2 seconds ago.

[0158] As an example, when the query parameters in the query request are type, key, begintimestamp, and endtimestamp, retrieve all data with type and key from begintimestamp to endtimestamp.

[0159] Step 230: If it exists, then traverse the cache data structure of the second storage medium according to the data parameters to obtain the query result.

[0160] Based on the cache query request parsing, first verify whether the accessed object type, start timestamp, and end timestamp are correct, and whether it is data in the cache. If the verification fails, return empty directly.

[0161] After the verification is passed, the time wheel cache data structure is traversed according to the query parameters in the query request to obtain the cached object data and cached object type. The object data and object type are combined into a Map structure and returned. The Map structure is a Map structure formed by merging the object data and object data type.

[0162] It should be noted that this invention obtains first data to be cached, processes it based on a preset time precision and a preset data storage structure to obtain first cached data with the same preset data storage structure, and stores the first cached data in a first storage medium; further, it processes the obtained second data to be cached based on the same preset time precision and preset data storage structure to obtain second cached data with the same preset data structure; and replaces the first cached data with the second cached data based on the same preset time precision, stores the replaced first cached data in a second storage medium, and executes a cached data eviction policy on the cached data in the second storage medium; thus, it provides a strategy for processing and eviction of cached data with the same time precision using a time wheel, and in... When replacing the first cached data with the second cached data, and storing the replaced first cached data in the second storage medium, a synchronization lock is used to control the consistency of the second storage medium's memory to update the cached data. This solves the problem in existing technologies where, during concurrent cache updates, blocking leads to long latency and low throughput for parallel cache updates and reads. It reduces thread failure spins between adding cached objects and accessing cached objects, improves the throughput of accessing cached objects, and achieves low latency under high concurrency throughput while still ensuring strong consistency of data within the cache. The lock control enables atomic operations for adding cached objects, synchronizing cached data, and concurrently accessing cached data. This achieves a high cache hit rate and ensures that frequently used cached data is not easily evicted.

[0163] In summary, the data caching method provided by this invention can solve the problems of low cache hit rate and easy eviction of frequently used cached data; and adopts a time wheel strategy with the same time precision to cache and evict data to ensure strong consistency of data in the cache. Through lock control, it realizes atomic operations of adding cache objects, synchronizing cache data, and accessing cached data, thereby improving the efficiency and accuracy of concurrent write and cache access under SMP multi-core CPU architecture.

[0164] Furthermore, based on the data caching method provided by this invention, this invention also includes a data reading method, which implements a method for parsing input parameters and converting them into corresponding query types or key values, thereby executing different data structure access processes, improving data access efficiency and increasing data hit rate.

[0165] Based on the same concept, this specification also provides a data caching processing device. For example... Figure 9 As shown, Figure 9 This is a schematic diagram of a data caching device provided in an embodiment of this specification.

[0166] like Figure 9 As shown, the apparatus includes:

[0167] The first acquisition module 910 is used to acquire the first data to be cached;

[0168] The first preprocessing module 920 is used to preprocess the first data to be cached based on a preset time precision and a preset data storage structure to obtain a first cached data structure that is the same as the structure represented by the preset data storage structure; the preset data storage structure includes a first structure for storing object data type of data to be cached and a second structure for storing object data of data to be cached.

[0169] The first storage module 930 is used to store the first cache data structure in the first storage medium;

[0170] The second acquisition module 940 is used to acquire the second data to be cached.

[0171] The second preprocessing module 950 is used to preprocess the second data to be cached based on the preset time precision and the preset data storage structure to obtain a second cached data structure that is the same as the structure represented by the preset data storage structure.

[0172] Replacement module 960 is used to replace the first cache data structure in the first storage medium with the second cache data structure;

[0173] The second storage module 970 is used to store the replaced first cache data structure in the second storage medium.

[0174] The apparatus described in the embodiments of this specification may further include a module for data querying, such as... Figure 10 As shown, Figure 10 This is another schematic diagram of a data caching device provided in the embodiments of this specification.

[0175] The first receiving module 1010 is used to receive a cached data query request; the cached data query request contains data parameters of the data to be queried; the data parameters include the object data type and / or object key value of the data.

[0176] The first judgment module 1020 is used to determine whether the data parameters exist in the second storage medium;

[0177] The first traversal module 1030 is used to traverse the cache data structure of the second storage medium according to the data parameters if the data exists, and obtain the query result.

[0178] For detailed instructions on the above devices, please refer to the corresponding methods described above; they will not be repeated here.

[0179] It should also be noted that the terms “comprising,” “including,” or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus.

[0180] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to interchangeably. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments.

[0181] The above description is merely an embodiment of this specification and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principle of this application should be included within the scope of the claims of this application.

Claims

1. A method for data caching, characterized in that, include: Get the first data to be cached; Based on a preset time precision, the first data to be cached is preprocessed according to a preset data storage structure to obtain a first cached data structure that is the same as the structure represented by the preset data storage structure. The preset data storage structure includes a first structure for storing object data types of data to be cached and a second structure for storing object data to be cached; the preset time precision is determined according to the data storage precision requirements of the data to be cached; the first cache data structure is stored in a first storage medium; Get the second set of data to be cached; Based on the preset time precision, the second data to be cached is preprocessed according to the preset data storage structure to obtain a second cached data structure that is the same as the structure represented by the preset data storage structure. Replace the first cache data structure in the first storage medium with the second cache data structure; The replaced first cache data structure is stored in the second storage medium; The step of storing the replaced first cache data structure in the second storage medium specifically includes: Based on a first time interval, it is detected whether the first cached data structure has been replaced, wherein the first time interval is less than the preset time precision; If the replaced first cache data structure is detected, the attribute of the second storage medium is set to unreadable by using a synchronization lock to control memory consistency; Obtain the object data and object data type from the first cached data structure, and store the object data and object data type in the second storage medium according to the preset time precision.

2. The method as described in claim 1, characterized in that, The step of storing the first cache data structure in the first storage medium specifically includes: Based on the timestamp of the first data to be cached, determine the data value that uniquely corresponds to the first data to be cached; Based on the data value, the partition number corresponding to the first cache data structure is determined; the partition number is the partition number of the partition medium in the first storage medium. Based on the partition number, the first cache data structure is stored in the partition medium corresponding to the partition number.

3. The method as described in claim 2, characterized in that, The step of preprocessing the first data to be cached according to a preset data storage structure to obtain a first cached data structure with the same structure as the preset data storage structure specifically includes: Obtain the object type of the first data to be cached, and store the object data type in the first structure; Obtain the object data of the first data to be cached, and store the object data in the second structure; Establish a first cache data structure that includes the partition number, the first structure, and the second structure.

4. The method as described in claim 1, characterized in that, The process of obtaining the first data to be cached specifically includes: Retrieve the first data to be cached that meets the preset type from the dataset of data to be cached.

5. The method as described in claim 1, characterized in that, Also includes: Construct a time wheel according to the preset time precision; Each segment in the time wheel has the same time period.

6. The method as described in claim 1, characterized in that, The step of replacing the first cache data structure in the first storage medium with the second cache data structure specifically includes: By using a synchronization lock to control memory consistency, the first cache data structure in the first storage medium is replaced with the second cache data structure.

7. The method as described in claim 1, characterized in that, Also includes: Based on the second time interval, a cache data eviction policy is implemented on the cached data in the second storage medium to delete object data that exceeds the second time interval from the data time series storage of the second storage medium.

8. The method as described in claim 1, characterized in that, Also includes: Receive a cached data query request; the cached data query request contains data parameters of the data to be queried; the data parameters include the object data type and / or object key-value pairs of the data. Determine whether the data parameters exist in the second storage medium; If it exists, the cache data structure of the second storage medium is traversed according to the data parameters to obtain the query result.

9. An apparatus for data caching, characterized in that, include: The first acquisition module is used to acquire the first data to be cached. The first preprocessing module is used to preprocess the first data to be cached based on a preset time precision and a preset data storage structure to obtain a first cache data structure that is the same as the structure represented by the preset data storage structure. The preset data storage structure includes a first structure for storing object data types to be cached and a second structure for storing object data to be cached. The first storage module is used to store the first cache data structure in the first storage medium; The second acquisition module is used to acquire the second data to be cached. The second preprocessing module is used to preprocess the second data to be cached based on the preset time precision and the preset data storage structure to obtain a second cached data structure that is the same as the structure represented by the preset data storage structure. The preset time precision is determined based on the data storage precision requirements of the data to be cached. A replacement module is used to replace the first cache data structure in the first storage medium with the second cache data structure; The second storage module is used to store the replaced first cache data structure in the second storage medium; The second storage module is specifically used for: Based on a first time interval, it is detected whether the first cached data structure has been replaced, wherein the first time interval is less than the preset time precision; If the replaced first cache data structure is detected, the attribute of the second storage medium is set to unreadable by using a synchronization lock to control memory consistency; Obtain the object data and object data type from the first cached data structure, and store the object data and object data type in the second storage medium according to the preset time precision.

Citation Information

Patent Citations

  • Data processing method and device, server and storage medium

    CN112306369A

  • Cache data refreshing method and distributed system

    CN112597195A