High-performance short-chain short code generation method

By defining the character set and assigning unique starting values ​​using Redis, the problems of predictability risks and hash collisions in existing short code generation algorithms are solved, and efficient, unique and safe short code generation is achieved.

CN119670691APending Publication Date: 2025-03-21BEIJING BAIJU YIXING TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411714266.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-27
Publication Date
2025-03-21

AI Technical Summary

Technical Problem

The existing short code generation algorithms have predictability risks and hash collision problems, making it difficult to ensure their uniqueness and security while efficiently generating short chains.

Method used

By defining the character set, assigning unique starting values ​​using Redis, creating BitSet to manage natural number numbers, using Canto Expand Inverse Operations and Factorial Calculation, gradually constructing and removing elements to form the final short code, and recording the number of characters occurrences and frequency of the dictionary.

Benefits of technology

Ensure that the generated short codes are consistent and predictable, reduce the possibility of collisions, enhance the recognition and uniqueness of short codes, and improve generation efficiency and operation stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119670691A_ABST
    Figure CN119670691A_ABST
Patent Text Reader

Abstract

The invention discloses a high-performance short-chain short-code generation method, which relates to the field of short-chain short-code services and comprises the following steps of: 1, defining composition of a character set, creating a CHARSET character string for assignment, recording the total number of characters by adopting a counter, and updating character count and storing a unique character by traversing the character set; according to the method, the composition of a character set is clearly defined, the length of the short code is fixed, the consistency and predictability of the generated short code are ensured, a unique initial value is allocated to each machine by utilizing an INCR command of Redis, a BitSet with the length of 10000 is created to manage a natural number sequence number, and the real-time performance of the machine is improved. A natural number serial number is effectively and systematically restored to an arrangement with a specified length n through Cantor expansion inverse operation, an index of an element is calculated by utilizing factorial assistance, so that a next element to be placed can be quickly found in a given arrangement, and calculation burden during short code generation is reduced by pre-calculating a factorial value and caching a result; the operation efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of short link and short code services, and in particular to a high-performance short link and short code generation method. Background Art

[0002] Short link services are becoming increasingly important in modern online marketing and information dissemination. Especially with the popularity of social media, e-commerce and mobile applications, the amount of information users face has increased dramatically, and the effectiveness and convenience of information processing have become the key. The emergence of short links not only simplifies the link sharing process, but also improves user experience through its concise way. With the development of technology and the diversification of user needs, a single short link generation method can no longer meet the complexity of the modern network environment.

[0003] The existing technology has the following deficiencies: The existing mainstream short code generation algorithms mainly include the self-incrementing sequence method and the summary method. The self-incrementing sequence method is simple and easy to implement. It generates short codes by gradually increasing the sequence of short links. This method is easy to track and manage, but there are certain risks, especially the predictability of the link allows potential malicious users to easily guess the next link, resulting in data leakage. The summary method uses the hash algorithm to generate short codes. Although it can effectively eliminate predictability, it itself may also have hash collisions, that is, different inputs generate the same short code, which will lead to duplication of short links and confusion in use. Therefore, how to efficiently generate short links while ensuring their uniqueness and security has become an urgent problem to be solved.

[0004] The above information disclosed in this Background section is only for enhancement of understanding of the background of the present disclosure and therefore it may contain information that does not constitute the prior art that is already known to one of ordinary skill in the art. Summary of the invention

[0005] The purpose of the present invention is to provide a high-performance short chain and short code generation method to solve the problems in the above-mentioned background technology.

[0006] In order to achieve the above object, the present invention provides the following technical solution, a high-performance short chain short code generation method, comprising the following steps:

[0007] Step 1: Define the composition of the character set and create a CHAR_SET string for assignment. Use a counter to record the total number of characters, and update the character count and store unique characters by traversing the character set.

[0008] Step 2: Connect to the Redis service and use the INCR command to assign a unique starting value and record it. Use the threading.Lock command to prevent concurrent self-increment. Create a BitSet with a length of 10,000 to manage natural number sequences. Randomly select an initial number, and use BitSet to select an unoccupied natural number sequence starting from the random initial number. Reset the BitSet when all bits are occupied.

