Distributed Caching Method, Device, Terminal Device and Storage Medium for Business Data
By using a preset algorithm in the distributed cache system to calculate the hash value of the target data object and perform modulo calculations in combination with the number of cache machines, the problem of client reading failure after the cache machine is expanded is solved, and the efficiency of distributed cache is improved.
Patent Information
- Application Number
- CN202010571207.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-19
- Publication Date
- 2025-06-10
- Estimated Expiration
- 2040-06-19
AI Technical Summary
The distributed caching method of existing business data After the cache machine is expanded, the client may easily hit the cache machine when reading the cache business data, resulting in the cache policy failure.
By calculating the hash value of the target data object in the business data according to the preset algorithm, and performing modulo operations in combination with the number of cache machines in the distributed cache system, the reading position on the target cache machine is determined.
It avoids the problem that the client cannot accurately hit the data cache machine after the cache machine is expanded, and improves the distributed cache efficiency of business data.
Smart Images

Figure CN111723113B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of financial technology (Fintech), and particularly to a distributed caching method, device, terminal device and computer-readable storage medium for business data. Background Art
[0002] With the development of computer technology, more and more technologies are applied in the financial field, and traditional finance is gradually transforming into financial technology (Fintech). However, due to the security and real-time requirements of the financial industry, higher requirements are also imposed on technologies.
[0003] Currently, in the development of common Internet distributed application services, in order to reduce the access pressure on the system database and improve the access speed, the Internet distributed caching method is usually adopted, where different data is stored on different caching machines. In order to achieve load balancing among caching machines during distributed caching, generally, the hash value of a data object is obtained through a hash function, and then the caching machine for this data object is located. In this way, the caching requests of the client for different data objects can be dispatched to different caching machines.
[0004] However, in the case of expanding the caching machines in the system, since the total number of caching machines changes before and after the expansion, the encoding of the caching machine for the same data object in the system will also change accordingly. Thus, after the caching machines are expanded, the client will not be able to read the data object from the caching machine specified by the current location encoding, that is, the client fails to hit the system caching machine when reading cached data. Moreover, when there are dozens or even hundreds of caching machines in the distributed caching, the failure rate of the client hitting the caching machine to read data after the system caches the data object will become higher, and basically the caching strategy will fail. Summary of the Invention
[0005] The main objective of the present invention is to provide a distributed caching method, device, equipment and computer-readable storage medium for business data, aiming to solve the technical problem that in the existing distributed caching method for business data, after the caching machines are expanded, it is easy to fail for the client to hit the caching machine when reading cached business data.
[0006] To achieve the above objective, the present invention provides a distributed caching method for business data, and the distributed caching method for business data includes:
[0007] Calculate the hash value of a target data object in the business data according to a preset algorithm;
[0008] Obtain the first number of machines of the caching machines in the distributed caching system that cache the business data;
[0009] Perform a modulo operation based on the hash value and the number of the first machines to obtain a first modulo value, so as to read the target data object from the target cache machine specified by the first modulo value.
[0010] Optionally, the step of calculating the hash value of the target data object in the service data according to a preset algorithm includes:
[0011] Obtain the actual length of the target data object, and perform an exclusive OR operation on binary numbers based on the actual length to obtain a first calculation result;
[0012] Perform an unsigned right shift operation on binary numbers based on the first calculation result to obtain a second calculation result, and use the second calculation result as the hash value of the target data object.
[0013] Optionally, the step of obtaining the actual length of the target data object includes:
[0014] Convert the target data object into array data;
[0015] Construct a byte buffer object according to the array data, and reorder the byte buffer object according to a preset sorting rule;
[0016] Obtain the length of the byte buffer object after reordering, and use the length as the actual length of the target data object.
[0017] Optionally, the step of performing a modulo operation based on the hash value and the number of the first machines to obtain a first modulo value includes:
[0018] Perform a binary AND operation on the hash value and a preset constant to obtain an intermediate result;
[0019] Perform a modulo operation on the intermediate result and the number of the first machines to obtain a first modulo value.
[0020] Optionally, before the step of obtaining the number of the first machines of the cache machines caching the service data in the distributed cache system, it further includes:
[0021] Expand the cache machines caching the service data in the distributed cache system.
[0022] Optionally, the step of expanding the cache machines caching the service data in the distributed cache system includes:
[0023] According to the sequential numbers of the cache machines, promote the backup machines corresponding to the cache machines to new cache machines;
[0024] Establish backup machines corresponding to the new cache machines to complete the expansion of the cache machines.
[0025] Optionally, before the step of expanding the cache machines that cache the service data in the distributed cache system, the method further includes:
[0026] Obtaining a second number of machines of the cache machines that cache the service data in the distributed cache system;
[0027] Performing a modulo operation based on the hash value and the second number of machines to obtain a second modulo value, so as to cache the target data object on the target cache machine specified by the second modulo value.
[0028] In addition, to achieve the above object, the present invention further provides a distributed cache device for service data, where the distributed cache device for service data includes:
[0029] A hash value obtaining module, configured to calculate a hash value of a target data object in service data according to a preset algorithm;
[0030] An obtaining module, configured to obtain a first number of machines of the cache machines that cache the service data in the distributed cache system;
[0031] A data reading module, configured to perform a modulo operation based on the hash value and the first number of machines to obtain a first modulo value, so as to read the target data object from the target cache machine specified by the first modulo value.
[0032] In addition, to achieve the above object, the present invention further provides a terminal device, where the terminal device includes: a memory, a processor, and a distributed cache program for service data stored on the memory and executable on the processor. When the distributed cache program for service data is executed by the processor, the steps of the distributed cache method for service data as described above are implemented.
[0033] In addition, to achieve the above object, the present invention further provides a computer-readable storage medium, where a distributed cache program for service data is stored on the computer-readable storage medium. When the distributed cache program for service data is executed by a processor, the steps of the distributed cache method for service data as described above are implemented.
[0034] The present invention provides a distributed cache method, device, terminal device, and computer-readable storage medium for service data. By calculating a hash value of a target data object in service data according to a preset algorithm; obtaining a first number of machines of the cache machines that cache the service data in the distributed cache system; performing a modulo operation based on the hash value and the first number of machines to obtain a first modulo value, so as to read the target data object from the target cache machine specified by the first modulo value.
[0035] Based on a preset algorithm that can accurately distinguish each data object in business data, the present invention calculates the hash value of the target data object that needs to be cached in the distributed cache system or read by the client. Then, when the client reads the target data object, a modulo operation is performed in combination with the hash value and the number of cache machines used to cache business data in the distributed cache system, so as to obtain a modulo value that identifies the cache machine number storing the target data object, facilitating the client to read the target data object from the cache machine hit by the modulo value. The present invention avoids the problem that in the existing distributed caching method of business data, after the total number of cache machines in the system changes, the client cannot accurately hit the cache machine storing the data for data reading. It not only reduces the probability of hash collisions between data with high similarity based on accurately distinguishing each data object in business data, but also ensures that after the cache machines change in the distributed environment, the client can still accurately hit the cache, improving the distributed caching efficiency of business data. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] Figure 1 It is a schematic diagram of the device structure of the hardware operating environment related to the solution of the embodiment of the present invention;
[0037] Figure 2 It is a schematic flowchart of the first embodiment of the distributed caching method for business data of the present invention;
[0038] Figure 3 It is a schematic flowchart of the second embodiment of the distributed caching method for business data of the present invention;
[0039] Figure 4 It is a schematic flowchart of the third embodiment of the distributed caching method for business data of the present invention;
[0040] Figure 5 It is a schematic diagram of the application scenario related to an embodiment of the distributed caching method for business data of the present invention;
[0041] Figure 6 It is a schematic diagram of another application scenario related to an embodiment of the distributed caching method for business data of the present invention;
[0042] Figure 7 It is a schematic diagram of the functional modules of an embodiment of the distributed caching device for business data of the present invention.
[0043] The realization, functional features and advantages of the object of the present invention will be further described with reference to the embodiments and the accompanying drawings. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0044] It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0045] Refer toFigure 1 , Figure 1 is a schematic diagram of the device structure of the hardware operating environment involved in the embodiment solution of the present invention.
[0046] The terminal device in the embodiment of the present invention can be a smart phone, or a PC (Personal Computer), a tablet computer, a portable computer, or other terminal devices.
[0047] As Figure 1 shown, the terminal device may include: a processor 1001, such as a CPU, a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Display), an input unit such as a keyboard (Keyboard), and optionally, the user interface 1003 may further include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a Wi-Fi interface). The memory 1005 may be a high-speed RAM memory, or a stable memory (non-volatile memory), such as a disk memory. The memory 1005 may optionally be a storage device independent of the aforementioned processor 1001.
[0048] Those skilled in the art can understand that Figure 1 the terminal device structure shown in
[0049] does not constitute a limitation on the terminal device, and may include more or fewer components than shown in the figure, or combine some components, or have different component arrangements. Figure 1 As
[0050] shown, the memory 1005, as a computer storage medium, may include an operating system, a network communication module, a user interface module, and a distributed cache program for service data. Figure 1 In the terminal shown in
[0051] the network interface 1004 is mainly used to connect to the background server and communicate with the background server for data; the user interface 1003 is mainly used to connect to the client and communicate with the client for data; and the processor 1001 can be used to call the distributed cache program for service data stored in the memory 1005 and perform the following operations:
[0052] Calculate the hash value of the target data object in the service data according to a preset algorithm;
[0053] Perform a modulo operation based on the hash value and the number of the first machines to obtain a first modulo value, so as to read the target data object from the target cache machine specified by the first modulo value.
[0054] Further, the processor 1001 may call the distributed cache program of the service data stored in the memory 1005, and further perform the following operations:
[0055] Obtain the actual length of the target data object, and perform an exclusive OR operation on binary numbers based on the actual length to obtain a first calculation result;
[0056] Perform an unsigned right shift operation on binary numbers based on the first calculation result to obtain a second calculation result, and use the second calculation result as the hash value of the target data object.
[0057] Further, the processor 1001 may call the distributed cache program of the service data stored in the memory 1005, and further perform the following operations:
[0058] Convert the target data object into array data;
[0059] Construct a byte buffer object according to the array data, and reorder the byte buffer object according to a preset sorting rule;
[0060] Obtain the length of the byte buffer object after reordering, and use the length as the actual length of the target data object.
[0061] Further, the processor 1001 may call the distributed cache program of the service data stored in the memory 1005, and further perform the following operations:
[0062] Perform a binary AND operation based on the hash value and a preset constant to obtain an intermediate result;
[0063] Perform a modulo operation based on the intermediate result and the number of the first machines to obtain a first modulo value.
[0064] Further, the processor 1001 may call the distributed cache program of the service data stored in the memory 1005, and before executing to obtain the number of the first machines of the cache machines caching the service data in the distributed cache system, further perform the following operations:
[0065] Expand the cache machines caching the service data in the distributed cache system.
[0066] Further, the processor 1001 may call the distributed cache program of the service data stored in the memory 1005, and further perform the following operations:
[0067] According to the sequential number of the cache machines, promote the backup machine corresponding to the cache machine to a new cache machine;
[0068] Establish a backup machine corresponding to the new cache machine to complete the expansion of the cache machine.
[0069] Further, the processor 1001 may call the distributed cache program of the service data stored in the memory 1005. Before performing the expansion of the cache machine that caches the service data in the distributed cache system, the following operations are also performed:
[0070] Obtain the second machine number of the cache machines that cache the service data in the distributed cache system;
[0071] Perform a modulo operation based on the hash value and the second machine number to obtain a second modulo value, so as to cache the target data object on the target cache machine specified by the second modulo value.
[0072] Based on the above hardware structure, various embodiments of the distributed cache method for service data of the present invention are proposed. It should be noted that in the common development of Internet distributed application services, in order to read the service data persisted in the system, in addition to directly reading from the database, it can also be read from each cache machine in the distributed cache system. In order to reduce the access pressure on the database (i.e., the distributed cache system) and improve the access speed, for Internet distributed caches, different service data is usually stored on different cache machines in the system. Therefore, in order to achieve the load balancing of these cache machines, the algorithm shown in formula 1 below is often used to locate the storage machine where the target data object in the service data is cached.
[0073] m = hash(o) mod n —— Formula 1;
[0074] In this formula 1, o is the target data object, n is the number of cache machines in the distributed cache system, m is the machine number, hash (hash) is the hash value obtained by the target data object through the hash function, and the hash values of different target data objects are unique.
[0075] Using the above algorithm, the cache requests of different target data objects in the service data by the client can be dispatched to different cache machines in the distributed cache system. For example, for the target data object o, after the calculation of this algorithm, the value of m is 3, then all requests for reading and storing the target data object o are sent to cache machine 3 for execution.
[0076] The above algorithm works well most of the time. However, when the cache machine in the distributed cache system needs to be expanded or one (or more) cache machines go down, the value of n will change, and the above formula 1 will change accordingly:
[0077] m=hash(o)mod(n+1)——Formula 2 (machine expansion by 1);
[0078] m=hash(o)mod(n-1)——Formula 3 (the server crashes or is reduced in capacity by one server).
[0079] Take the expansion of cache machines in a distributed cache system as an example, assuming that the number of cache machines increases from 3 to 4. Assuming that the hash value of a target data object o is 9, since the hash value of the target data object is unchanged, before the cache machine is expanded, the modulo value of the target data object o according to the algorithm of formula 1 is 0. At this time, the distributed cache system will send all accesses to o to the cache machine numbered 0. If the cache machine is expanded to 4 at this time, the modulo value calculated by formula 2 for the target data object o will become 1. Therefore, if the client sends a request to access o, the distributed cache system will send the request to the cache machine numbered 1. However, since the system has cached the target data object o on cache machine 0 before the expansion, it is obviously impossible to obtain the target data object from cache machine 1 at this time, which leads to the failure of the client to obtain the target data object, and generates additional database access overhead. As a result, in actual applications, the number of cache machines in a distributed cache system may reach dozens or even hundreds, and the probability of a client failing to read data from a cache machine will be higher, which will basically invalidate the overall cache strategy.
[0080] In view of the above phenomenon, the present invention provides a distributed caching method for business data. Figure 2 , Figure 2 It is a flow chart of the first embodiment of the distributed caching method for business data of the present invention.
[0081] In this embodiment, the distributed caching method of the business data includes:
[0082] Step S10, calculating the hash value of the target data object in the business data according to a preset algorithm;
[0083] The terminal device optimizes the hash value algorithm for calculating each target data object in the business data to obtain a preset algorithm that can accurately distinguish each data object in the business data, and then calculates the hash value of the target data object in the business data that the distributed cache system needs to cache or the client needs to read based on the preset algorithm.
[0084] It should be noted that in this embodiment, since the amount of data in the application server is very large, the massive amount of data will cause data objects with high similarity between the generated business data, which will cause the hash values of some highly similar data objects to be very close, which is very unfavorable for the identification between the objects. Therefore, the first problem to be solved is that the algorithm for obtaining the hash value of the object has relatively high requirements, and it must be able to distinguish each object accurately enough.
[0085] Furthermore, in one embodiment, step S10 may include:
[0086] Step S101, obtaining the actual length of the target data object, and performing a binary XOR operation based on the actual length to obtain a first calculation result;
[0087] The terminal device obtains the first calculation result by reading the actual length of the target data object in the business data and performing an XOR operation on the binary number in combination with the defined long integer constant.
[0088] It should be noted that, in this embodiment, in order to make the final calculation result as hashed as possible, the defined long integer constant needs to be defined large enough. For example, the long integer constant L is defined as 0xc6a4a79eebd1e995L. It should be understood that, in this embodiment, based on different design requirements of actual applications, the long integer constant can of course be defined as other values. The distributed caching method for business data of the present invention does not limit the specific value of the defined long integer constant L.
[0089] Specifically, for example, the terminal device selects a hexadecimal constant number X=0x1234ABCD and uses it as the seed value for calculating the hash value of the target data object. After the terminal device reads and obtains the actual length R of the target data object, it multiplies R and L according to the formula: X^(R*L), and then performs a binary XOR operation with the constant number X, thereby obtaining a first calculation result: r.
[0090] Further, in one embodiment, in step S101, the step of “obtaining the actual length of the target data object” may include:
[0091] Step S1011, converting the target data object into array data;
[0092] Step S1012, constructing a byte buffer object according to the array data, and reordering the byte buffer object according to a preset sorting rule;
[0093] It should be noted that in this embodiment, the preset sorting rule can specifically be: the low-order bytes are arranged at the low memory address end, and the high-order bytes are arranged at the high memory address end. It should be understood that based on different design requirements of actual applications, this preset sorting rule can of course also be defined as other sorting orders. The distributed caching method for service data of the present invention does not limit the specific sorting method of this preset sorting rule.
[0094] Specifically, for example, the terminal device converts the target data object into an array data of byte (bit) type by calling the method of the Object object (a mature existing data type conversion method), and then constructs a new byte buffer object with this array data of byte type as the underlying data, and re-sorts the constructed new byte buffer object in the order of "the low-order bytes are arranged at the low memory address end, and the high-order bytes are arranged at the high memory address end".
[0095] In this embodiment, by ii. re-sorting the array data obtained by converting the type of the target data object in the order that the low-order bytes are arranged at the low memory address end and the high-order bytes are arranged at the high memory address end, the calculation performance of the terminal device for the hash value of the target data object can be effectively improved, and it can help to avoid the problem of hash value conflicts among target data objects in service data.
[0096] Step S1013, obtain the length of the byte buffer object after re-sorting, and use the length as the actual length of the target data object.
[0097] Specifically, for example, after the terminal device finishes re-sorting the newly constructed byte buffer object, it obtains the length of the byte buffer object after re-sorting by calling the remaining method of the byte buffer object (a mature existing byte length reading method), and then determines this length as the actual length (i.e., R) of the above-mentioned target data object.
[0098] Step S102, perform an unsigned right shift operation on the first calculation result to obtain a second calculation result, and use the second calculation result as the hash value of the target data object.
[0099] After the terminal device obtains the first calculation result through exclusive OR operation of binary numbers by reading the actual length of the target data object in the service data and combining the defined long integer constant, it defines a prime number constant, and then uses this prime number to perform an unsigned right shift operation on the first calculation result to obtain a long integer second calculation result, and finally uses the second calculation result as the hash value of the target data object.
[0100] Specifically, for example, the terminal device multiplies R and L according to the formula: X^(R*L) based on the actual length R of the target data object, and then performs an exclusive OR operation of binary numbers with the constant number X to obtain the first calculation result: r. After that, the terminal device defines a prime constant T = 47 to further perform an unsigned right shift operation of binary numbers on the first calculation result: r, that is, according to the formula: r>>>T, the binary number corresponding to the first calculation result r is shifted to the right by T (47) bits, so as to obtain the second calculation result of long integer type (assumed to be long integer h) through the operation. This long integer h is the final hash value of the target data object.
[0101] Step S20, obtain the first number of machines of the cache machines caching the service data in the distributed cache system;
[0102] The terminal device obtains the first number of machines of the cache machines used for data caching and reading in the distributed cache system that caches the persistent service data of the Internet distributed service.
[0103] It should be noted that in this embodiment, the cache machine can specifically be a redis server (a high-performance key-value database, written in ANSI C language, supporting network, log-based and Key-Value database that can be based on memory or persistent, and providing APIs (Application Programming Interface) in multiple languages). The first number of machines is specifically the number of all cache machines that perform data caching and reading in the distributed system after dynamic expansion of the cache machines for the distributed system.
[0104] Step S30, perform a modulo operation according to the hash value and the first number of machines to obtain a first modulo value, so as to read the target data object from the target cache machine specified by the first modulo value.
[0105] After the terminal device calculates the hash value of the target data object and obtains the first number of machines of the cache machines in the distributed cache system, it combines the hash value and the first number of machines to perform a modulo operation to obtain the first modulo value that identifies the cache machine number storing the target data object. Thus, the terminal device can execute the read request for the target data object initiated by the client on the target cache machine specified by the first modulo value for the client to read the target data object.
[0106] Further, in an embodiment, in step S30, the step of "performing a modulo operation according to the hash value and the first number of machines to obtain a first modulo value" may include:
[0107] Step S301: Perform a binary AND operation on the hash value and a preset constant to obtain an intermediate result;
[0108] Step S302: Perform a modulo operation on the intermediate result and the first machine quantity to obtain a first modulo value.
[0109] It should be noted that in this embodiment, the preset constant is a custom hexadecimal constant, specifically: 0x1FFF. After the terminal device calculates the hash value of the target data object according to the preset algorithm, the terminal device is based on the new modulo formula: long value = (h & 0x1FFF) % n (where n still represents the number of cache machines in the distributed cache system, and % is the modulo operation), and combines the hash value and the first machine quantity to perform a modulo operation to obtain the first modulo value: value.
[0110] Specifically, for example, the terminal device follows the above new modulo formula: long value = (h & 0x1FFF) % n. First, perform a binary AND operation on the hash value h of the target data object and the custom hexadecimal constant 0x1FFF to obtain an intermediate result, and then perform a modulo operation on the intermediate result and the obtained first machine quantity n of the cache machine to obtain the value value, and this value value is the first modulo value used to identify the cache machine number storing the target data object.
[0111] The embodiment of the present invention provides a distributed caching method for service data. The terminal device optimizes the hash value algorithm for calculating each target data object in the service data to obtain a preset algorithm that can accurately distinguish each data object in the service data, and then calculates the hash value of the target data object that needs to be cached by the distributed cache system or read by the client based on this preset algorithm in the service data; moreover, the terminal device obtains the first machine quantity of the cache machines used to perform data caching and reading in the distributed cache system that caches the persistent service data of the Internet distributed service; finally, perform a modulo operation by combining the hash value of the target data object and the obtained first machine quantity to obtain the first modulo value that identifies the cache machine number storing the target data object, so that the terminal device can execute the read request initiated by the client for the target data object on the cache machine specified by the first modulo value, for the client to read the object of the target data.
[0112] The present invention performs a modulo operation by combining the hash value of the target data object and the number of cache machines to determine the cache machine number for storing the target data object, avoiding the problem that in the existing distributed caching method of business data, after the total number of cache machines in the system changes, the client cannot accurately hit the cache machine storing the data for data reading. This ensures that after the cache machines change in a distributed environment, the client can still accurately hit the cache, improving the distributed caching efficiency of business data.
[0113] In addition, during the process of calculating the hash value of the target data object and performing the modulo operation by combining the hash value and the number of cache machines, most operations are based on bit operations of binary numbers. Compared with conventional numerical calculations, the operation efficiency and performance of the terminal device are higher. Moreover, based on the optimized hash value algorithm for the target data object, the change in the calculation result is intense enough to effectively solve the problem that the hash values calculated by traditional hash algorithms for similar data objects are prone to conflicts (for example, for two strings "abc" and "abd", the ASCII value of "abd" is 1 more than that of "abc", and the results obtained by using the optimized preset algorithm in the embodiments of the present invention for the two are respectively: 1118836419 for "abc" and 413429783 for "abd". It can be seen that the difference between the two is very large, effectively avoiding the problem of hash conflicts.
[0114] Further, based on the above first embodiment, a second embodiment of the distributed caching method for business data of the present invention is proposed. Please refer to Figure 3 , Figure 3 which is a schematic flowchart of the second embodiment of the distributed caching method for business data of the present invention.
[0115] In this embodiment, before step S20 of obtaining the first number of machines of the cache machines caching the business data in the distributed caching system in the above, the distributed caching method for business data of the present invention may further include:
[0116] Step S40 of expanding the cache machines caching the business data in the distributed caching system.
[0117] The terminal device dynamically expands the cache machines that execute the requests of the client for caching or reading the target data object in the distributed system.
[0118] It should be noted that in this embodiment, in actual applications, based on different design requirements, the terminal device can also expand the cache machines before calculating the hash value of the target data object based on the above optimized preset algorithm. It should be understood that the distributed caching method for business data of the present invention does not specifically limit the timing of dynamically expanding the cache machines.
[0119] Further, in one embodiment, step S40 may include:
[0120] Step S401: Promote the backup machine corresponding to the cache machine to a new cache machine according to the sequential number of the cache machine.
[0121] Before the terminal device expands the capacity of each cache machine in the distributed cache system, it reads the respective sequential numbers of each cache machine, and then when expanding the capacity, promotes the backup machine corresponding to each cache machine to a new cache machine according to this sequential number.
[0122] Specifically, for example, please refer to Figure 6 , in the application scenario as Figure 6 shown, the terminal device expands the capacity according to 2 to the power of m (that is, expands from 2 cache machines to 4, and from 4 cache machines to 8. In this embodiment, an example is given of expanding from 2 cache machines to 4, and the expansion method for the remaining number of cache machines is similar). When the terminal device expands the capacity of the cache machine, the terminal device promotes the backup machines "0-1" and "1-1" corresponding to the cache machines numbered "0" and "1" respectively to the main cache machines according to the sequential numbers: 0, 1, 2... and re-numbers them as "2" and "3" respectively (at this time, the service data on the cache machines numbered "2" and "3" is respectively the same as the data content on the cache machines numbered "0" and "1").
[0123] Step SS402: Establish the backup machine corresponding to the new cache machine to complete the expansion of the cache machine.
[0124] After the terminal device promotes the backup machine to a new cache machine according to the sequential number, it respectively establishes the corresponding backup machine for the new cache machine, thereby completing the expansion operation of the cache machine.
[0125] Specifically, for example, in the application scenario as Figure 6 shown, after the backup machines "0-1" and "1-1" corresponding to the cache machines numbered "0" and "1" are promoted to the main cache machines "2" and "3", new backup machines "0-2" and "1-2" corresponding to the cache machines numbered "0" and "1" are re-established respectively, and for the cache machines numbered "2" and "3", their corresponding backup machines "2-1" and "3-1" are also established simultaneously. In this way, the expansion operation for the original cache machines "0" and "1" is completed.
[0126] It should be noted that, in the present embodiment, after the terminal device completes the capacity expansion for the cache machine, if at this time the client initiates a request in the distributed cache system to access again the target data object o that was cached before the capacity expansion for the cache machine (for example, the string "ABCD", the target data object has been placed in the cache machine numbered "0" for caching before the capacity expansion, because "0-1" before the capacity expansion is the backup machine of the cache machine "0", the business data in "0-1" before the capacity expansion is the same as the business data in the cache machine "0", that is, it also contains the target data object o), since the hash value of an object taken according to the same hash algorithm is unchanged, therefore, after the capacity expansion of the cache machine, the target data object o is cached again. The hash value of the target data object o is calculated by performing a modulo operation according to the formula (h&0x1FFF)%n (at this time n has changed from 2 to 4, and 4 and the hash value of the target data object o are substituted into the formula for calculation) to obtain a modulus value of 2. Therefore, at this time (after expansion), the distributed cache system will execute the request initiated by the client on the cache machine numbered "2" to obtain the target data object o (from the above expansion method, it can be known that the cache machine numbered "2" is upgraded from the original backup machine numbered "0-1", so the business data cached by the cache machine numbered "2" includes the business data of the previous cache machine numbered "0", and at this time, the target data object o can be successfully obtained from the cache machine numbered "2").
[0127] In this embodiment, before the expansion of each cache machine in the distributed cache system, the terminal device reads the sequence number of each cache machine, and then promotes the backup machine corresponding to each cache machine to the new cache machine according to the sequence number during the expansion, and establishes the corresponding backup machine for the new cache machine, thereby completing the expansion operation of the cache machine. In addition, the expansion operation is combined with the modulo operation, that is, the modulo operation is performed using the number of cache machines after the expansion and the hash value of the target data object, ensuring that the client can still accurately hit the cache after the cache machine is expanded, avoiding the problem that the client cannot accurately hit the cache machine for data reading after the total number of system cache machines changes in the existing distributed cache method of business data, thereby improving the distributed cache efficiency of business data.
[0128] Further, based on the above first and second embodiments, a third embodiment of the distributed caching method for business data of the present invention is proposed. Figure 4 , Figure 4 It is a flow chart of the third embodiment of the distributed caching method for business data of the present invention.
[0129] In this embodiment, before expanding the cache machines that cache the service data in the above-mentioned step S40, the distributed cache method for the service data of the present invention may further include:
[0130] Step S50, obtaining the second number of machines of the cache machines that cache the service data in the distributed cache system;
[0131] Before expanding the cache machines in the distributed system, the terminal device obtains the second number of machines of the cache machines used for data caching and reading in the distributed cache system.
[0132] Step S60, performing a modulo operation according to the hash value and the second number of machines to obtain a second modulo value, so as to cache the target data object on the target cache machine specified by the second modulo value.
[0133] After the terminal device obtains the second number of machines of the cache machines in the distributed cache system, it combines the calculated hash value of the target data object to perform a modulo operation to obtain a second modulo value that identifies the cache machine number storing the target data object. Thus, the terminal device can execute the cache request for the target data object initiated by the client on the cache machine identified by the second modulo value, and cache the target data object that the client needs to cache on the target cache machine specified by the second modulo value.
[0134] Specifically, for example, please refer to Figure 5 , in the application scenario shown in Figure 5 , before the terminal device expands the cache machines, there are only cache machines numbered "0" and "1" and their corresponding backup machines "0-1" and "1-1" in the distributed cache system. Assume that at this time, the distributed cache system receives a client request to cache the target data object o (assumed to be the string "ABCD") into the cache machine. Then the terminal device calculates the hash value of the string "ABCD" according to the optimized preset algorithm obtained above, and the resulting hash value is: h = -308408986493729820L (long integer). Then the terminal device performs a modulo operation according to the above formula: (h & 0x1FFF) % n (where n is the second number of machines 2 at this time) to obtain a second modulo value of 0, that is, the terminal device places the cache of the target data object o on the machine numbered "0".
[0135] In this embodiment, before the terminal device expands the cache machine in the distributed system, it obtains the second number of machines of the cache machines used to execute data caching and reading in the distributed cache system. After the terminal device obtains the second number of cache machines in the distributed cache system, it combines the calculated hash value of the target data object to perform a modulo operation to obtain a second modulo value that identifies the cache machine number storing the target data object. Thus, the terminal device can execute the cache request for the target data object initiated by the client on the cache machine specified by the second modulo value, and cache the target data object that the client needs to cache on the target cache machine specified by the second modulo value. In this way, by using the method of combining the number of cache machines with the hash value of the target data object for modulo operation, it is ensured that after the cache machine is expanded, the client can still accurately hit the cache, avoiding the problem that in the existing distributed cache method of business data, after the total number of system cache machines changes, the client cannot accurately hit the cache machine for data reading, and improving the distributed cache efficiency of business data.
[0136] The present invention also provides a distributed cache device for business data.
[0137] Referring to Figure 7 , Figure 7 is a schematic diagram of the functional modules of the first embodiment of the distributed cache device for business data of the present invention.
[0138] As Figure 7 shown, the distributed cache device for business data includes:
[0139] A hash value obtaining module 10, configured to calculate the hash value of a target data object in business data according to a preset algorithm;
[0140] An obtaining module 20, configured to obtain the first number of machines of the cache machines caching the business data in the distributed cache system;
[0141] A data reading module 30, configured to perform a modulo operation according to the hash value and the first number of machines to obtain a first modulo value, so as to read the target data object from the target cache machine specified by the first modulo value.
[0142] Further, the hash value obtaining module 10 includes:
[0143] A first calculation unit, configured to obtain the actual length of the target data object, and perform an exclusive OR operation on binary numbers based on the actual length to obtain a first calculation result;
[0144] A second calculation unit, configured to perform an unsigned right shift operation on binary numbers based on the first calculation result to obtain a second calculation result, and use the second calculation result as the hash value of the target data object.
[0145] Further, the first calculation unit includes:
[0146] A conversion unit for converting the target data object into array data;
[0147] A sorting unit for constructing a byte buffer object according to the array data and re - sorting the byte buffer object according to a preset sorting rule;
[0148] A determination unit for obtaining the length of the byte buffer object after re - sorting and using the length as the actual length of the target data object.
[0149] Further, the data reading module 30 includes:
[0150] A third calculation unit for performing a binary AND operation on the hash value and a preset constant to obtain an intermediate result;
[0151] A fourth calculation unit for performing a modulo operation on the intermediate result and the first number of machines to obtain a first modulo value.
[0152] Further, the distributed cache device for service data further includes:
[0153] An expansion module for expanding the cache machines in the distributed cache system that cache the service data.
[0154] Further, the expansion module includes:
[0155] A promotion unit for promoting the backup machine corresponding to the cache machine to a new cache machine according to the sequential number of the cache machine;
[0156] A construction unit for establishing a backup machine corresponding to the new cache machine to complete the expansion of the cache machine.
[0157] Further, the acquisition module 20 of the distributed cache device for service data is further configured to acquire the second number of machines of the cache machines in the distributed cache system that cache the service data;
[0158] The distributed cache device for service data further includes:
[0159] A data caching module for performing a modulo operation on the hash value and the second number of machines to obtain a second modulo value, so as to cache the target data object on the target cache machine specified by the second modulo value.
[0160] Among them, the functional implementation of each module in the distributed cache device for the above business data corresponds to each step in the embodiments of the distributed cache method for the above business data, and its functions and implementation processes will not be elaborated here one by one.
[0161] The present invention also provides a computer-readable storage medium, on which a distributed cache program for business data is stored. When the distributed cache program for business data is executed by a processor, it implements the steps of the distributed cache method for business data as described in any one of the above embodiments.
[0162] The specific embodiments of the computer-readable storage medium of the present invention are basically the same as those of the above embodiments of the distributed cache method for business data, and will not be elaborated here.
[0163] It should be noted that in this article, the terms "include", "comprise" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements not only includes those elements, but also includes other elements not expressly listed, or further includes elements inherent to such process, method, article or system. Without further limitation, an element defined by the statement "including a..." does not exclude the existence of additional identical elements in the process, method, article or system including the element.
[0164] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0165] Through the description of the above embodiments, those skilled in the art can clearly understand that the above embodiment methods can be implemented by means of software plus a necessary general hardware platform. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. The computer software product is stored in a storage medium as described above (such as ROM / RAM, magnetic disk, optical disk), and includes several instructions for causing a terminal device (which can be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in various embodiments of the present invention.
[0166] The above are only the preferred embodiments of the present invention, and do not limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made by using the specification and drawings of the present invention, or directly or indirectly applied in other related technical fields, shall be equally included in the patent protection scope of the present invention.
Claims
1. A distributed caching method for service data, characterized in that, the distributed caching method for service data includes: calculating the hash value of the target data object in the service data according to a preset algorithm; obtaining the first number of machines of the cache machines caching the service data in the distributed caching system; performing a modulo operation according to the hash value and the first number of machines to obtain a first modulo value, so as to read the target data object from the target cache machine specified by the first modulo value; wherein, the step of calculating the hash value of the target data object in the service data according to a preset algorithm includes: obtaining the actual length of the target data object, and performing an exclusive OR operation on binary numbers based on the actual length to obtain a first calculation result. Specifically, obtaining a long integer constant L, using the long integer constant L as the seed value for calculating the hash value of the target data object, and calculating the first calculation result according to the following formula: r = X ^ (R * L); wherein, r is the first calculation result, X is a custom constant, and R is the actual length; performing an unsigned right shift operation on binary numbers based on the first calculation result to obtain a second calculation result, and using the second calculation result as the hash value of the target data object. Specifically, obtaining a prime number constant T, shifting the binary number corresponding to the first calculation result to the right by T bits, thereby calculating the second calculation result.
2. The distributed caching method for service data according to claim 1, characterized in that, the step of obtaining the actual length of the target data object includes: converting the target data object into array data; constructing a byte buffer object according to the array data, and re - sorting the byte buffer object according to a preset sorting rule; obtaining the length of the byte buffer object after re - sorting, and using the length as the actual length of the target data object.
3. The distributed caching method for service data according to claim 1, characterized in that, the step of performing a modulo operation according to the hash value and the first number of machines to obtain a first modulo value includes: performing a binary AND operation based on the hash value and a preset constant to obtain an intermediate result; performing a modulo operation based on the intermediate result and the first number of machines to obtain a first modulo value.
4. The distributed caching method for service data according to claim 1, characterized in that, before the step of obtaining the first number of machines of the cache machines caching the service data in the distributed caching system, it further includes: expanding the cache machines caching the service data in the distributed caching system.
5. The distributed caching method for service data according to claim 4, characterized in that, the step of expanding the cache machines caching the service data in the distributed caching system includes: lifting the backup machines corresponding to the cache machines to new cache machines according to the sequential numbers of the cache machines; establishing backup machines corresponding to the new cache machines to complete the expansion of the cache machines.
6. The distributed caching method for service data according to claim 4, characterized in that, Before the step of expanding the cache machines that cache the service data in the distributed cache system, the following steps are further included: Obtain the second number of machines of the cache machines that cache the service data in the distributed cache system; Perform a modulo operation based on the hash value and the second number of machines to obtain a second modulo value, so as to cache the target data object on the target cache machine specified by the second modulo value.
7. A distributed cache device for service data Characterized in that The distributed cache device for service data includes: A hash value obtaining module, configured to calculate the hash value of a target data object in service data according to a preset algorithm; An obtaining module, configured to obtain the first number of machines of the cache machines that cache the service data in the distributed cache system; A data reading module, configured to perform a modulo operation based on the hash value and the first number of machines to obtain a first modulo value, so as to read the target data object from the target cache machine specified by the first modulo value; Wherein, in terms of calculating the hash value of the target data object in the service data according to the preset algorithm, the hash value obtaining module is specifically configured to: Obtain the actual length of the target data object, and perform an exclusive OR operation on binary numbers based on the actual length to obtain a first calculation result. Specifically, obtain a long integer constant L, use the long integer constant L as the seed value for calculating the hash value of the target data object, and calculate the first calculation result according to the following formula: r = X ^ (R * L); Wherein, r is the first calculation result, X is a custom constant, and R is the actual length; Perform an unsigned right shift operation on binary numbers based on the first calculation result to obtain a second calculation result, and use the second calculation result as the hash value of the target data object. Specifically, obtain a prime number constant T, and shift the binary number corresponding to the first calculation result to the right by T bits, so as to calculate and obtain the second calculation result.
8. A terminal device Characterized in that The terminal device includes: a memory, a processor, and a distributed cache program for service data stored on the memory and executable on the processor. When the distributed cache program for service data is executed by the processor, the steps of the distributed cache method for service data according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium Characterized in that A distributed cache program for service data is stored on the computer-readable storage medium. When the distributed cache program for service data is executed by a processor, the steps of the distributed cache method for service data according to any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Distributed data access method, device and system
CN101867607A
Distributed cache computing method based on Hash algorithm
CN108124012A
Data storage method, migration method and device
CN110287197A
Insurance policy data migration storage method, system and device and readable storage medium
CN111274228A