A sequence number generation method and device, electronic equipment and storage medium

By performing incremental modulo operations on random numbers and using a remote dictionary database mutex lock mechanism, a unique sequence number is generated, which solves the problem of duplicate random numbers in multi-instance deployments and high-concurrency scenarios, ensuring the uniqueness of the sequence number and the stability of the system.

CN115934035BActive Publication Date: 2026-04-07CHINA TELECOM CORP LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-18
Publication Date
2026-04-07

AI Technical Summary

Technical Problem

In scenarios involving multiple application instances and high concurrency, the math/rand package provided by the Golang language may generate duplicate random numbers, leading to duplicate order numbers and affecting order tracking.

Method used

By obtaining the current random number and adding it to the increment value, performing a modulo operation, or by obtaining a new preset maximum value and subtracting it from the modulo operation when the random number exceeds the preset maximum value, a unique sequence number is generated by combining a remote dictionary database and a mutex lock mechanism.

Benefits of technology

In multi-instance deployments and high-concurrency scenarios, it significantly reduces the possibility of duplicate serial numbers, ensuring the uniqueness of serial numbers and system stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115934035B_ABST
    Figure CN115934035B_ABST
Patent Text Reader

Abstract

The present disclosure relates to a sequence number generation method and device, electronic equipment and storage medium, comprising: obtaining a current random number; adding the current random number and a first increment value to obtain a reference value; in the case where the reference value is less than a preset maximum value, performing a modulo operation on the reference value and the preset maximum value to obtain an updated current random number; in the case where the reference value is greater than or equal to the preset maximum value, obtaining a new preset maximum value, subtracting the new preset maximum value from a second increment value, and performing a modulo operation on the subtraction result and the new preset maximum value to obtain an updated current random number; and performing transformation processing on the updated current random number to obtain a sequence number. In this way, in the application program multi-instance deployment and high concurrency scenario, the possibility of generating repeated sequence numbers is low, which is conducive to maintaining the uniqueness of the sequence number.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present disclosure relates to the technical field of data processing, and particularly relates to a serial number generation method and device, electronic equipment and storage medium. BACKGROUND

[0002] With the vigorous development of e-commerce business, various e-commerce platforms provide cooperation business flow ability, in order to facilitate tracking of orders, it is necessary to generate a unique order number corresponding to each order which is not repeated with other orders.

[0003] In the related art, a random number can be generated first, and then the random number is processed based on a fixed format to obtain a unique order number, wherein the generation of the random number can be implemented by using a math / rand package of a Golang language, the Golang language is a programming language developed by Google company, and the math / rand package is a random number generator defined in the Golang language.

[0004] However, in the application program multi-instance deployment and high concurrency scene, the math / rand package can generate repeated random numbers, and then cause the generated order numbers to be repeated, thereby affecting the tracking of the orders. SUMMARY

[0005] The present disclosure provides a serial number generation system, method, device, electronic equipment and storage medium to at least solve the problem that in the related art, in the application program multi-instance deployment and high concurrency scene, the math / rand package can generate repeated random numbers, and then cause the generated order numbers to be repeated, thereby affecting the tracking of the orders. The technical solutions of the present disclosure are as follows:

[0006] According to a first aspect of an embodiment of the present disclosure, a serial number generation method is provided, comprising:

[0007] obtaining a current random number;

[0008] adding the current random number and a first increment value to obtain a reference value;

[0009] in a case where the reference value is less than a preset maximum value, performing a modulo operation on the reference value and the preset maximum value to obtain an updated current random number;

[0010] in a case where the reference value is greater than or equal to the preset maximum value, obtaining a new preset maximum value, subtracting a second increment value from the new preset maximum value, and performing a modulo operation on the subtraction result and the new preset maximum value to obtain an updated current random number;

[0011] performing transformation processing on the updated current random number to obtain a serial number.

[0012] Optionally, obtaining the current random number includes:

[0013] Retrieve the current random number from a remote dictionary database;

[0014] The step of obtaining a new preset maximum value when the reference value is greater than or equal to the preset maximum value includes:

[0015] If the reference value is greater than or equal to the preset maximum value, the output of the preset command function is called from the remote dictionary database as the new preset maximum value; the preset command function is used to add the preset key value to the second incremental value, update the preset key value and use it as the output result.

[0016] Optionally, obtaining the current random number from a remote dictionary database further includes:

[0017] The remote dictionary database is locked using a mutex lock.

[0018] After obtaining the updated current random number, the process also includes:

[0019] Perform mutex lock unlocking on the remote dictionary database.

[0020] Optionally, obtaining a new preset maximum value when the reference value is greater than or equal to the preset maximum value further includes:

[0021] A monitoring thread is established, which is used to acquire the output of the preset command function according to a preset period.

[0022] After the monitoring thread obtains the output result of the preset command function, it performs mutex lock processing on the output result of the preset command function;

[0023] After obtaining the updated current random number, the process also includes:

[0024] The output of the preset command function is subjected to mutex lock unlocking, and the monitoring thread is terminated.

[0025] Optionally, the method further includes:

[0026] In response to the serial number retrieval command, obtain the downgrade flag;