[0009] Step 3: Use the inverse operation of Cantor expansion to restore the natural number sequence number to a permutation of length n, subtract 1 and use factorial to calculate the element index starting from 0, update the sequence number by taking the modulus, and gradually build and remove elements to form the final short code;

[0010] Step 4: Lock the character set, short code length, element index and string type during the short code generation process, use the dictionary to record the number of character occurrences and frequency to calculate the weight, pre-calculate the factorial value cache result, and implement the unified interface and factory class instantiation generation strategy through Base62ShortUrlGenerator, while handling exceptions and recording error logs.

[0011] Preferably, the character set is defined as 26 uppercase letters (AZ), 26 lowercase letters (az) and 10 numbers (0-9), the short code length is fixed to 6 bits, 6 elements are selected from the character set for arrangement, and a CHAR_SET string is created for assignment, a counter is used to record the total number of characters in the character set, and a single character category set is initialized to store unique characters, each character in the character set is traversed, and the total character count is updated and added to the corresponding short code position according to the single character category.

[0012] Preferably, connect to the Redis service, allocate and record a unique starting value for the current machine through the Redis INCR command when initializing the machine, perform an atomic addition operation on the given key through the INCR command when calling the allocation record, and return the given key new value, use the threading.Lock command to prevent concurrent self-increment from causing errors, create a BitSet with a length of 10000, initialize the BitSet initial state device to False, indicating that the BitSet is not occupied, and randomly select an initial number from 0 to 10000 each time a natural number sequence number is obtained. Using the random initial number as the starting point, select an unoccupied natural number sequence number through BitSet, and clear the initial number. The specific formula of the natural number sequence number actually used is:

[0013] M=N*10000+t

[0014] Among them, M represents the natural number sequence number actually used, N represents the random initial number, and t represents the unoccupied natural number sequence number. By checking the bitset.count() method, it is determined whether all bits in the BitSet are True. If it is determined that all bits in the BitSet are occupied, the Redis INCR command is called and the BitSet is reset to a fully unoccupied state.

[0015] Preferably, according to the inverse operation of Cantor expansion, the natural number sequence number is restored to an arrangement of length n, and a list containing all elements to be arranged is created. Since Cantor expansion starts from 0, 1 is subtracted from the given natural number sequence, and the element index to be placed at the current loop position is determined by factorial. The specific formula is:

[0016]

[0017] Among them, n i Represents the index of the element that should be placed at the current loop position, n′ represents the natural number sequence number after the given natural number sequence is minus 1, n represents the total number of elements to be arranged, i represents the index of the arrangement position currently being calculated, factorial() represents the factorial function that returns the current input value, uses the modulo operation to provide an updated natural number sequence number for the next loop position, initializes a list containing all the elements to be arranged, and starts counting from 0, adds elements to the arrangement according to the index of the element that should be placed at the current loop position, removes the used elements to execute the loop, and updates the natural number sequence number to construct the short code corresponding to the arrangement.

[0018] Preferably, the character set, short code length, element index and string type are locked in the short code generation process, and the number is converted to the short code according to the index and character set. A dictionary is created to store the number of occurrences of each character, and all generated short codes are traversed to update the number of occurrences of each character. The frequency of occurrence of each character is converted and the weight of the character set required for the short code is pre-calculated. The specific formula is:

[0019]

[0020] Among them, W(c i ) represents the weight of the character set, f(c i) represents the frequency of a character appearing in a specific short code, N represents the total frequency of all characters in the character set, the factorial value required for Cantor expansion is pre-calculated and cached, and can be directly called without repeated calculation when generating short codes, and different generation strategies for generating short codes are defined based on different character sets. A unified short code generation interface is implemented through Base62ShortUrlGenerator and each generation logic is responsible for it. The factory class is used to instantiate the generation strategy of the corresponding short code according to the character set, short code length, element index and string type in the short code generation process, and the instance is returned to integrate the error handling strategy, and the exception in the short code generation process is returned with a friendly error message and the error log is recorded.

