Transferable high-speed number generator based on Redis and relational database
Through the migable high-speed number generator based on Redis and relational databases, the problem of inflexible and difficult maintenance of number generation in the information system is solved, and flexible number generation, high-speed response and migratory number generation are realized, improving system performance and user experience.
Patent Information
- Application Number
- CN202510328113.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-19
- Publication Date
- 2025-07-25
AI Technical Summary
In the prior art In the information system, the number generator has problems such as inflexible, difficult to maintain, and unpersistent and migrated, which affects the system performance and user experience.
The migable high-speed number generator based on Redis and relational databases is adopted, combined with Redis high-speed number generation module, relational database synchronization module and numbering rule generation module, to realize flexible number generation, high-speed response and migratory numbering, and add permission authentication module to increase security.
It realizes the flexibility and security of number generation, supports permission authentication, adapts to cloud computing and containerization, provides dual backup of numbered data and cross-platform migration, and improves system performance and user experience.
Smart Images

Figure CN120371811A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer software, and relates to a migratable high-speed number generator based on Redis and a relational database, specifically a method for generating business numbers in an information-based business system with a new type of flexible and definable coding rule. Background Art
[0002] In modern information-based systems, with the continuous expansion of enterprise business and the increase in complexity, the generation and management of unique identifiers such as business document numbers and master data codes are particularly important. These numbers are not only the unique identifiers of data but also directly related to multiple aspects such as data management, retrieval, analysis, and statistics, and play an irreplaceable role in ensuring data consistency, integrity, and traceability. Therefore, developing an efficient, reliable, and migratable number generator is of great significance for improving system performance and user experience. For a number generator, the following three performance requirements need to be met:
[0003] (1) Uniqueness guarantee: In a database, each record requires a unique identifier to distinguish different data items. The number generator generates unique numbers through algorithms or rules to ensure data consistency and accuracy.
[0004] (2) Easy management: The number generator can automatically or manually generate numbers according to business requirements, greatly simplifying the work processes of data entry and management and improving work efficiency.
[0005] (3) Traceability: Through the number, information such as the data source, creation time, and modification history can be traced, providing an important basis for data analysis and auditing.
[0006] In the previous development process of information-based systems, major manufacturers have various technical implementation methods.
[0007] One is deeply bound to the business, designing a dedicated number field for each business table, and the business program specifically parses the number string to generate the next number. This method can be implemented, but it will generate a lot of repetitive code, and the parsing program is not unified according to the definition of the coding rule, and can only be customized and developed.
[0008] Another is to define a dedicated coding rule table for unified management, and use some database-specific features such as locks and auto-increment field features to ensure the uniqueness of business numbers. However, the coding rules can only be unified as much as possible within the business scope and are not flexible enough.
[0009] After popular caching products such as Redis emerged in the industry, a new approach is to use Redis auto-increment keys to generate business numbers. However, Redis is not a persistent product and cannot be saved to a persistent relational database, making it inconvenient to further provide data migration. Summary of the Invention
[0010] The purpose of the present invention is to provide a migratable high-speed number generator based on Redis and a relational database, which solves the need for information system business applications to generate unified, flexible, high-speed, and migratable numbers, addresses the pain points of scattered and difficult-to-maintain bill number generation in previous implementations, and at the same time responds quickly to reduce the impact of bill number generation on normal business. For the on-site server upgrade and migration requirements, it can also migrate the latest serial number of the number, avoiding secondary maintenance of the latest serial number. The present invention incorporates a permission authentication module, and only authorized applications can use it, increasing the security of use.
[0011] The migratable high-speed number generator based on Redis and a relational database includes a Redis high-speed number generation module, a relational database synchronization module, and a number rule generation module.
[0012] The Redis high-speed number generation module is used to atomically increase the number value through the INCR command of Redis and return the new number; the relational database synchronization module is used to synchronize the number data in Redis to the relational database at regular intervals; the Redis high-speed number generation module and the relational database synchronization module achieve double backup and cross-platform migration of the number data. The relational database synchronization module supports mainstream relational databases such as Oracle and Postgresql.
[0013] The number rule generation module, based on the WebApi technology of dotNET6+, is used to generate business codes according to the defined coding rules, supporting Http-Api services and background timing services. The number rule generation module supports flexible custom coding rules, including constant rules, time and date rules, and serial number rules. The number rule generation module supports batch code generation. The number rule generation module includes a region rule processor, a serial number rule processor, a fixed constant rule processor, and a date rule processor to achieve the generation of combined coding rules.
[0014] It also includes a permission authentication module for authenticating the permissions of applications using the number generator. The permission authentication module supports the JWT (JSON Web Token) permission authentication method and can be externally connected to a third-party authentication service according to the configuration.
[0015] This number generator supports the Http communication method for easy integration. It provides Docker containerized deployment, adapting to the development trends of cloud computing and containerization.
[0016] The process of generating and managing numbers using a migratable high-speed number generator based on Redis and a relational database is specifically as follows:
[0017] Step 1: The caller of the number generator sends the call parameters to the permission authentication module. The permission authentication module determines whether the call parameters can pass the permission authentication. If so, the parameters are passed to the number rule generation module to execute Step 2; otherwise, the permission is denied and the call process ends.
[0018] The process of the permission authentication module performing permission authentication is specifically as follows:
[0019] The permission authentication module externally connects to a permission authentication service, and the number generator comes with a default configurable application code set as the basic authentication implementation;
[0020] The caller fills the AuthToken parameter with the passed parameters. The number generator implements a simple method of allowing application codes and parsing AuthToken to support JWT permission authentication; additionally, according to the configuration, it externally connects to a third-party authentication service, and the number generator externally calls the AuthToken to a third-party authentication server.
[0021] Step 2: The number rule generation module performs encoding rule parsing and encoding rule generation, and outputs the encoding rules to the Redis high-speed number generation module;
[0022] Number rule generation module:
[0023] First, parse the Rule rule string of the passed parameters from the caller, and parse the combined rule string into an ordered array of individual rules to support the generation of codes for each individual rule in the next step, including:
[0024] Business coding rule Rule: Constant rule {S: Custom fixed coding prefix}; Time and date rule {D: yyyyMMdd}, indicating in the year-date format, following the time string format of the Microsoft system; Serial number rules include: Serial number prefix {N: {D: yyyyMMdd} / 0000}, indicating a four-digit serial number counted under the previous rule prefix + internal date prefix, and serial number prefix {NA: {D: yyyyMMdd} / 0000}, indicating a four-digit serial number counted independently by date and time.
[0025] Coding region Region: Isolate the codes between different systems to prevent confusion and conflicts.
[0026] Whether the returned code has a region ReturnWithRegion: Whether the returned code comes with a region prefix.
[0027] SpecifyBaseTime: Use the specified time as the benchmark, otherwise use the current system time as the benchmark, which affects the time encoding of the serial number.
[0028] Auth_token: Authentication and authorization are supported.
[0029] GetCount: The default value is 1, which means the number of encodings obtained at one time, and batch encoding generation is supported.
[0030] Then, the WebApi program generates business encoding keys according to the parsed different single rule types:
[0031] The system defines a unified parameter processing interface IParameterHandler, and the interface defines a void Execute(StringBuilder sb / *The only string builder for this generation operation* / , string value / *Single rule string* / ) method. That is, according to different single rule types, it respectively implements the RegionParameterHandler area rule processor, SequenceParameter serial number rule processor, ConstantParameterHandler fixed constant rule processor, and DateParameterHandler date rule processor. The date rule processor and the serial number rule processor are specifically described as follows:
[0032] DateParameterHandler: According to the given reference date or the current date, obtain the date encoding that conforms to the encoding rule of the corresponding time in accordance with the Microsoft time format.
[0033] SequenceParameter: Calculate the key of the serial number based on whether the passed parameter calculates the serial number independently, the serial number rule, and the encoding obtained in the previous step, and send it to the Redis high-speed number generation module.
[0034] Step 3: Set key-value pairs in the Redis high-speed number generation module to store the current number key. When a new number is needed, use the INCR command of Redis to obtain the next or multiple serial numbers and return the new numbers.
[0035] The number of serial numbers generated by the Redis high-speed number generation module is the number of encodings obtained at one time according to the system passed parameter.
[0036] After looping and iterating the above single rule encoding process, the encoding that meets the requirements is finally obtained, and the current key-value of this encoding has been solidified in the Redis server.
[0037] Step 4, the data in the Redis high-speed number generation module is synchronized and migrated with the relational database synchronization module regularly.
[0038] The synchronization function defines two modes: OnlyNxOverride (only overwrite non-existent keys) and Override. The number generator uses a timing service (in seconds) to synchronize the key-value data in Redis to the relational database. The number generator supports mainstream relational databases such as Oracle, Postgresql, MySql, and SqlServer.
[0039] The migration function defines two migration modes: only overwrite non-existent keys and full overwrite. The number generator synchronizes the key-value data stored in the relational database to the newly deployed Redis in reverse.
[0040] The synchronization and migration configuration structure includes:
[0041] LoadMode: The migration mode configuration includes OnlyNxOverride and Override.
[0042] DBType: The relational database type supports mainstream relational databases such as Oracle, Postgresql, MySql, and SqlServer
[0043] RedisConnString: The connection string of Redis
[0044] DBConnString: The relational database connection string
[0045] SyncMode: The synchronization mode includes OnlyNxOverride and Override
[0046] IntervalSeconds: The synchronization period time, in seconds.
[0047] The advantages of the present invention are as follows:
[0048] (1) The coding generator proposed by the present invention has a flexible combined coding rule, uses Redis to achieve high-speed response and uniqueness, and uses the migration and synchronization function to achieve the persistence and portability of the coding.
[0049] (2) The coding generator proposed by the present invention supports permission authentication (including third-party authentication);
[0050] (3) The coding generator proposed by the present invention uses the current mainstream communication method Http, which is convenient for integration;
[0051] (4) The encoding generator proposed by the present invention provides a Docker packaged image, which supports Docker containerized deployment and better supports the development of information systems in the large environment of cloud computing and containerization. Description of the Drawings
[0052] Figure 1 It is an architecture diagram of a migratable high-speed number generator based on Redis and relational database proposed by the present invention. Detailed Implementation Modes
[0053] The present invention will be further described in detail below with reference to the drawings and embodiments.
[0054] In response to the high requirements of modern information systems for number generators, the present invention proposes a business number generator that can flexibly customize rules, respond quickly, and migrate number data. This generator combines the high performance of Redis and the data persistence advantage of relational databases. It generates high-speed numbers through Redis and periodically synchronizes the generated numbers to the relational database, realizing double backup and cross-platform migration of number data. In addition, the number rule part is implemented using the high-performance WebApi program technology of dotNET6+ because the rules defined in the database are limited by the field characteristics of the relational database, while program code can achieve greater flexible coding rules.
[0055] Specifically, according to the defined encoding rule format, the number consumer sends the number rule string to the WebApi program. The WebApi program generates a business encoding key according to the encoding rule. For the part that requires a serial number, the generator first sets a key-value pair in Redis to store the current number. Whenever a new number needs to be generated, the number value is atomically incremented through the INCR command of Redis and the new number is returned. At the same time, to ensure the security of number data, the data in Redis is periodically synchronized to the relational database. When the system is migrated or upgraded, the number data can be restored from the relational database to achieve seamless migration.
[0056] In summary, the migratable high-speed number generator based on Redis and relational database realizes the rapid generation, efficient management, and reliable migration of numbers by combining the advantages of Redis and relational databases, providing strong support for modern information systems.
[0057] This number generator supports the Http communication method and is easy to integrate. It provides Docker containerized deployment and adapts to the development trends of cloud computing and containerization.
[0058] A migratable high-speed number generator based on Redis and relational databases, whose BNR (Business Number Rule) includes two major functions: BNR synchronization and migration functions, and BNR number service functions. To implement these functions, the number generator system architecture of the present invention is as Figure 1 shown, including a Redis high-speed number generation module, a relational database synchronization module, and a number rule generation module.
[0059] Among them, the permission authentication module is used to perform permission authentication on the applications using the number generator. The permission authentication module supports the JWT (JSON Web Token) permission authentication method and can be externally connected to a third-party authentication service according to the configuration.
[0060] The number rule generation module, based on the WebApi technology of dotNET6+, is used to generate business codes according to the defined coding rules, and supports Http-Api services and background timing services. The number rule generation module supports flexible custom coding rules, including constant rules, time and date rules, and serial number rules. The number rule generation module supports batch code generation. The number rule generation module includes a regional rule processor, a serial number rule processor, a fixed constant rule processor, and a date rule processor to achieve the generation of combined coding rules.
[0061] The Redis high-speed number generation module is used to atomically increase the number value through the INCR command of Redis and return the new number; the Redis high-speed number generation module and the relational database synchronization module achieve double backup and cross-platform migration of number data.
[0062] The relational database synchronization module is used to synchronize the number data in Redis to the relational database at regular intervals; the relational database synchronization module supports mainstream relational databases such as Oracle and Postgresql.
[0063] The present invention includes the following technical solutions:
[0064] (1) Technical selection: Select dotNet6 Webapi technology, including Webapi and BackgroundService, to support Http-Api services and background timing services.
[0065] (2) BNR synchronization and migration functions:
[0066] The synchronization function defines two modes: OnlyNxOverride, which only overwrites keys that do not exist, and Override mode. The generator uses a timed service (in seconds) to synchronize the key-value data of Redis to a relational database. The generator supports mainstream relational databases such as Oracle and Postgresql.
[0067] The migration function also defines two similar migration modes, namely, the mode of only overwriting keys that do not exist and the full overwrite mode. The generator can reverse-synchronize the key-value data stored in the relational database to a newly deployed Redis.
[0068] Define the configuration structure for synchronization and migration modes:
[0069]
[0070]
[0071] The meaning of the above configuration structure is as follows:
[0072] LoadMode: The migration mode configuration includes OnlyNxOverride and Override
[0073] DBType: The relational database types supported are mainstream relational databases such as Oracle, Postgresql, MySql, and SqlServer
[0074] RedisConnString: The connection string of Redis
[0075] DBConnString: The relational database connection string
[0076] SyncMode: The synchronization mode includes OnlyNxOverride and Override
[0077] IntervalSeconds: The synchronization cycle time, in seconds
[0078] (3) BNR numbering service function
[0079] This function belongs to the core function and includes the following modules:
[0080] Permission Authentication Module: It can be externally connected to a permission authentication service. The generator comes with a default set of configurable application codes as the basic authentication implementation. That is, the caller should fill in the AuthToken parameter. The generator defaults to a simple method of allowing application codes and parsing AuthToken to support JWT permission authentication. Additionally, according to the configuration, it can be externally connected to a third-party authentication service, and the generator can send the AuthToken to an external third-party authentication server.
[0081] After successful permission authentication, it enters the BNR Numbering Rule Generation Module, which is one of the most fundamental core modules.
[0082] BNR Numbering Rule Generation Module:
[0083] ① Core Call Parameters
[0084] The caller needs to fill in the core call parameters to call the generator to generate codes, which will be detailed below.
[0085]
[0086]
[0087] Business Coding Rule Rule: It supports constant rules {S: Custom Fixed Coding Prefix}, time and date rules {D: yyyyMMdd} indicating the year-date format, following the time string format of the Microsoft system, and serial number prefixes {N: {D: yyyyMMdd} / 0000}, {NA: {D: yyyyMMdd} / 0000} which respectively represent four-digit serial numbers counted with the previous rule prefix + internal date prefix and four-digit serial numbers counted independently by date and time.
[0088] Coding Region Region: Isolates the codes between different systems to prevent confusion and conflicts.
[0089] Return Coding with Region ReturnWithRegion: Whether the returned code comes with a region prefix.
[0090] Specify Base Time SpecifyBaseTime: Defaults to the system current time, which will affect the time coding of the serial number.
[0091] Authentication Authorization Auth_token: Supports authentication and authorization.
[0092] Get Coding Quantity GetCount: Defaults to 1, that is, the quantity obtained at one time, and supports batch code generation.
[0093] ② Rule Analysis Module RuleAnalysis:
[0094] This module is responsible for validating and parsing the Rule rule string passed as a parameter, and parsing the combined rule string into an ordered array of individual rules, so as to support the generation of codes for each individual rule in the next step.
[0095] ③ Single rule generation module. The system defines a unified parameter processing interface IParameterHandler, and the interface defines a method void Execute(StringBuilder sb / *The only string builder for this generation operation* / , string value / *Single rule string* / ). That is, according to different single rule types, it has respectively implemented the RegionParameterHandler regional rule processor, the SequenceParameter serial number rule processor, the ConstantParameterHandler fixed constant rule processor, and the DateParameterHandler date rule processor. Among them, the regional and constant rule processors are relatively simple. The date rule processor and the serial number rule processor are described separately below.
[0096] Date rule processor: According to the given reference date or the current date, obtain the date code that conforms to the encoding rule for the corresponding time in the Microsoft time format.
[0097] Serial number rule processor: Then calculate the key of the serial number based on whether the passed parameter calculates the serial number independently, the serial number rule, and the code obtained in the previous step, and then use commands such as INCR of Redis to obtain the next one or more serial numbers (the number of codes obtained at one time according to the system passed parameter).
[0098] After looping and iterating through the single rule processor, the required code is finally obtained, and the current key-value of this code has been solidified in the Redis server.
[0099] The configuration structure of the BNR numbering rule has one more BNRAuthOptions permission configuration option than the configuration structure of the BNR migration synchronization module.
[0100]
Claims
1. A migratable high-speed number generator based on Redis and relational databases, characterized in that, It includes a Redis high-speed number generation module, a relational database synchronization module, a numbering rule generation module, and a permission authentication module; The Redis high-speed number generation module atomically increments the number value through the INCR command of Redis and returns the new number; the relational database synchronization module is used to synchronize the number data in Redis to the relational database at regular intervals; the Redis high-speed number generation module and the relational database synchronization module achieve dual backup and cross-platform migration of the number data; The numbering rule generation module, based on the WebApi technology of dotNET6+, is used to generate business codes according to the defined coding rules, and supports Http-Api services and background scheduled services; the numbering rule generation module supports flexible customization of coding rules, including constant rules, time and date rules, and serial number rules; the numbering rule generation module supports batch code generation; the numbering rule generation module includes a region rule processor, a serial number rule processor, a fixed constant rule processor, and a date rule processor to realize the generation of combined coding rules; The permission authentication module is used to authenticate the permissions of applications using the number generator. The permission authentication module supports the JWT permission authentication method and can connect to external third-party authentication services according to the configuration; This number generator supports the Http communication method, provides Docker containerized deployment, and adapts to the development trends of cloud computing and containerization.
2. Number generation and management are performed using the migratable high-speed number generator based on Redis and relational databases according to claim 1, characterized in that The specific process is as follows: Step 1, the caller of the number generator sends the call parameters to the permission authentication module. The permission authentication module determines whether the call parameters can pass the permission authentication. If so, the parameters are passed to the numbering rule generation module to execute Step 2; otherwise, the permission is denied and the call process ends; Step 2, the numbering rule generation module performs coding rule parsing and coding rule generation, and outputs the coding rules to the Redis high-speed number generation module; The working process of the numbering rule generation module is as follows: First, parse the Rule rule string passed by the caller to parse the combined rule string into an ordered array of single rules to support the generation of codes for each single rule in the next step; Then, the WebApi program generates business code keys according to the parsed different single rule types: The system defines a unified parameter processing interface IParameterHandler, and the interface defines a void Execute(StringBuilder sb / *The only string builder for this generation operation* / , string value / *Single rule string* / ) method, that is, according to different single rule types, it respectively implements the RegionParameterHandler region rule processor, the SequenceParameter serial number rule processor, the ConstantParameterHandler fixed constant rule processor, and the DateParameterHandler date rule processor; Specific descriptions of the date rule processor and the serial number rule processor are as follows: Date Rule Processor: Obtain the date code that conforms to the coding rules for the corresponding time according to the given reference date or the current date in the Microsoft time format; Serial Number Rule Processor: Calculate the key of the serial number according to whether the passed parameter calculates the serial number independently, the serial number rule, and the code obtained in the previous step, and send it to the Redis high-speed number generation module; Step 3, set the key-value pair in the Redis high-speed number generation module to store the current number key. When a new number is needed, use the INCR command of Redis to obtain the next or multiple serial numbers and return the new number; The number of serial numbers generated by the Redis high-speed number generation module is the number of codes obtained at one time according to the system passed parameter; After looping and iterating the above single rule coding process, the required code is finally obtained, and the current key-value of this code has been solidified in the Redis server; Step 4, the data in the Redis high-speed number generation module is synchronized and migrated with the relational database synchronization module regularly; The synchronization function defines only the overwrite non-existent key mode OnlyNxOverride and the overwrite mode Override. The number generator uses a timed service to synchronize the key-value data of Redis to the relational database; The migration function defines two migration modes, namely the overwrite non-existent key mode and the full overwrite mode. The number generator synchronizes the key-value data stored in the relational database to the newly deployed Redis in reverse; 3. Number generation and management are performed by the number generator according to claim 2, characterized in that, The process of the permission authentication module for permission authentication is specifically as follows: The permission authentication module externally connects to the permission authentication service, and the number generator comes with a default configurable application code set as the basic authentication implementation; The calling party fills the AuthToken parameter in the passed parameter. The number generator implements a simple way to allow application codes and parse AuthToken to support JWT permission authentication; in addition, according to the configured external third-party authentication service, the number generator externally calls the AuthToken to the third-party authentication server.
4. Number generation and management are performed by the number generator according to claim 2, characterized in that The result obtained by parsing the number rule is expressed as: Business Coding Rule Rule: Constant Rule {S: Custom Fixed Coding Prefix}; Time and Date Rule {D: yyyyMMdd}, indicating the year-date format, following the time string format of the Microsoft system; Serial Number Rule includes: Serial Number Prefix {N: {D: yyyyMMdd} / 0000}, indicating the four-digit serial number counted by the previous rule prefix + the internal date prefix, and Serial Number Prefix {NA: {D: yyyyMMdd} / 0000}, indicating the four-digit serial number counted independently by the date and time; Coding Region Region: Isolate the codes between different systems; Return Coding with Region ReturnWithRegion: Whether the returned code comes with a region prefix; Specify Base Time SpecifyBaseTime: Use the specified time as the reference, otherwise use the system current time as the reference, affecting the time coding of the serial number; Authentication and Authorization Auth_token: Support for authentication and authorization; GetCount for obtaining the number of codes: Defaults to 1, i.e., the number obtained at one time, supports batch code generation.
5. Number generation and management are performed by the number generator according to claim 2, characterized in that The synchronization and migration configuration structure includes: LoadMode: The migration mode configuration includes OnlyNxOverride and Override; DBType: Relational database type; RedisConnString: Connection string for Redis; DBConnString: Relational database connection string; SyncMode: The synchronization mode includes OnlyNxOverride and Override; IntervalSeconds: Synchronization cycle time, in seconds.
6. Number generation and management are performed by the number generator according to claim 2, characterized in that, The above-mentioned relational databases include Oracle, Postgresql, MySql, and SqlServer.