[0027] If the downgrade flag is a first preset value, the step of obtaining the current random number is executed;

[0028] When the downgrade identifier is set to the second preset value, a preset random number generator is invoked to generate a random number, and the generated random number is transformed to obtain a serial number.

[0029] Optionally, the method further includes:

[0030] If the current random number is not obtained, or the new preset maximum value is not obtained, the downgrade flag is modified to the second preset value, and the steps of calling the preset random number generator to generate a random number and transforming the generated random number to obtain the serial number are executed.

[0031] Until the current random number is obtained, or the new preset maximum value is obtained, the downgrade flag is modified to the first preset value.

[0032] Optionally, the step of transforming the updated current random number to obtain the sequence number includes:

[0033] Obtain user code and time parameters;

[0034] The serial number is obtained by concatenating the user code, the time parameter, and the updated current random number.

[0035] According to a second aspect of the present disclosure, a serial number generation apparatus is provided, comprising:

[0036] The get module is used to obtain the current random number;

[0037] The calculation module is used to add the current random number to the first increment value to obtain a reference value;

[0038] The first update module is used to perform a modulo operation on the reference value and the preset maximum value when the reference value is less than the preset maximum value, so as to obtain the updated current random number;

[0039] The second update module is used to obtain a new preset maximum value when the reference value is greater than or equal to the preset maximum value, subtract the new preset maximum value from the second incremental value, and perform a modulo operation on the subtraction result and the new preset maximum value to obtain the updated current random number.

[0040] The processing module is used to transform the updated current random number to obtain a sequence number.

[0041] Optionally, the acquisition module is used to:

[0042] Retrieve the current random number from a remote dictionary database;

[0043] The second update module is used for:

[0044] If the reference value is greater than or equal to the preset maximum value, the output of the preset command function is called from the remote dictionary database as the new preset maximum value; the preset command function is used to add the preset key value to the second incremental value, update the preset key value and use it as the output result.

[0045] Optionally, the device further includes:

[0046] The lock / unlock module is used to lock the remote dictionary database with a mutex lock; and after obtaining the updated current random number, to unlock the remote dictionary database with a mutex lock.

[0047] Optionally, the second update module is used for:

[0048] A monitoring thread is established, which is used to acquire the output of the preset command function according to a preset period.

[0049] After the monitoring thread obtains the output result of the preset command function, it performs mutex lock processing on the output result of the preset command function;

[0050] After obtaining the updated current random number, the output of the preset command function is subjected to mutex lock unlocking, and the monitoring thread is terminated.

[0051] Optionally, the device further includes a response module for:

[0052] In response to the serial number retrieval command, obtain the downgrade flag;

[0053] If the downgrade flag is a first preset value, the step of obtaining the current random number is executed;

[0054] When the downgrade identifier is set to the second preset value, a preset random number generator is invoked to generate a random number, and the generated random number is transformed to obtain a serial number.

[0055] Optionally, the response module is configured to:

[0056] If the current random number is not obtained, or the new preset maximum value is not obtained, the downgrade flag is modified to the second preset value, and the steps of calling the preset random number generator to generate a random number and transforming the generated random number to obtain the serial number are executed.

[0057] Until the current random number is obtained, or the new preset maximum value is obtained, the downgrade flag is modified to the first preset value.

[0058] Optionally, the processing module is configured to:

[0059] Obtain user code and time parameters;

[0060] The serial number is obtained by concatenating the user code, the time parameter, and the updated current random number.

[0061] According to a third aspect of the present disclosure, a serial number generation electronic device is provided, comprising:

[0062] processor;

[0063] Memory used to store the processor's executable instructions;

[0064] The processor is configured to execute the instructions to implement the serial number generation method described in any one of the claims.

[0065] According to a fourth aspect of the present disclosure, a computer-readable storage medium is provided, wherein when instructions in the computer-readable storage medium are executed by a processor of a serial number generating electronic device, the serial number generating electronic device is enabled to perform the serial number generating method described in any one of the claims.

[0066] According to a fifth aspect of the present disclosure, a computer program product is provided, including a computer program / instructions that, when executed by a processor, implement the serial number generation method described in any one of the present claims.

[0067] The technical solutions provided by the embodiments of this disclosure have at least the following beneficial effects:

[0068] Obtain the current random number; add the current random number to the first increment value to obtain a reference value; if the reference value is less than the preset maximum value, perform a modulo operation on the reference value and the preset maximum value to obtain the updated current random number; if the reference value is greater than or equal to the preset maximum value, obtain the new preset maximum value, subtract the new preset maximum value from the second increment value, and perform a modulo operation on the subtraction result and the new preset maximum value to obtain the updated current random number; perform a transformation process on the updated current random number to obtain the sequence number.

[0069] In this way, after obtaining the current random number, the current random number is processed by assigning a value and taking the modulo according to the range of values ​​of the random number to obtain the updated current random number. The serial number is then generated based on the updated current random number. At the same time, the range of values ​​of the random number is dynamically assigned, which makes it less likely to generate duplicate serial numbers in multi-instance deployment and high-concurrency scenarios of the application, which is conducive to maintaining the uniqueness of the serial number.

[0070] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description

[0071] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure, and are not intended to unduly limit this disclosure.