[0021] In the above technical solution, the technical effects and advantages provided by the present invention are:

[0022] By clearly defining the composition of the character set and fixing the length of the short code, the generated short code is ensured to be consistent and predictable. A counter is used to record the total number of characters to provide a basis for fast indexing and access to characters during the generation process. By storing unique characters, it is ensured that each short code will not repeat in character selection, thereby enhancing the recognition and uniqueness of the short code. By arranging and selecting characters, the flexibility and diversity of short code generation are enhanced, and the possibility of collision is reduced. A unique starting value is assigned to each machine through the Redis INCR command to ensure that the generated natural number sequence will not be repeated in the distributed system. The threading.Lock command is used to prevent multiple threads from modifying the same resource at the same time, ensuring the correctness and stability of natural number self-increment in a high-concurrency environment. By creating a BitSet with a length of 10,000 to manage the natural number sequence, the occupancy of the sequence number is clear at a glance, improving the efficiency of querying and allocating natural numbers, and reducing the prediction of the sequence number by randomly selecting the initial number as the starting point. The testability is improved to reduce the risk of abuse. Through the inverse operation of Cantor expansion, the natural number sequence can be effectively and systematically restored to a permutation of a specified length n, making the permutation generation process simple and clear. The factorial is used to help calculate the index of the element, so that the next element to be placed in a given permutation can be quickly found, thereby improving the generation efficiency. By gradually building and removing elements, it is ensured that each element appears only once in the final permutation, thereby effectively avoiding the situation of repeated elements. By locking the character set, short code length, element index and string type, the consistency and controllability of the short code generation process are ensured, reducing potential errors and uncertainties. The dictionary is used to record the number and frequency of character occurrences, optimize the short code generation strategy, improve the randomness and distribution uniformity of the short code, and pre-calculate the factorial value and cache the result to reduce the computational burden when generating the short code, thereby improving the operating efficiency. The factory class instantiates the corresponding short code generation strategy according to different generation parameters, which is convenient for adding new strategies or modifying existing strategies in the future. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present invention. For ordinary technicians in this field, other drawings can also be obtained based on these drawings.

[0024] Figure 1 The present invention is a method flow chart of a high-performance short chain and short code generation method. DETAILED DESCRIPTION

[0025] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as limited to the examples set forth herein; rather, these example embodiments are provided so that the description of the present disclosure will be more comprehensive and complete, and the concept of the example embodiments will be fully conveyed to those skilled in the art.

[0026] Embodiment 1: The present invention provides Figure 1 A high-performance short chain and short code generation method shown includes the following steps:

[0027] Step 1: Define the composition of the character set and create a CHAR_SET string for assignment. Use a counter to record the total number of characters, and update the character count and store unique characters by traversing the character set.

[0028] The character set is defined as 26 uppercase letters (AZ), 26 lowercase letters (az) and 10 numbers (0-9), with a fixed short code length of 6 bits. Six elements are selected from the character set for arrangement, and a CHAR_SET string is created for assignment. A counter is used to record the total number of characters in the character set, and a single character category set is initialized to store unique characters. Each character in the character set is traversed, and the total character count is updated, and added to the corresponding short code position according to the single character category.

[0029] For example, if 6 elements are selected from the current character set for arrangement, and each element in this arrangement is different, there are 28.9 billion combinations: P(62,6) = 28904403600, which can meet most usage scenarios. If there are 3 elements (natural numbers) (1, 2, 3), then their permutations and combinations are: 123, 132, 213, 231, 312, 321, and their corresponding rankings are: 1, 2, 3, 4, 5, 6, then the Cantor expansion values ​​of the corresponding rankings are: 0*2! +0*1! +0*0!, 0*2! +1*1! +0*0!, 1*2! +0*1! +0*0!, 1*2! +1*1! +0*0!, 2*2! +0*1! +0*0!, 2*2! +1*1! +0*0!.

[0030] Step 2: Connect to the Redis service and use the INCR command to assign a unique starting value and record it. Use the threading.Lock command to prevent concurrent self-increment. Create a BitSet with a length of 10,000 to manage natural number sequences. Randomly select an initial number, and use BitSet to select an unoccupied natural number sequence starting from the random initial number. Reset the BitSet when all bits are occupied.