[0072] Figure 1 This is a flowchart illustrating a serial number generation method according to an exemplary embodiment.

[0073] Figure 2 This is a flowchart illustrating one implementation method according to an exemplary embodiment.

[0074] Figure 3 This is a block diagram illustrating a serial number generation apparatus according to an exemplary embodiment.

[0075] Figure 4 This is a block diagram illustrating an electronic device for serial number generation according to an exemplary embodiment.

[0076] Figure 5 This is a block diagram illustrating an apparatus for serial number generation according to an exemplary embodiment. Detailed Implementation

[0077] To enable those skilled in the art to better understand the technical solutions of this disclosure, the technical solutions in the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings.

[0078] It should be noted that the terms "first," "second," etc., used in the specification, claims, and accompanying drawings of this disclosure are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this disclosure described herein can be implemented in orders other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this disclosure. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this disclosure as detailed in the appended claims.

[0079] Figure 1 This is a flowchart illustrating a serial number generation method according to an exemplary embodiment, such as... Figure 1 As shown, the serial number generation method includes:

[0080] In step S11, the current random number is obtained.

[0081] In some scenarios, it's necessary to generate unique serial numbers and assign them to different target objects to ensure a one-to-one correspondence between the serial number and the target object. The target object can be an item, order, or production process; correspondingly, the serial number can be an item number, order number, or operation sequence number, etc., with no specific limitations. In this way, based on the uniquely corresponding serial number, the corresponding target object can be found, enabling tracking of the target object and maintaining the normal operation of the system.

[0082] In related technologies, the process of generating serial numbers involves first generating a unique random number, and then processing this random number according to a fixed format to obtain a unique serial number. While the random number can be generated using the `math / rand` package provided by the Golang language, in scenarios with multiple instances and high concurrency, the `math / rand` package is prone to generating duplicate random numbers, leading to duplicate serial numbers. Therefore, this serial number generation method needs improvement.

[0083] In this application, the current random number is first obtained. The current random number is not randomly generated, but is obtained from a preset database or storage device.

[0084] In one implementation, obtaining the current random number includes retrieving it from a remote dictionary database. The remote dictionary database is a distributed Redis (Remote Dictionary Server) database; that is, the current random number is stored in the remote dictionary database, and can be retrieved from it when a sequence number is requested to be generated.

[0085] It is understandable that, because Redis database has atomic operation characteristics, each atomic operation is indivisible and will not be interrupted by any other tasks or events before the atomic operation is completed. Therefore, obtaining the current random number from a remote dictionary database can avoid the current random number being obtained repeatedly as much as possible, thereby maintaining the uniqueness of the sequence number.

[0086] In step S12, the current random number is added to the first increment value to obtain the reference value.

[0087] In this step, the obtained current random number is added to the first increment value to obtain a reference value. The first increment value can be 1 or any other positive integer; there are no specific restrictions. The reference value is used to reassign the current random number subsequently. Each time, the current random number is added to the first increment value, so the update of the current random number gradually increases with the first increment value at intervals.

[0088] In step S13, if the reference value is less than the preset maximum value, a modulo operation is performed on the reference value and the preset maximum value to obtain the updated current random number.

[0089] The preset maximum value is used to limit the range of values ​​for the current random number. As mentioned above, the reference value is used to reassign the current random number. Therefore, the preset maximum value should also limit the range of values ​​for the reference value. In other words, the reference value should be less than the preset maximum value.

[0090] In this step, after obtaining the reference value, we can further compare the reference value with the preset maximum value. If the reference value is less than the preset maximum value, we perform a modulo operation on the reference value and the preset maximum value to obtain the updated current random number.

[0091] The modulo operation refers to finding the remainder when two numbers are divided, which is the remainder when a reference value is divided by a preset maximum value. For example, if the reference value is 1 and the preset maximum value is 1001, dividing 1 by 1001 results in a quotient of 0 and a remainder of 1, so the modulo operation yields a value of 1. Similarly, if the reference value is 1002 and the preset maximum value is 1001, dividing 1002 by 1001 results in a quotient of 1 and a remainder of 1, so the modulo operation yields a value of 1.

[0092] In this step, for cases where the reference value is less than the preset maximum value, a modulo operation is performed on the reference value and the preset maximum value. The resulting value is usually equal to the reference value, that is, the updated current random number is equal to the reference value.

[0093] However, in some abnormal situations, the judgment of the reference value and the preset maximum value may be wrong, or the reference value may be negative. If the current random number is directly assigned as the reference value, it will cause the current random number to be greater than the preset maximum value or to be negative, which will lead to the failure of serial number generation.

[0094] By performing a modulo operation on the reference value and the preset maximum value, the aforementioned situation can be avoided as much as possible. Even if the reference value is greater than the preset maximum value, or if the reference value might be negative, the value obtained after the modulo operation will still be less than the preset maximum value. This further improves the success rate of subsequent serial number generation and the stability of the system.

[0095] In step S14, if the reference value is greater than or equal to the preset maximum value, a new preset maximum value is obtained, the new preset maximum value is subtracted from the second incremental value, and a modulo operation is performed on the subtraction result and the new preset maximum value to obtain the updated current random number.

[0096] In this step, if the reference value is greater than or equal to the preset maximum value, a new preset maximum value can be obtained, and then subtracted from the second incremental value to obtain the subtraction result. The step of obtaining the new preset maximum value is similar to the step of obtaining the current random number in step S11, and can be obtained from a preset database or storage device.

[0097] In one implementation, obtaining a new preset maximum value when the reference value is greater than or equal to the preset maximum value includes: when the reference value is greater than or equal to the preset maximum value, calling the output of a preset command function from a remote dictionary database as the new preset maximum value; the preset command function is used to add the preset key value to the second increment value, update the preset key value, and use it as the output result.

[0098] The default command function can be the IncrBy command function in Redis. The IncrBy command function is used to add a specified increment value to the number stored in the default key value, that is, to add the default key value to the second increment value to get a new default key value, and use the new default key value as the output result of the IncrBy command function. In addition, the IncrBy command function can limit the output result to a 64-bit signed number representation, and has the characteristics of atomic increment and thread safety.

[0099] For example, if the default key value is 1 and the second increment value is 1000, then after calling the Incrby command function, 1 and 1000 are added together to get a new default key value of 1001, and 1001 can be output as the output of the Incrby command function. Furthermore, if the Incrby command function is called again, 1001 and 1000 are added together to get a new default key value of 2001, and 2001 can be output as the output of the Incrby command function.

[0100] Then, a modulo operation is performed on the subtraction result and the new preset maximum value to obtain the updated current random number. The step of performing the modulo operation on the subtraction result and the new preset maximum value is similar to the step of performing the modulo operation on the reference value and the preset maximum value in step S13, and will not be described again here.

[0101] In one implementation, obtaining the current random number from a remote dictionary database further includes: locking the remote dictionary database with a mutex lock; correspondingly, after obtaining the updated current random number, the implementation also includes: unlocking the remote dictionary database with a mutex lock.

[0102] Among them, the mutex (sync.Mutex) is a type of lock defined in the Golang language, used to lock shared data to ensure that only one thread can perform operations on the shared data at the same time.

[0103] Specifically, after the current thread acquires a mutex lock on the remote dictionary database, other threads will be unable to obtain the current random number from the remote dictionary database until the current thread obtains the updated current random number and unlocks the mutex lock on the remote dictionary database. Only then can other threads obtain the current random number from the remote dictionary database.

[0104] In this way, Redis's atomic operation feature is fully utilized, and in multi-threaded and multi-concurrency scenarios, the probability of different threads obtaining the same current random number is further reduced, thereby further maintaining the uniqueness of the sequence number.

[0105] In one implementation, when the reference value is greater than or equal to the preset maximum value, a new preset maximum value is obtained, which further includes: establishing a monitoring thread, which is used to obtain the output result of a preset command function according to a preset period; after the monitoring thread obtains the output result of the preset command function, the output result of the preset command function is locked by a mutex lock; further, after obtaining the updated current random number, the implementation further includes: unlocking the output result of the preset command function by a mutex lock and ending the monitoring thread.

[0106] In other words, in this step, if the reference value is greater than or equal to the preset maximum value, a monitoring thread can be established to monitor the output of the preset command function. After the output of the preset command function is monitored, a mutex lock is acquired. After the preset maximum value and the current random number are updated, the mutex lock is unlocked and the monitoring thread is terminated.

[0107] In this way, before the current thread obtains the output result of the preset command function as the new preset maximum value, the preset key value of the preset command function will not be updated repeatedly, maintaining the consistency between the preset key value and the preset maximum value. This makes the update of the preset maximum value more stable in multi-threaded and multi-concurrency scenarios, thereby further maintaining the uniqueness of the sequence number.

[0108] The monitoring thread can use heartbeat monitoring to monitor the output of the preset command function. That is, it executes the operation of obtaining the output of the preset command function according to a preset period. The preset period can be 1 second or 5 seconds, and there is no specific limitation.

[0109] In step S15, the updated current random number is transformed to obtain the sequence number.

[0110] In this application, after obtaining the updated current random number, the corresponding serial number can be obtained through further transformation processing of the updated current random number. This transformation processing can be encryption of the updated current random number, or adding other information to the updated current random number; the specific method is not limited. By transforming the updated current random number, the serial number can carry more information and has stronger uniqueness.

[0111] For example, the updated current random number is transformed to obtain the sequence number, including: obtaining the user code and time parameter; concatenating the user code, time parameter and updated current random number to obtain the sequence number.

[0112] The time parameter can be the current public time, accurate to the second or minute. For example, if the current time is 10:00 AM on November 11, 2022, the time parameter can be represented as 202211111000. Alternatively, the current Unix time can also be used as the time parameter. Unix time refers to the total number of seconds from 00:00:00 UTC on January 1, 1970 to the current time, excluding leap seconds. The user code can be a region code or a partner code, etc., without specific limitations.

[0113] In one implementation, this application further includes the following steps: in response to a serial number acquisition instruction, acquiring a downgrade flag; if the downgrade flag is a first preset value, performing the step of acquiring the current random number; if the downgrade flag is a second preset value, calling a preset random number generator to generate a random number, and transforming the generated random number to obtain the serial number. The preset random number generator can be the math / rand package provided by the Golang language, or other random number generators, and is not specifically limited.