[0031] Connect to the Redis service. When initializing the machine, use the Redis INCR command to assign and record a unique starting value for the current machine. When calling the allocation record, use the INCR command to atomically add one to the given key and return the given key value. Use the threading.Lock command to prevent errors caused by concurrent self-increment. Create a BitSet with a length of 10000. Initialize the initial state device of BitSet to False, indicating that the BitSet is not occupied. Each time a natural number sequence number is obtained, randomly select an initial number from 0 to 10000. Use the random initial number as the starting point to select an unoccupied natural number sequence number through BitSet, and clear the initial number. The specific formula for the natural number sequence number actually used is:

[0032] M=N*10000+t

[0033] Among them, M represents the natural number sequence number actually used, N represents the random initial number, and t represents the unoccupied natural number sequence number. By checking the bitset.count() method, it is determined whether all bits in the BitSet are True. If it is determined that all bits in the BitSet are occupied, the Redis INCR command is called and the BitSet is reset to a fully unoccupied state.

[0034] Step 3: Use the inverse operation of Cantor expansion to restore the natural number sequence number to a permutation of length n, subtract 1 and use factorial to calculate the element index starting from 0, update the sequence number by taking the modulus, and gradually build and remove elements to form the final short code;

[0035] According to the inverse operation of Cantor expansion, the natural number sequence is restored to a length of n arrangement, and a list containing all the elements to be arranged is created. Since Cantor expansion starts at 0, the given natural number sequence is subtracted by 1, and the factorial is used to determine the element index that should be placed at the current loop position. The specific formula is:

[0036]

[0037] Among them, n iRepresents the index of the element to be placed at the current loop position, n′ represents the natural number sequence number after the given natural number sequence is minus 1, n represents the total number of elements to be arranged, i represents the index of the arrangement position currently being calculated, factorial() represents the factorial function that returns the current input element, uses the modulo operation to provide an updated natural number sequence number for the next loop position, initializes a list containing all elements to be arranged, and starts counting from 0, adds elements to the arrangement according to the index of the element to be placed at the current loop position, removes the used elements to execute the loop, and updates the natural number sequence number to construct the short code corresponding to the arrangement.

[0038] This process ensures that the generated short codes are unique and ordered by mapping natural numbers to elements in a specific character set. The application of Cantor expansion not only improves the flexibility of short code generation, but also provides adaptability to needs in different scenarios and can quickly respond to short code requests of different lengths and formats.

[0039] For example, given that the list containing all the elements to be arranged is 12345, if you need to obtain the index of the element that should be placed at the current loop position of the 107th natural number sequence, first replace 107-1. Since the Cantor expansion starts at 0, we have: 106 / 4! = 4...10, when selecting from (1, 2, 3, 4, 5), there are 4 smaller than it, so the first position of the 107th sequence is 5, 10 / 3! = 1...4, when selecting from (1, 2, 3, 4), there is 1 smaller than it, so the second position of the 107th sequence is 2, 4 / 2! = 2...0, when selecting from (1, 3, 4), there are 2 smaller than it, so the third position of the 107th sequence is 4, 0 / 1! = 0...0, when selecting from (1, 3), there is 0 smaller than it, so the fourth position of the 107th sequence is 1, 0 / 0! =0…0, when selecting from (3), there is 0 smaller than it, so the fifth bit of the 107th sequence is 3.

[0040] Step 4: Lock the character set, short code length, element index and string type during the short code generation process, use the dictionary to record the number of character occurrences and frequency to calculate the weight, pre-calculate the factorial value cache result, and implement the unified interface and factory class instantiation generation strategy through Base62ShortUrlGenerator, while handling exceptions and recording error logs;

[0041] Lock the character set, short code length, element index and string type in the short code generation process, convert numbers to short codes according to the index and character set, create a dictionary to store the number of occurrences of each character, and traverse all generated short codes to update the number of occurrences of each character, convert the frequency of occurrence of each character and pre-calculate the weight of the character set required for the short code. The specific formula is:

[0042]

[0043] Among them, W(c i ) represents the weight of the character set, f(c i ) represents the frequency of a character appearing in a specific short code, N represents the total frequency of all characters in the character set, the factorial value required for Cantor expansion is pre-calculated and cached, and can be directly called without repeated calculation when generating short codes, and different generation strategies for generating short codes are defined based on different character sets. A unified short code generation interface is implemented through Base62ShortUrlGenerator and each generation logic is responsible for it. The factory class is used to instantiate the generation strategy of the corresponding short code according to the character set, short code length, element index and string type in the short code generation process, and the instance is returned to integrate the error handling strategy, and the exception in the short code generation process is returned with a friendly error message and the error log is recorded.

[0044] This process significantly reduces the computational burden of short code generation by pre-calculating and caching factorial values. When generating each short code, the required value can be quickly obtained, thus avoiding the overhead of repeated calculations, effectively improving the performance of the overall algorithm, shortening the time of short code generation, and improving user experience.

[0045] Embodiment 2: The present invention asynchronously generates and retrieves short codes, reduces the waiting time of requests, and improves throughput. For frequent short code retrieval operations, Redis memory cache is used to store short codes and corresponding original links to reduce the access frequency of the database and improve the retrieval speed. Redis INCR command is used as a way to coordinate requests between multiple instances. Requests are distributed to multiple back-end service instances through similar load balancing technology to avoid single-point bottlenecks and improve overall processing capabilities. Possible short codes are pre-calculated and compressed, so that the short code can be returned directly to avoid repeated calculations under high concurrency conditions.

[0046] Embodiment 3: The present invention defines a fixed character set and short code length, and combines the INCR command of Redis to assign a unique natural number to each short code, thereby fundamentally avoiding the occurrence of collisions. The use of BitSet greatly reduces the frequent reading and writing of the database. In high concurrency scenarios, threading.Lock is used to ensure safe access to the counter, avoid errors caused by concurrent self-increment, and further improve stability and response speed. By recording the frequency of occurrence of each character and calculating the weight of the character set, the generated short codes are visually evenly distributed to avoid excessive use of certain characters. Flexible switching between different short code generation strategies is achieved through the factory mode, and the state management mechanism of Redis and BitSet is combined to effectively track the usage of short codes to ensure state consistency in a distributed environment, thereby solving the shortcomings of the prior art in state synchronization.

[0047] The above description is only by way of illustration of certain exemplary embodiments of the present invention. It is undoubted that those skilled in the art can modify the described embodiments in various ways without departing from the spirit and scope of the present invention. Therefore, the above drawings and descriptions are illustrative in nature and should not be construed as limiting the scope of protection of the claims of the present invention.

Claims

1. A high-performance short chain and short code generation method, characterized in that: The following steps are involved: Step 1: Define the composition of the character set and create a CHAR_SET string for assignment. Use a counter to record the total number of characters, and update the character count and store unique characters by traversing the character set. Step 2: Connect to the Redis service and use the INCR command to assign a unique starting value and record it. Use the threading.Lock command to prevent concurrent self-increment. Create a BitSet with a length of 10,000 to manage natural number sequences. Randomly select an initial number, and use BitSet to select an unoccupied natural number sequence starting from the random initial number. Reset the BitSet when all bits are occupied. Step 3: Use the inverse operation of Cantor expansion to restore the natural number sequence number to a permutation of length n, subtract 1 and use factorial to calculate the element index starting from 0, update the sequence number by taking the modulus, and gradually build and remove elements to form the final short code; Step 4: Lock the character set, short code length, element index and string type during the short code generation process, use the dictionary to record the number of character occurrences and frequency to calculate the weight, pre-calculate the factorial value cache result, and implement the unified interface and factory class instantiation generation strategy through Base62ShortUrlGenerator, while handling exceptions and recording error logs.