[0114] In other words, before obtaining the current random number in step S11, in response to the serial number acquisition instruction, a downgrade flag can be obtained first. The downgrade flag is used to indicate whether the downgrade processing method needs to be used to generate the serial number. For example, the first preset value can be "false", indicating that the downgrade processing method does not need to be used at present, and the second preset value can be "true", indicating that the downgrade processing method needs to be used at present.

[0115] Therefore, if the downgrade flag is "false", the step of obtaining the current random number can be executed; if the downgrade flag is "true", then the preset random number generator can be called to generate a random number, and the generated random number can be transformed to obtain the serial number.

[0116] This makes the generation of serial numbers more flexible. In particular, if the remote dictionary database malfunctions or other steps fail, a downgraded approach can be adopted to generate serial numbers, thereby maintaining the normal operation of the system.

[0117] Specifically, the method further includes: if the current random number is not obtained or a new preset maximum value is not obtained, modifying the downgrade flag to a second preset value, and executing the step of calling a preset random number generator to generate a random number, and transforming the generated random number to obtain a serial number; until the current random number is obtained or a new preset maximum value is obtained, modifying the downgrade flag to a first preset value.

[0118] In other words, if the current random number is not obtained, or if a new preset maximum value is not obtained, the downgrade flag can be modified to the second preset value, and a downgrade processing method can be adopted. The preset random number generator is called to generate a random number, and the generated random number is transformed to obtain the serial number. Simultaneously, subsequent serial numbers will also adopt the downgrade processing method. If the current random number is obtained, or a new preset maximum value is obtained, then the downgrade flag can be modified to the first preset value, and the serial number can be generated again using the methods described in steps S11 to S15.

[0119] As can be seen from the above, the technical solution provided by the embodiments of this disclosure, after obtaining the current random number, performs processing such as assignment and modulo operation on the current random number according to the value range of the random number to obtain the updated current random number, and further generates a serial number based on the updated current random number. At the same time, the value range of the random number is dynamically assigned, so that the possibility of generating duplicate serial numbers is low in multi-instance deployment and high-concurrency scenarios of the application, which is conducive to maintaining the uniqueness of the serial number.

[0120] like Figure 2 As shown, the method in this embodiment of the invention will be described below with reference to a specific example. This embodiment can be applied to a remote dictionary database, i.e., a Redis database, for generating order numbers.

[0121] First, let's explain the initialization parameters:

[0122] Define the trigger time: Triggered when the serial number generation service starts;

[0123] Define a random number generator: Initialize the random number generator with the current Unix time as a parameter, as follows:

[0124] rd=rand.New(rand.NewSource(time.Now().UnixNano()));

[0125] Define the current random number currRandomValue as 0;

[0126] Define a preset maximum value, that is, the current maximum random number maxRandomValue is 0;

[0127] Define a downgrade flag, with an initial value of false (the first preset value).

[0128] Define the default command function as the Incrby command function, and define its second increment value delta as m;

[0129] Define the range of random numbers as [0, n].

[0130] This embodiment includes the following steps:

[0131] First, a mutex lock is used to lock Redis using the Golang language package sync.Mutex, and a delayed unlock operation is set up.

[0132] The downgrade flag is initially set to the first preset value, false. When downgrade cannot be retrieved from Redis, it will be updated to true. Conversely, when downgrade can be retrieved from Redis, it will be updated to false.

[0133] Therefore, by checking if the downgrade flag is true, the current random number generation method is switched: if it is true, the downgrade uses the preset random number generator math / rand package to generate random numbers in the range [0, n]; if it is false, the Redis Incrby command function is used to obtain the current random number.

[0134] Then, increment the current random number obtained from Redis by 1, and check if the current random number is less than the current maximum random number:

[0135] If the value is less than the defined maximum value of the random number range, the modulo of the current random number and the value of the modulo is taken and returned as the current random number.

[0136] If the value is greater than or equal to the value, then the predefined key value after incrementing by delta needs to be retrieved from Redis using the IncrBy command function;

[0137] After successfully obtaining ret, reassign the current random number currRandomValue to ret-delta, reassign the current maximum random number maxRandomValue to ret, calculate the modulo between the current random number and the maximum value of the defined random number range, and return it as a random number.

[0138] If the random number is not successfully obtained, the math / rand package will be used to generate a random number, and fallback will be enabled at the same time;

[0139] For example: if delta is set to 1000 and key is set to 1, the value ret returned by the Redis operation of adding 1000 is 1001. Then the current random number ret - delta = 1, and the range of random number values ​​is [1, 1001]. At the same time, the key is updated to 1001. When the values ​​in the range are used up, the Redis operation of adding 1000 is performed again, and the value returned is 2001. Then the range of random number values ​​is [1001, 2001], and so on.

[0140] In this embodiment, a child thread needs to be maintained as a heartbeat thread for monitoring Redis. The heartbeat thread cycles through the IncrBy command function every second until it successfully obtains the output of the IncrBy command function. Then, it locks the result with a mutex lock, assigns the current random number currRandomValue and the current maximum random number maxRandomValue successfully, and then manually releases the lock to terminate the child thread.