2. A high-performance short chain and short code generation method according to claim 1, characterized in that: In the step 1, the character set is defined as 26 uppercase letters (AZ), 26 lowercase letters (az) and 10 numbers (0-9), the short code length is fixed to 6 bits, 6 elements are selected from the character set for arrangement, and a CHAR_SET string is created for assignment, a counter is used to record the total number of characters in the character set, and a single character category set is initialized to store unique characters, each character in the character set is traversed, and the total character count is updated, and added to the corresponding short code position according to the single character category.

3. A high-performance short chain and short code generation method according to claim 1, characterized in that: In the step 2, the Redis service is connected, and when the machine is initialized, a unique starting value is allocated and recorded for the current machine through the Redis INCR command. When the allocation record is called, the given key is atomically increased by one through the INCR command, and the given key new value is returned. The threading.Lock command is used to prevent concurrent self-increment from causing errors to increase. A BitSet with a length of 10000 is created, and the initial state device of the BitSet is initialized to False, indicating that the BitSet is not occupied at all. Each time a natural number sequence number is obtained, an initial number of 0-10000 is randomly selected, and an unoccupied natural number sequence number is selected through the BitSet with the random initial number as the starting point, and the initial number is cleared. The bitset.count() method is checked to determine whether all bits of the BitSet are True. If it is determined that all bits in the BitSet are occupied, the Redis INCR command is called and the BitSet is reset to a fully unoccupied state.

4. A high-performance short chain and short code generation method according to claim 3, characterized in that: The specific formula for selecting an unoccupied natural number sequence number through BitSet starting from a random initial number and clearing the initial number in the process of actually using the natural number sequence number is: M=N*10000+t Among them, M represents the natural number sequence number actually used, N represents the random initial number, and t represents the natural number sequence number that is not occupied.

5. A high-performance short chain and short code generation method according to claim 1, characterized in that: In the step three, according to the inverse operation of Cantor expansion, the natural number sequence number is restored to an arrangement of length n, and a list containing all elements to be arranged is created. Since the Cantor expansion starts with 0, 1 is subtracted from the given natural number sequence, and the element index to be placed at the current loop position is determined by factorial, and the modulo operation is used to provide an updated natural number sequence number for the next loop position, and the list containing all elements to be arranged is initialized and counted from 0. The elements are added to the arrangement according to the element index to be placed at the current loop position, and the used elements are removed to execute the loop, and the natural number sequence number is updated to construct the short code corresponding to the arrangement.

6. A high-performance short chain and short code generation method according to claim 5, characterized in that: The specific formula for determining the element index to be placed at the current loop position using factorial is: Among them, n i Represents the index of the element that should be placed at the current loop position, n′ represents the natural number sequence number after the given natural number sequence is minus 1, n represents the total number of elements to be arranged, i represents the index of the arrangement position currently being calculated, and factorial() represents the factorial function that returns the current input value.

7. A high-performance short chain and short code generation method according to claim 1, characterized in that: In the step 4, the character set, short code length, element index and string type in the short code generation process are locked, and the number is converted to the short code according to the index and character set. A dictionary is created to store the number of occurrences of each character, and all generated short codes are traversed to update the number of occurrences of each character. The frequency of occurrence of each character is converted and the weight of the character set required for the short code is pre-calculated. The factorial value required for the Cantor expansion is pre-calculated and cached, which can be directly called when the short code is generated without repeated calculation. Different generation strategies for generating short codes based on different character set definitions are implemented, and a unified short code generation interface is implemented through Base62ShortUrlGenerator, which is responsible for each generation logic. The factory class is used to instantiate the corresponding short code generation strategy according to the character set, short code length, element index and string type in the short code generation process, and the instance is returned to integrate the error handling strategy, and the exception in the short code generation process is returned with a friendly error message and an error log is recorded.

8. A high-performance short chain and short code generation method according to claim 7, characterized in that: The specific formula for converting the occurrence frequency of each character and precalculating the weight of the character set required for the short code is: Among them, W(c i ) represents the weight of the character set, f(c i ) represents the frequency of occurrence of a character in a particular short code, and N represents the total frequency of all characters in the character set.

Citation Information

Cited By

  • Distributed short chain resource management and fault tolerance method and system based on double-layer pool architecture

    CN121233387A