[0141] Then, a unique order number is generated: by concatenating the partner code, time parameter, and current random number, a unique order number is obtained.

[0142] This invention dynamically defines the range of random numbers by retrieving data from Redis, and determines the current random number. In case of emergencies such as Redis operation failure, it will use the math / rand package to generate random numbers through a fallback method. At the same time, it will add remedial measures, start a sub-thread to access Redis until Redis recovers, and then turn off the fallback method.

[0143] In this way, the order number generation method in this solution can generate the required order number efficiently and accurately, and it can be applied to a variety of scenarios. It is easy to migrate, thereby reducing the amount of code development, improving development efficiency, and reducing the difficulty of project maintenance.

[0144] Figure 3 This is a block diagram of a serial number generation apparatus according to an exemplary embodiment, comprising:

[0145] Module 201 is used to obtain the current random number;

[0146] Calculation module 202 is used to add the current random number to the first increment value to obtain a reference value;

[0147] The first update module 203 is used to perform a modulo operation on the reference value and the preset maximum value when the reference value is less than the preset maximum value, so as to obtain the updated current random number;

[0148] The second update module 204 is used to obtain a new preset maximum value when the reference value is greater than or equal to the preset maximum value, subtract the new preset maximum value from the second incremental value, and perform a modulo operation on the subtraction result and the new preset maximum value to obtain the updated current random number.

[0149] The processing module 205 is used to transform the updated current random number to obtain a sequence number.

[0150] Optionally, the acquisition module 201 is configured to:

[0151] Retrieve the current random number from a remote dictionary database;

[0152] The second update module 203 is used for:

[0153] If the reference value is greater than or equal to the preset maximum value, the output of the preset command function is called from the remote dictionary database as the new preset maximum value; the preset command function is used to add the preset key value to the second incremental value, update the preset key value and use it as the output result.

[0154] Optionally, the device further includes:

[0155] The lock / unlock module is used to lock the remote dictionary database with a mutex lock; and after obtaining the updated current random number, to unlock the remote dictionary database with a mutex lock.

[0156] Optionally, the second update module 203 is used for:

[0157] A monitoring thread is established, which is used to acquire the output of the preset command function according to a preset period.

[0158] After the monitoring thread obtains the output result of the preset command function, it performs mutex lock processing on the output result of the preset command function;

[0159] After obtaining the updated current random number, the output of the preset command function is subjected to mutex lock unlocking, and the monitoring thread is terminated.

[0160] Optionally, the device further includes a response module for:

[0161] In response to the serial number retrieval command, obtain the downgrade flag;

[0162] If the downgrade flag is a first preset value, the step of obtaining the current random number is executed;

[0163] When the downgrade identifier is set to the second preset value, a preset random number generator is invoked to generate a random number, and the generated random number is transformed to obtain a serial number.

[0164] Optionally, the response module is configured to:

[0165] If the current random number is not obtained, or the new preset maximum value is not obtained, the downgrade flag is modified to the second preset value, and the steps of calling the preset random number generator to generate a random number and transforming the generated random number to obtain the serial number are executed.

[0166] Until the current random number is obtained, or the new preset maximum value is obtained, the downgrade flag is modified to the first preset value.

[0167] Optionally, the processing module 205 is configured to:

[0168] Obtain user code and time parameters;

[0169] The serial number is obtained by concatenating the user code, the time parameter, and the updated current random number.

[0170] As can be seen from the above, the technical solution provided by the embodiments of this disclosure, after obtaining the current random number, performs processing such as assignment and modulo operation on the current random number according to the value range of the random number to obtain the updated current random number, and further generates a serial number based on the updated current random number. At the same time, the value range of the random number is dynamically assigned, so that the possibility of generating duplicate serial numbers is low in multi-instance deployment and high-concurrency scenarios of the application, which is conducive to maintaining the uniqueness of the serial number.

[0171] Figure 4 This is a block diagram illustrating an electronic device for serial number generation according to an exemplary embodiment.

[0172] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory including instructions that can be executed by a processor of an electronic device to perform the method. Optionally, the computer-readable storage medium may be a ROM, random access memory (RAM), CD-ROM, magnetic tape, floppy disk, and optical data storage device, etc.

[0173] In an exemplary embodiment, a computer program product is also provided that, when run on a computer, causes the computer to implement the method for generating the serial number.

[0174] As can be seen from the above, the technical solution provided by the embodiments of this disclosure, after obtaining the current random number, performs processing such as assignment and modulo operation on the current random number according to the value range of the random number to obtain the updated current random number, and further generates a serial number based on the updated current random number. At the same time, the value range of the random number is dynamically assigned, so that the possibility of generating duplicate serial numbers is low in multi-instance deployment and high-concurrency scenarios of the application, which is conducive to maintaining the uniqueness of the serial number.

[0175] Figure 5 This is a block diagram illustrating an apparatus 800 for serial number generation according to an exemplary embodiment.

[0176] For example, device 800 can be a mobile phone, computer, digital broadcasting electronic device, messaging device, game console, tablet device, medical device, fitness equipment, personal digital assistant, etc.

[0177] Reference Figure 5 The device 800 may include one or more of the following components: a processing component 802, a memory 804, a power component 806, a multimedia component 808, an audio component 810, an input / output (I / O) interface 812, a sensor component 814, and a communication component 816.

[0178] Processing component 802 typically controls the overall operation of device 800, such as operations associated with display, telephone calls, data communication, camera operation, and recording operations. Processing component 802 may include one or more processors 820 to execute instructions to perform all or part of the steps described. Furthermore, processing component 802 may include one or more modules to facilitate interaction between processing component 802 and other components. For example, processing component 802 may include a multimedia module to facilitate interaction between multimedia component 808 and processing component 802.

[0179] Memory 804 is configured to store various types of data to support the operation of device 800. Examples of this data include instructions for any application or method operating on device 800, contact data, phonebook data, messages, pictures, videos, etc. Memory 804 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.

[0180] Power supply component 807 provides power to various components of device 800. Power supply component 807 may include a power management system, one or more power sources, and other components associated with generating, managing, and distributing power to device 800.

[0181] Multimedia component 808 includes a screen that provides an output interface between the device 800 and the account. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touchscreen to receive input signals from the account. The touch panel includes one or more touch sensors to sense touches, swipes, and gestures on the touch panel. The touch sensors may sense not only the boundaries of the touch or swipe action but also the duration and pressure associated with the touch or swipe operation.

[0182] In some embodiments, the multimedia component 808 includes a front-facing camera and / or a rear-facing camera. When the device 800 is in an operating mode, such as a shooting mode or a video mode, the front-facing camera and / or the rear-facing camera can receive external multimedia data. Each front-facing camera and rear-facing camera can be a fixed optical lens system or have focal length and optical zoom capabilities.

[0183] Audio component 810 is configured to output and / or input audio signals. For example, audio component 810 includes a microphone (MIC) configured to receive external audio signals when device 800 is in an operating mode, such as call mode, recording mode, and voice recognition mode. The received audio signals may be further stored in memory 804 or transmitted via communication component 816. In some embodiments, audio component 810 also includes a speaker for outputting audio signals.

[0184] I / O interface 812 provides an interface between processing component 802 and peripheral interface modules, which may be a keyboard, click wheel, buttons, etc. These buttons may include, but are not limited to, a home button, volume buttons, a power button, and a lock button.

[0185] Sensor assembly 814 includes one or more sensors for providing status assessments of various aspects of device 800. For example, sensor assembly 814 can detect the on / off state of device 800, the relative positioning of components such as the display and keypad of device 800, changes in position of device 800 or a component of device 800, the presence or absence of contact between an account and device 800, orientation or acceleration / deceleration of device 800, and temperature changes of device 800. Sensor assembly 814 may include a proximity sensor configured to detect the presence of nearby objects without any physical contact. Sensor assembly 814 may also include a light sensor, such as a CMOS or CCD image sensor, for use in imaging applications.

[0186] In some embodiments, the sensor assembly 814 may also include an accelerometer, a gyroscope, a magnetometer, a pressure sensor, or a temperature sensor.

[0187] Communication component 816 is configured to facilitate wired or wireless communication between device 800 and other devices. Device 800 can access wireless networks based on communication standards, such as WiFi, carrier networks (such as 2G, 3G, 4G, or 5G), or combinations thereof. In one exemplary embodiment, communication component 816 receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel.

[0188] In one exemplary embodiment, the communication component 816 further includes a near-field communication (NFC) module to facilitate short-range communication. For example, the NFC module may be implemented based on radio frequency identification (RFID) technology, Infrared Data Association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology, and other technologies.

[0189] In an exemplary embodiment, the apparatus 800 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field-programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to perform the methods described in the first and second aspects.

[0190] In an exemplary embodiment, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 804 including instructions that can be executed by a processor 820 of the device 800 to perform the method.

[0191] Optionally, for example, the storage medium may be a non-transitory computer-readable storage medium, such as a ROM, random access memory (RAM), CD-ROM, magnetic tape, floppy disk, and optical data storage device.

[0192] In an exemplary embodiment, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to perform any of the serial number generation methods described in the embodiments.

[0193] As can be seen from the above, the technical solution provided by the embodiments of this disclosure, after obtaining the current random number, performs processing such as assignment and modulo operation on the current random number according to the value range of the random number to obtain the updated current random number, and further generates a serial number based on the updated current random number. At the same time, the value range of the random number is dynamically assigned, so that the possibility of generating duplicate serial numbers is low in multi-instance deployment and high-concurrency scenarios of the application, which is conducive to maintaining the uniqueness of the serial number.

[0194] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the following claims.

[0195] It should be understood that this disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this disclosure is limited only by the appended claims.

Claims

1. A serial number generation method, characterized in that, include: Get the current random number; Add the current random number to the first increment value to obtain a reference value; If the reference value is less than the preset maximum value, a modulo operation is performed on the reference value and the preset maximum value to obtain the updated current random number; If the reference value is greater than or equal to the preset maximum value, a new preset maximum value is obtained. The new preset maximum value is subtracted from the second incremental value. The modulo operation is performed on the subtraction result and the new preset maximum value to obtain the updated current random number. The updated random number is transformed to obtain the sequence number; The step of obtaining the current random number includes: Retrieve the current random number from a remote dictionary database; The step of obtaining a new preset maximum value when the reference value is greater than or equal to the preset maximum value includes: If the reference value is greater than or equal to the preset maximum value, the output of the preset command function is called from the remote dictionary database as the new preset maximum value; the preset command function is used to add the preset key value to the second incremental value, update the preset key value and use it as the output result.

2. The serial number generation method according to claim 1, characterized in that, The step of obtaining the current random number from the remote dictionary database also includes: The remote dictionary database is locked using a mutex lock. After obtaining the updated current random number, the process also includes: Perform mutex lock unlocking on the remote dictionary database.

3. The serial number generation method according to claim 1, characterized in that, The step of obtaining a new preset maximum value when the reference value is greater than or equal to the preset maximum value further includes: A monitoring thread is established, which is used to acquire the output of the preset command function according to a preset period. After the monitoring thread obtains the output result of the preset command function, it performs mutex lock processing on the output result of the preset command function; After obtaining the updated current random number, the process also includes: The output of the preset command function is subjected to mutex lock unlocking, and the monitoring thread is terminated.

4. The serial number generation method according to claim 1, characterized in that, The method further includes: In response to the serial number retrieval command, obtain the downgrade flag; If the downgrade flag is a first preset value, the step of obtaining the current random number is executed; When the downgrade identifier is set to the second preset value, a preset random number generator is invoked to generate a random number, and the generated random number is transformed to obtain a serial number.

5. The serial number generation method according to claim 4, characterized in that, The method further includes: If the current random number is not obtained, or the new preset maximum value is not obtained, the downgrade flag is modified to the second preset value, and the steps of calling the preset random number generator to generate a random number and transforming the generated random number to obtain the serial number are executed. Until the current random number is obtained, or the new preset maximum value is obtained, the downgrade flag is modified to the first preset value.

6. The serial number generation method according to claim 1, characterized in that, The step of transforming the updated current random number to obtain the sequence number includes: Obtain user code and time parameters; The serial number is obtained by concatenating the user code, the time parameter, and the updated current random number.

7. A serial number generation device, characterized in that, include: The get module is used to obtain the current random number; The calculation module is used to add the current random number to the first increment value to obtain a reference value; The first update module is used to perform a modulo operation on the reference value and the preset maximum value when the reference value is less than the preset maximum value, so as to obtain the updated current random number; The second update module is used to obtain a new preset maximum value when the reference value is greater than or equal to the preset maximum value, subtract the new preset maximum value from the second incremental value, and perform a modulo operation on the subtraction result and the new preset maximum value to obtain the updated current random number. The processing module is used to transform the updated current random number to obtain a sequence number; The acquisition module is used for: Retrieve the current random number from a remote dictionary database; The second update module is used for: If the reference value is greater than or equal to the preset maximum value, the output of the preset command function is called from the remote dictionary database as the new preset maximum value; the preset command function is used to add the preset key value to the second incremental value, update the preset key value and use it as the output result.

8. The serial number generation device according to claim 7, characterized in that, The device further includes: The lock / unlock module is used to lock the remote dictionary database with a mutex lock; and after obtaining the updated current random number, to unlock the remote dictionary database with a mutex lock.

9. The serial number generation device according to claim 7, characterized in that, The second update module is used for: A monitoring thread is established, which is used to acquire the output of the preset command function according to a preset period. After the monitoring thread obtains the output result of the preset command function, it performs mutex lock processing on the output result of the preset command function; After obtaining the updated current random number, the output of the preset command function is subjected to mutex lock unlocking, and the monitoring thread is terminated.

10. The serial number generation device according to claim 7, characterized in that, The device further includes a response module for: In response to the serial number retrieval command, obtain the downgrade flag; If the downgrade flag is a first preset value, the step of obtaining the current random number is executed; When the downgrade identifier is set to the second preset value, a preset random number generator is invoked to generate a random number, and the generated random number is transformed to obtain a serial number.

11. The serial number generation apparatus according to claim 10, characterized in that, The response module is used for: If the current random number is not obtained, or the new preset maximum value is not obtained, the downgrade flag is modified to the second preset value, and the steps of calling the preset random number generator to generate a random number and transforming the generated random number to obtain the serial number are executed. Until the current random number is obtained, or the new preset maximum value is obtained, the downgrade flag is modified to the first preset value.

12. The serial number generation device according to claim 7, characterized in that, The processing module is used for: Obtain user code and time parameters; The serial number is obtained by concatenating the user code, the time parameter, and the updated current random number.

13. An electronic device, characterized in that, include: processor; Memory used to store the processor's executable instructions; The processor is configured to execute the instructions to implement the serial number generation method as described in any one of claims 1 to 6.

14. A computer-readable storage medium, characterized in that, When the instructions in the computer-readable storage medium are executed by the processor of the serial number generating electronic device, the serial number generating electronic device is enabled to perform the serial number generating method as described in any one of claims 1 to 6.

15. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the serial number generation method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Distributed serial number generation method and device

    CN111949324A

  • Target task execution method and device, storage medium and electronic device

    CN114327369A