Method, device, electronic device and storage medium for generating short URL
By pre-stored the current minimum and current maximum values of the identification, the problem of inefficiency of the database self-increasing identification algorithm in high concurrency scenarios is solved, and a more efficient short URL generation is achieved.
Patent Information
- Application Number
- CN202010032513.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-01-13
- Publication Date
- 2025-02-21
- Estimated Expiration
- 2040-01-13
AI Technical Summary
In the prior art, the database self-increasing identification algorithm cannot maintain the efficiency of short URL generation in high concurrency scenarios.
Generate short URLs by pre-store the current minimum and current maximum values of the identity, reducing the number of accesses to the database, and obtaining new identity from the database if necessary.
The efficiency of short URL generation is improved, especially in high concurrency scenarios, and the collision probability of generating identifiers is reduced.
Smart Images

Figure CN113111275B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method, device, electronic device and storage medium for generating a short URL. Background Art
[0002] At present, some messages are limited in the number of characters when they are sent. For example, a single short message is usually limited to 70 characters. If the content to be sent contains a long URL (assuming 50 characters), then only 20 characters are left for the valid information to be sent, which can easily be unclear, or exceed 70 characters and use 2 text messages to send. At this time, a short URL service is usually used to convert the long URL into a short URL (assuming 20 characters), thereby reducing the number of characters occupied by the long URL. The method of converting a long URL into a short URL is usually a database self-incrementing identification algorithm. For example, after the long URL is saved in the database, the database can generate a corresponding identifier for it, and return the identifier, and then generate a short URL based on the identifier to obtain the short URL corresponding to the long URL.
[0003] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:
[0004] The database self-incrementing identifier algorithm is a method for generating short URLs based on the database. Although it can minimize the collision probability of generated identifiers, it is too inefficient because it obtains identifiers for generating short URLs through the database, especially in high-concurrency scenarios, and cannot maintain the efficiency of short URL generation. Summary of the invention
[0005] In view of this, the embodiments of the present invention provide a method, device, system and storage medium for generating a short URL, which can solve the problem that the database self-increment identification algorithm obtains an identification for generating a short URL through database storage, and cannot maintain the efficiency of short URL generation in a high concurrency scenario.
[0006] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for generating a short URL is provided.
[0007] A method for generating a short URL in an embodiment of the present invention includes: when a short URL corresponding to a target long URL is not included in a storage record, determining a current minimum value and a current maximum value of a pre-stored identifier, wherein the pre-stored identifier is used to generate a short URL; if the current minimum value is less than or equal to the current maximum value, generating a target short URL according to an identifier of the current minimum value, and updating the current minimum value; and determining the target short URL as the short URL of the target long URL.
[0008] In one embodiment, determining the target short URL as the short URL of the target long URL includes:
[0009] Query whether the storage record includes the target short URL;
[0010] If not, the target short URL is determined as the short URL of the target long URL;
[0011] If so, the current minimum value is updated, and when the updated current minimum value is less than the current maximum value, the target short URL is regenerated according to the updated current minimum value, and the recalculated target short URL is generated as the short URL of the target long URL.
[0012] In yet another embodiment, generating a target short URL according to the identifier of the current minimum value includes:
[0013] Generate a code corresponding to a preset base according to the identifier of the current minimum value;
[0014] The code corresponding to the preset base is determined as the target short URL.
[0015] In yet another embodiment, if the current minimum value is not less than the maximum value, the method further includes:
[0016] Get the new minimum value of the identifier used to generate the short URL from the database;
[0017] Determine a new maximum value of an identifier for generating a short URL according to the new minimum value and a preset step size;
[0018] Determine whether the new minimum value and the new maximum value belong to a preset coding range, where the preset coding range is determined according to a preset base system and a preset length of the short URL;
[0019] If yes, then update the new minimum value to the current minimum value, and update the new maximum value to the current maximum value;
[0020] A target short URL is generated according to the identifier of the current minimum value, and the target short URL is determined as the short URL of the target long URL.
[0021] In yet another embodiment, if the new minimum value and the new maximum value do not belong to the preset coding range, the method further includes:
[0022] Sending a reset instruction to the database so that the database is initialized according to the reset instruction;
[0023] A new minimum value of an identifier for generating a short URL is received from the database.
[0024] In yet another embodiment, after determining the target short URL as the short URL of the target long URL, the method further includes:
[0025] Storing a mapping relationship between the target long URL and the target short URL in a storage record;
[0026] Setting a storage period for the mapping relationship between the target long URL and the target short URL;
[0027] When the storage time of the mapping relationship reaches the storage period, the mapping relationship between the target long URL and the target short URL is deleted.
[0028] In yet another embodiment, determining the current minimum value and the current maximum value of the pre-stored identifier includes:
[0029] Determine whether the number of short URLs generated within a preset time period reaches a threshold;
[0030] If not, then determine the current minimum value and the current maximum value of the pre-stored identifier.
[0031] To achieve the above objective, according to another aspect of the present invention, a device for generating a short URL is provided.
[0032] A device for generating a short URL of the present invention comprises: a determination unit, which is used to determine the current minimum value and the current maximum value of a pre-stored identifier when the short URL corresponding to the target long URL is not included in the storage record, and the pre-stored identifier is used to generate the short URL; a generation unit, which is used to generate the target short URL according to the identifier of the current minimum value if the current minimum value is less than or equal to the current maximum value, and update the current minimum value; the determination unit is also used to determine the target short URL as the short URL of the target long URL.
[0033] In one embodiment, the determining unit is specifically configured to:
[0034] Query whether the storage record includes the target short URL;
[0035] If not, the target short URL is determined as the short URL of the target long URL;
[0036] If so, the current minimum value is updated, and when the updated current minimum value is less than the current maximum value, the target short URL is regenerated according to the updated current minimum value, and the recalculated target short URL is generated as the short URL of the target long URL.
[0037] In yet another embodiment, the generating unit is specifically configured to:
[0038] Generate a code corresponding to a preset base according to the identifier of the current minimum value;
[0039] The code corresponding to the preset base is determined as the target short URL.
[0040] In yet another embodiment, if the current minimum value is not less than the maximum value, the method further includes:
[0041] An acquiring unit, used for acquiring a new minimum value of an identifier used for generating a short URL from a database;
[0042] The determining unit is further used to determine a new maximum value of an identifier for generating a short URL according to the new minimum value and a preset step size;
[0043] A judging unit, used to judge whether the new minimum value and the new maximum value belong to a preset coding range, wherein the preset coding range is determined according to a preset base system and a preset length of the short URL;
[0044] an updating unit, configured to update the new minimum value to the current minimum value and update the new maximum value to the current maximum value if yes;
[0045] The generating unit is specifically configured to generate a target short URL according to the identifier of the current minimum value;
[0046] The determining unit is specifically configured to determine the target short URL as the short URL of the target long URL.
[0047] In yet another embodiment, if the new minimum value and the new maximum value do not belong to the preset coding range, the method further includes:
[0048] A sending unit, used for sending a reset instruction to the database, so that the database is initialized according to the reset instruction;
[0049] The receiving unit is further used to receive a new minimum value of an identifier for generating a short URL sent by the database.
[0050] In yet another embodiment, further comprising:
[0051] A storage unit, used for storing a mapping relationship between the target long URL and the target short URL in a storage record;
[0052] A setting unit, used to set a storage period of the mapping relationship between the target long URL and the target short URL;
[0053] The deleting unit is used to delete the mapping relationship between the target long URL and the target short URL when the storage time of the mapping relationship reaches the storage period.
[0054] In yet another embodiment, the determining unit is specifically configured to:
[0055] Determine whether the number of short URLs generated within a preset time period reaches a threshold;
[0056] If not, then determine the current minimum value and the current maximum value of the pre-stored identifier.
[0057] To achieve the above objective, according to another aspect of the present invention, a device for generating a short URL is provided.
[0058] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device is provided.
[0059] An electronic device according to an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the method for generating a short URL provided by the embodiment of the present invention.
[0060] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer readable medium is provided.
[0061] A computer-readable medium according to an embodiment of the present invention stores a computer program, and when the program is executed by a processor, the method for generating a short URL provided by an embodiment of the present invention is implemented.
[0062] One embodiment of the above invention has the following advantages or beneficial effects: In the embodiment of the present invention, when the short URL corresponding to the target long URL is not included in the storage record, the current minimum value and the current maximum value of the pre-stored identifier are determined; when it is determined that the current minimum value is less than the current maximum value, the target short URL can be generated according to the identifier of the current minimum value, and the target short URL is determined as the short URL of the target long URL. In the embodiment of the present invention, the target short URL is generated by pre-stored identifiers, so that the number of accesses to the database can be reduced, and the efficiency of short URL generation can be improved while reducing the collision probability of generated identifiers, especially improving the efficiency of short URL generation in high concurrency scenarios.
[0063] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS
[0064] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0065] Figure 1 is a schematic diagram of a main process of a method for generating a short URL according to an embodiment of the present invention;
[0066] Figure 2 is a schematic diagram of another main process of the method for generating a short URL according to an embodiment of the present invention;
[0067] Figure 3is a schematic diagram of main units of a device for generating a short URL according to an embodiment of the present invention;
[0068] Figure 4 is another exemplary system architecture diagram to which the embodiments of the present invention can be applied;
[0069] Figure 5 It is a schematic diagram of the structure of a computer system suitable for implementing the embodiment of the present invention. DETAILED DESCRIPTION
[0070] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0071] It should be pointed out that the embodiments of the present invention and the features in the embodiments may be combined with each other if there is no conflict.
[0072] The method for generating a short URL in an embodiment of the present invention can be used in a distributed system, and the distributed system includes multiple nodes and a database connected to each node. The database can be a cache database, such as redis, and each node can obtain an identification segment for generating a short URL from the database and store it in the memory. Each node can use the identification stored in the memory to generate a short URL in sequence starting from the current minimum value of the identification, thereby ensuring the uniqueness of the short URL generation. The identification position and the identification increment step can be stored in the database, the identification position indicates the current generation position of the identification used to generate the short URL, and the identification increment step indicates the length of the identification obtained by the node each time, that is, the range of the identification segment obtained by each node from the database each time is the value of the identification position to the identification position plus the identification increment step. The database receives the acquisition instruction of the node and can return the identification position to the node as the new current minimum value of the identification in the node, indicating that the node can use the identification between the value of the identification position and the identification position plus the identification increment step to generate a short URL, and then the database stores the identification position plus the identification increment step as the new identification position, so as to return the new identification position after the next node request.
[0073] Since it is a distributed system, multiple nodes may obtain identifiers from the database at the same time. The database can determine the location identifier returned to each node through minId=incrby(key, by), where minId represents the new minimum value returned to the node, by represents the identifier increment step, and key represents the location identifier stored in the database.
[0074] In an embodiment of the present invention, in order to avoid generating a short URL for the same long URL, each node can store the correspondence between the generated short URL and the corresponding long URL, for example, as a storage record in a database. Each time each node needs to convert a long URL into a short URL, it can query the storage record in advance. If the storage record includes the correspondence between the long URL and the corresponding short URL, it means that the storage record includes the short URL corresponding to the long URL, and the short URL corresponding to the long URL can be directly obtained from the storage record; if the storage record does not include the short URL corresponding to the long URL, it is necessary to generate a short URL in the manner of an embodiment of the present invention.
[0075] Since the number of identifiers is limited, the identifiers will be used cyclically to generate short URLs. Therefore, in the embodiment of the present invention, a storage period for the corresponding relationship between each long URL and short URL in the storage record can be set. For example, the storage period is set to 10 days. When the storage time of the corresponding relationship between the long URL and the short URL reaches the storage period, the corresponding relationship between the long URL and the short URL is deleted, so that these short URL corresponding identifiers can be reused to generate short URLs corresponding to other long URLs.
[0076] Since the number of identifiers used to generate short URLs is limited, each time the identifiers are exhausted, the identifiers can be reused to generate short URLs by initializing the identifier position in the database. However, the storage record stores the corresponding relationship between the generated short URLs and the corresponding long URLs. In order to avoid the identifiers from being reused, the usage time of the identifiers can be set, that is, only the identifiers in the preset range can be used within the preset time period to ensure that when these identifiers are cyclically used, the storage period for generating short URLs based on them has expired, that is, they have been deleted. For example, the usable identifier range is set to 0 to N, the storage period is 10 days, and the usable identifier range per day can be evenly distributed to N, so that the usable identifier range per day is greater than 0 and less than or equal to N / 10, greater than N / 10 and less than or equal to 2N / 10, greater than 2N / 10 and less than or equal to 3N / 10, ..., greater than 9N / 10 and less than or equal to N. In this way, when the database is initialized after the 10th day, the short URLs generated by the identifiers in the range greater than 0 and less than or equal to N / 10 have been deleted, thereby avoiding the reuse of identifiers.
[0077] Since the usage time of the identifier is set, it is possible to determine whether a short identifier can still be generated based on the number of identifiers used within a preset time period. In an embodiment of the present invention, before generating a short URL based on the identifier, it can be determined whether the number of short URLs generated within a set time period reaches a threshold, and the threshold represents the number of identifiers used within a preset time period. If not, a short URL can be generated and step S101 is executed; if yes, the embodiment of the present invention is not executed to generate a short URL.
[0078] The embodiment of the present invention provides a method for generating a short URL, which can be executed by a node in a distributed system, such as Figure 1 As shown, the method includes the following steps.
[0079] S101: When the short URL corresponding to the target long URL is not included in the storage record, determine the current minimum value and the current maximum value of the pre-stored identifier.
[0080] The pre-stored identifier is used to generate a short URL, indicating an identifier pre-stored in the node that can be used to generate a short URL.
[0081] When a node needs to convert a long URL into a short URL, it can first query the storage record, which stores the correspondence between the generated short URL and the corresponding long URL. Therefore, if the storage record includes the correspondence between the target long URL and the corresponding short URL, it means that the storage record includes the short URL corresponding to the target long URL, and the short URL corresponding to the target long URL can be directly obtained from the storage record; if the storage record does not include the short URL corresponding to the target long URL, it is necessary to generate a short URL in the manner of the embodiment of the present invention.
[0082] When the storage record does not include the short URL corresponding to the target long URL, it indicates that a short URL needs to be generated. In an embodiment of the present invention, the short URL is generated by the identifier pre-stored in the node. This step first determines the current minimum value and the current maximum value of the pre-stored identifier.
[0083] The current minimum value and current maximum value of the pre-stored identifier represent the minimum value and maximum value of the current identifier range that can be used to generate short URLs pre-stored in the node's memory. For example, if the stored identifiers are 1050 to 1999, the current minimum value is 1050 and the current maximum value is 1999.
[0084] S102: If the current minimum value is less than the current maximum value, a target short URL is generated according to an identifier of the current minimum value, and the current minimum value is updated.
[0085] The identifiers pre-stored in the node memory are used to generate short URLs in order starting from the current minimum value. Each time after generating a short URL, the current minimum value can be updated by increasing the preset value. Therefore, when the pre-stored identifiers are exhausted, the updated current minimum value will be greater than the current maximum value. Therefore, when it is necessary to generate a short URL again, after determining the current minimum value and the current maximum value of the pre-stored identifiers in step S101, first determine whether the current minimum value is greater than the current maximum value. If the current minimum value is not greater than the current maximum value, it means that the identifiers pre-stored in the memory have not been exhausted, and the target short URL can be generated directly according to the identifier of the current minimum value. If the current minimum value is greater than the current maximum value, it means that the identifiers pre-stored in the memory have been exhausted. At this time, it is necessary to re-acquire the identifiers used to generate short URLs from the database for storage.
[0086] In the embodiment of the present invention, the method of generating the target short URL according to the identifier of the current minimum value can be specifically as follows: generating a code corresponding to a preset base according to the identifier of the current minimum value; and determining the code corresponding to the preset base as the target short URL.
[0087] In the embodiment of the present invention, a code in a preset base may be used as a short URL, so the identifier of the current minimum value may be converted into a code in a preset base to generate a target short URL.
[0088] For example, the preset base may be base 62, and the base 62 character table is: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST UVWXYZ (62 characters in total). Assuming the number of digits of the short URL is 2, the identifier of the current minimum value may be converted into a 2-digit code of base 62 as the short URL in this step.
[0089] After generating the target short URL according to the identifier of the current minimum value, the current minimum value can also be updated by increasing the current minimum value by a preset value, so that the next time a short URL is generated, it can be determined whether the current minimum value is greater than the current maximum value, where the preset value can be 1.
[0090] In an embodiment of the present invention, when the current minimum value is greater than the current maximum value, it is necessary to re-acquire the identifier for generating a short URL from the database and generate a target short URL. The specific method may be: acquiring a new minimum value for the identifier for generating a short URL from the database; determining a new maximum value for the identifier for generating a short URL based on the new minimum value and a preset step length; determining whether the new minimum value and the new maximum value belong to a preset coding range, the preset coding range being determined based on a preset base and a preset length of the short URL; if so, updating the new minimum value to the current minimum value and the new maximum value to the current maximum value; generating a target short URL based on the identifier of the current minimum value, and determining the target short URL as the short URL of the target long URL.
[0091] When the current minimum value of the node is greater than the current maximum value, the node can send an instruction to obtain the identifier to the database, and then the database returns the new minimum value of the identifier used to generate the short URL, that is, the node obtains the new minimum value of the identifier used to generate the short URL from the database. The node sets a preset step length that is the same as the value of the database identifier increment step length, and then the new maximum value corresponding to the new minimum value can be determined by increasing the value of the new minimum value by the preset step length and then subtracting the preset value. After determining the new minimum value and the new maximum value in this way, the identifier range obtained from the database for generating the short URL can be determined.
[0092] In the embodiment of the present invention, the identifier is converted into a preset base to generate a short URL, and only the identifier within the preset coding range can be successfully converted. Therefore, after determining the new minimum value and the new maximum value, it is also necessary to determine whether the new minimum value and the new maximum value belong to the preset coding range, wherein the preset coding range is determined according to the preset base and the preset length of the short URL.
[0093] If it is determined that the new minimum value and the new maximum value belong to the preset coding range, it means that the identifier obtained from the database can be used for short URL generation, and the new minimum value can be updated to the current minimum value, and the new maximum value can be updated to the current maximum value. Since the current minimum value and the current maximum value are both the identification range newly obtained from the database, it is possible to directly generate the target short URL according to the identifier of the current minimum value, update the current minimum value, and determine the target short URL as the short URL of the target long URL.
[0094] In the above process, if it is determined that the new minimum value and the new maximum value do not belong to the preset coding range, it means that the identifier generated in the database has exceeded the preset coding range. At this time, the database can be initialized, that is, the identifier position in the database is initialized to the initial value. It can be specifically performed as follows: sending a reset instruction to the database so that the database is initialized according to the reset instruction; receiving the new minimum value of the identifier sent by the database for generating a short URL.
[0095] After the node sends a reset instruction to the database, the database can directly return the new minimum value of the identifier used to generate the short URL after performing initialization.
[0096] It should be noted that, when each node is initialized, the current minimum value of the identifier may be set to be greater than the current maximum value, so that after initialization, the node directly obtains the identifier used to generate the short URL from the database for storage.
[0097] S103: Determine the target short URL as the short URL of the target long URL.
[0098] After the short URL is generated, in the embodiment of the present invention, the target short URL may be determined as the short URL of the target long URL.
[0099] In order to avoid the reuse of identifiers, this step can also be specifically performed as follows: query whether the target short URL is included in the storage record; if not, determine the target short URL as the short URL of the target long URL; if so, update the current minimum value, and when the updated current minimum value is less than the current maximum value, regenerate the target short URL according to the updated current minimum value, and generate the recalculated target short URL as the short URL of the target long URL.
[0100] After the target short URL is generated in step S102, the storage record can be queried. If the target short URL is included in the storage record, it means that the target short URL is still in use. To avoid duplication, the short URL needs to be regenerated at this time, that is, the current minimum value is updated, and when the updated current minimum value is less than the current maximum value, the target short URL is regenerated according to the updated current minimum value, and the recalculated target short URL is generated as the short URL of the target long URL. If the target short URL is not included in the storage record, it means that the target short URL is not in use, and the target short URL can be directly determined as the short URL of the target long URL.
[0101] After executing this step, the mapping relationship between the target long URL and the target short URL can also be stored in the storage record, and a storage period can be set for it. When the storage period reaches the storage period, it can be deleted.
[0102] In an embodiment of the present invention, when the short URL corresponding to the target long URL is empty in the storage record, the current minimum value and the current maximum value of the pre-stored identifier are determined; when it is determined that the current minimum value is less than the current maximum value, the target short URL can be generated according to the identifier of the current minimum value, and the target short URL is determined as the short URL of the target long URL. In an embodiment of the present invention, the target short URL is generated by pre-stored identifiers, so that the number of accesses to the database can be reduced, and the efficiency of short URL generation can be improved while reducing the collision probability of generated identifiers, especially improving the efficiency of short URL generation in high concurrency scenarios.
[0103] It should be noted that, in the embodiment of the present invention, the preset step length and the preset value can be set according to the requirements. For example, the preset step length can be set to 1000, and the preset value is 1, then each node will access the database once for every 1000 short URLs generated, so the performance is 1000 times faster than the traditional database algorithm. In the embodiment of the present invention, the number of digits of the short URL can also be set according to the requirements, for example, the short URL is set to 5 62-bit encoding, and the encoding position of the short URL is 10000-ZZZZZ.
[0104] Combination Figure 1 The embodiment shown in the figure specifically describes the method for generating a short URL in the embodiment of the present invention. Figure 2As shown, the method includes the following steps.
[0105] S201: Initialize the current minimum value and the current maximum value of the pre-stored identifier.
[0106] Among them, when the node initially executes the embodiment of the present invention, the current minimum value and the current maximum value of the pre-stored identifier can be initialized. During initialization, there are usually no pre-stored identifiers in the node memory, so the current minimum value of the identifier can be set to be greater than the current maximum value, so that the node directly obtains the identifier used to generate the short URL from the database for storage after initialization. For example, the current minimum value is represented by minId, and the initialization value is -1, and the current maximum value is represented by maxId, and the initialization value is -2. In this way, if minId is greater than maxId, the process of obtaining the identifier from the database will be executed. The initialization in the node can also include a preset step size that is the same as the value of the database identifier increment step size.
[0107] S202: Determine whether the current minimum value is greater than the current maximum value. If so, execute step S203; if not, execute step S206.
[0108] If the current minimum value is greater than the current maximum value, it means that the current stored identifiers have been used up and there are no available identifiers, and it is necessary to obtain identifiers from the database. If the current minimum value is not greater than the current maximum value, it means that the current stored identifiers are not used up and there are available identifiers, and step S206 can be directly executed.
[0109] S203: Obtain a new minimum value from the database, and determine a new maximum value according to the new minimum value.
[0110] When a node needs to obtain an identifier from a database, it can send an acquisition instruction to the database, and the database will return the new minimum value of the available identifier, and then determine the new maximum value according to the preset step size. The node can set a preset step size that is the same as the value of the database identifier increment step size, and then increase the value of the new minimum value by the preset step size and then subtract the preset value to determine the new maximum value corresponding to the new minimum value.
[0111] For example, the preset step size is 1000, the new minimum value is 2000, and the preset value is 1, then it can be determined that the new maximum value is 2000+1000-1, which is equal to 2999.
[0112] S204: Determine whether the new minimum value and the new maximum value are within a preset range, if so, execute step S205; if not, execute step S210.
[0113] Only identifiers within the preset coding range can be successfully converted, so after determining the new minimum value and the new maximum value, it is also necessary to determine whether the new minimum value and the new maximum value belong to the preset coding range. The preset coding range is determined according to the preset base and the preset length of the short URL. For example, the short URL is a 2-bit 62-base encoding, that is, the value of the short URL is from 10 to ZZ, then its corresponding decimal value range is 62-3843, so the decimal identifier needs to be within the range of 62-3843 to successfully convert the short URL, so the preset range is 62-3843, that is, the new minimum value needs to be no less than 62, and the new maximum value needs to be no greater than 3843.
[0114] S205: Update the new minimum value to the current minimum value, and update the new maximum value to the current maximum value.
[0115] If it is determined that the new minimum value and the new maximum value belong to the preset coding range, it means that the identifier obtained from the database can be used for short URL generation. The new minimum value can be updated to the current minimum value, and the new maximum value can be updated to the current maximum value for subsequent use in generating short URLs.
[0116] S206: Generate a target short URL according to the identifier of the current minimum value.
[0117] The short URL is generated by converting the identifier of the current minimum value into a code of a preset base. For example, if the short URL is set to a 2-digit base 62 code and the identifier of the current minimum value is 62, the generated target short identifier is 10.
[0118] S207: Increment the current minimum value by a preset value.
[0119] After the short URL is generated using the current preset value, the current minimum value may be incremented by the preset value to indicate an identifier used for the next generation of the short URL.
[0120] S208: Determine whether the target short URL is included in the storage record, if so, execute step S202; if not, execute step S209.
[0121] The storage record stores the generated short URL and the corresponding long URL. After generating the target short URL, in order to avoid duplication, it can be determined whether the target short URL is included in the storage record. If it is included, it is necessary to regenerate the target short URL and execute step S202; if not, it can directly execute step S209.
[0122] S209: Determine the target short URL as the short URL corresponding to the target long URL, and add the corresponding relationship between the target long URL and the target short URL in the storage record.
[0123] If the target short URL is available, the target short URL can be determined as the short URL corresponding to the target long URL.
[0124] After the target short URL is determined as the short URL corresponding to the target long URL, the mapping relationship between the target long URL and the target short URL can also be stored in a storage record, and a storage period can be set for it. When the storage period reaches the storage period, it is deleted. S210: Send a reset instruction to the database, receive a new minimum value of the identifier for generating the short URL sent by the database, and determine a new maximum value according to the new minimum value.
[0125] Send a reset command to the database to initialize the identifiers in the database.
[0126] Because it involves the scenario of multi-node concurrent operation, that is, multiple nodes send reset instructions to the database at the same time, so in the embodiment of the present invention, a distributed lock can be added before sending the reset instruction. That is to say, multiple nodes call the distributed lock at the same time, but only one node will be locked successfully, that is, the database will only execute the reset instruction of the node that is locked successfully. After the node executes the program to call and add the distributed lock, if the database returns YES, indicating that the lock is successful, then a reset instruction can be sent to initialize the database and wait for the feedback of the database. The database will directly return the new minimum value after initialization; if the data returns NO, indicating that the lock fails, you can wait for a period of time and directly execute step S205 to obtain the identifier from the database. The embodiment of the present invention takes the successful locking of this node as an example, so a reset instruction will be sent to the database, and then the new minimum value returned by the database will be waited for, and the new maximum value will be determined based on the new minimum value, and then step S205 will be executed.
[0127] In an embodiment of the present invention, when the short URL corresponding to the target long URL is empty in the storage record, the current minimum value and the current maximum value of the pre-stored identifier are determined; when it is determined that the current minimum value is less than the current maximum value, the target short URL can be generated according to the identifier of the current minimum value, and the target short URL is determined as the short URL of the target long URL. In an embodiment of the present invention, the target short URL is generated by pre-stored identifiers, so that the number of accesses to the database can be reduced, and the efficiency of short URL generation can be improved while reducing the collision probability of generated identifiers, especially improving the efficiency of short URL generation in high concurrency scenarios.
[0128] In order to solve the problems existing in the prior art, the embodiment of the present invention provides a device 300 for generating a short URL, such as Figure 3 As shown, the device 300 includes:
[0129] A determination unit 301, configured to determine a current minimum value and a current maximum value of a pre-stored identifier when the short URL corresponding to the target long URL is not included in the storage record, wherein the pre-stored identifier is used to generate the short URL;
[0130] A generating unit 302 is configured to generate a target short URL according to an identifier of the current minimum value and update the current minimum value if the current minimum value is less than or equal to the current maximum value;
[0131] The determining unit 301 is further configured to determine the target short URL as a short URL of the target long URL.
[0132] It should be understood that the manner of implementing the embodiments of the present invention is different from the Figure 1 The method of the illustrated embodiment is the same and will not be repeated here.
[0133] In an implementation manner of the embodiment of the present invention, the determining unit 301 is specifically configured to:
[0134] Query whether the storage record includes the target short URL;
[0135] If not, the target short URL is determined as the short URL of the target long URL;
[0136] If so, the current minimum value is updated, and when the updated current minimum value is less than the current maximum value, the target short URL is regenerated according to the updated current minimum value, and the recalculated target short URL is generated as the short URL of the target long URL.
[0137] In yet another embodiment, the generating unit 302 is specifically configured to:
[0138] Generate a code corresponding to a preset base according to the identifier of the current minimum value;
[0139] The code corresponding to the preset base is determined as the target short URL.
[0140] In yet another embodiment, if the current minimum value is not less than the maximum value, the apparatus 300 further includes:
[0141] An acquiring unit, used for acquiring a new minimum value of an identifier used for generating a short URL from a database;
[0142] The determining unit is further used to determine a new maximum value of an identifier for generating a short URL according to the new minimum value and a preset step size;
[0143] The device 300 further includes:
[0144] A judging unit, used to judge whether the new minimum value and the new maximum value belong to a preset coding range, wherein the preset coding range is determined according to a preset base system and a preset length of the short URL;
[0145] an updating unit, configured to update the new minimum value to the current minimum value and update the new maximum value to the current maximum value if yes;
[0146] The generating unit 302 is specifically configured to generate a target short URL according to the identifier of the current minimum value;
[0147] The determining unit 301 is specifically configured to determine the target short URL as a short URL of the target long URL.
[0148] In yet another embodiment, if the new minimum value and the new maximum value do not belong to the preset coding range, the apparatus 300 further includes:
[0149] A sending unit, used for sending a reset instruction to the database, so that the database is initialized according to the reset instruction;
[0150] The receiving unit is further used to receive a new minimum value of an identifier for generating a short URL sent by the database.
[0151] In yet another embodiment, the apparatus 300 further includes:
[0152] A storage unit, used for storing a mapping relationship between the target long URL and the target short URL in a storage record;
[0153] A setting unit, used to set a storage period of the mapping relationship between the target long URL and the target short URL;
[0154] The deleting unit is used to delete the mapping relationship between the target long URL and the target short URL when the storage time of the mapping relationship reaches the storage period.
[0155] In yet another embodiment, the determining unit 301 is specifically configured to:
[0156] Determine whether the number of short URLs generated within a preset time period reaches a threshold;
[0157] If not, then determine the current minimum value and the current maximum value of the pre-stored identifier.
[0158] It should be understood that the manner of implementing the embodiments of the present invention is different from the Figure 1 or Figure 2 The method of the illustrated embodiment is the same and will not be repeated here.
[0159] In an embodiment of the present invention, when the short URL corresponding to the target long URL is not included in the storage record, the current minimum value and the current maximum value of the pre-stored identifier are determined; when it is determined that the current minimum value is less than the current maximum value, the target short URL can be generated according to the identifier of the current minimum value, and the target short URL is determined as the short URL of the target long URL. In an embodiment of the present invention, the target short URL is generated by pre-stored identifiers, so that the number of accesses to the database can be reduced, and the efficiency of short URL generation can be improved while reducing the collision probability of generated identifiers, especially improving the efficiency of short URL generation in high concurrency scenarios.
[0160] According to an embodiment of the present invention, the present invention also provides an electronic device and a readable storage medium.
[0161] The electronic device of the present invention comprises: at least one processor; and a memory connected to the at least one processor in communication; wherein the memory stores instructions that can be executed by the one processor, and the instructions are executed by the at least one processor so that the at least one processor executes the method for generating a short URL provided in an embodiment of the present invention.
[0162] Figure 4 An exemplary system architecture 400 is shown to which the method or apparatus for generating a short URL according to an embodiment of the present invention may be applied.
[0163] like Figure 4 As shown, system architecture 400 may include terminal devices 401, 402, 403, network 404 and server 405. Network 404 is used to provide a medium for communication links between terminal devices 401, 402, 403 and server 405. Network 404 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0164] Users can use terminal devices 401, 402, 403 to interact with server 405 through network 404 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 401, 402, 403, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only as examples).
[0165] The terminal devices 401 , 402 , and 403 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.
[0166] The server 405 may be a server that provides various services, such as a backend management server (only an example) that provides support for shopping websites browsed by users using the terminal devices 401, 402, and 403. The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results (such as product information - only an example) to the terminal device.
[0167] It should be noted that the method for generating a short URL provided in the embodiment of the present invention is generally executed by the server 405 , and accordingly, the device for generating a short URL is generally disposed in the server 405 .
[0168] It should be understood that Figure 4 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to the implementation requirements.
[0169] Reference below Figure 5 , which shows a schematic diagram of the structure of a computer system 500 suitable for implementing an embodiment of the present invention. Figure 5 The computer system shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0170] like Figure 5 As shown, the computer system 500 includes a central processing unit (CPU) 501, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 502 or a program loaded from a storage part 508 into a random access memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the system 500 are also stored. The CPU 501, the ROM 502, and the RAM 503 are connected to each other via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0171] The following components are connected to the I / O interface 505: an input section 506 including a keyboard, a mouse, etc.; an output section 507 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 508 including a hard disk, etc.; and a communication section 509 including a network interface card such as a LAN card, a modem, etc. The communication section 509 performs communication processing via a network such as the Internet. A drive 510 is also connected to the I / O interface 505 as needed. A removable medium 511, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 510 as needed, so that a computer program read therefrom is installed into the storage section 508 as needed.
[0172] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 509, and / or installed from the removable medium 511. When the computer program is executed by the central processing unit (CPU) 501, the above-mentioned functions defined in the system of the present invention are executed.
[0173] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0174] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a unit, a program segment, or a part of a code, and the above-mentioned unit, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0175] The units involved in the embodiments of the present invention may be implemented by software or hardware. The units described may also be arranged in a processor, for example, it may be described as: a processor includes a receiving unit, a determining unit, an acquiring unit, and a comparing unit. The names of these units do not constitute a limitation on the units themselves in some cases, for example, the receiving unit may also be described as a "unit of the function of the receiving unit".
[0176] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device executes the method for generating a short URL provided by the present invention.
[0177] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for generating a short URL, characterized in that: include: When the short URL corresponding to the target long URL is not included in the storage record, determining the current minimum value and the current maximum value of the pre-stored identifier, the pre-stored identifier is used to generate the short URL; If the current minimum value is less than or equal to the current maximum value, generate a target short URL according to the identifier of the current minimum value, and update the current minimum value; Determine the target short URL as the short URL of the target long URL; The step of determining the target short URL as the short URL of the target long URL comprises: Query whether the storage record includes the target short URL; If not, the target short URL is determined as the short URL of the target long URL; If so, the current minimum value is updated, and when the updated current minimum value is less than the current maximum value, the target short URL is regenerated according to the updated current minimum value, and the recalculated target short URL is generated as the short URL of the target long URL.
2. The method according to claim 1, characterized in that: The step of generating a target short URL according to the identifier of the current minimum value includes: Generate a code corresponding to a preset base according to the identifier of the current minimum value; The code corresponding to the preset base is determined as the target short URL.
3. The method according to claim 1, characterized in that If the current minimum value is not less than the maximum value, the method further includes: Get the new minimum value of the identifier used to generate the short URL from the database; Determine a new maximum value of an identifier for generating a short URL according to the new minimum value and a preset step size; Determine whether the new minimum value and the new maximum value belong to a preset coding range, where the preset coding range is determined according to a preset base system and a preset length of the short URL; If yes, then update the new minimum value to the current minimum value, and update the new maximum value to the current maximum value; A target short URL is generated according to the identifier of the current minimum value, and the target short URL is determined as the short URL of the target long URL.
4. The method according to claim 3, characterized in that If the new minimum value and the new maximum value do not belong to the preset coding range, the method further includes: Sending a reset instruction to the database so that the database is initialized according to the reset instruction; A new minimum value of an identifier for generating a short URL is received from the database.
5. The method according to claim 1, characterized in that After determining the target short URL as the short URL of the target long URL, the method further includes: Storing a mapping relationship between the target long URL and the target short URL in a storage record; Setting a storage period for the mapping relationship between the target long URL and the target short URL; When the storage time of the mapping relationship reaches the storage period, the mapping relationship between the target long URL and the target short URL is deleted.
6. The method according to claim 5, characterized in that Determining the current minimum value and the current maximum value of the pre-stored identifier includes: Determine whether the number of short URLs generated within a preset time period reaches a threshold; If not, then determine the current minimum value and the current maximum value of the pre-stored identifier.
7. A method for generating a short URL, characterized in that: include: When the short URL corresponding to the target long URL is not included in the storage record, determining the current minimum value and the current maximum value of the pre-stored identifier, the pre-stored identifier is used to generate the short URL; If the current minimum value is less than or equal to the current maximum value, generate a target short URL according to the identifier of the current minimum value, and update the current minimum value; Determine the target short URL as the short URL of the target long URL; If the current minimum value is not less than the maximum value, the method further includes: Get the new minimum value of the identifier used to generate the short URL from the database; Determine a new maximum value of an identifier for generating a short URL according to the new minimum value and a preset step size; Determine whether the new minimum value and the new maximum value belong to a preset coding range, where the preset coding range is determined according to a preset base system and a preset length of the short URL; If yes, then update the new minimum value to the current minimum value, and update the new maximum value to the current maximum value; A target short URL is generated according to the identifier of the current minimum value, and the target short URL is determined as the short URL of the target long URL.
8. The method according to claim 7, characterized in that The step of generating a target short URL according to the identifier of the current minimum value includes: Generate a code corresponding to a preset base according to the identifier of the current minimum value; The code corresponding to the preset base is determined as the target short URL.
9. The method according to claim 7, characterized in that: If the new minimum value and the new maximum value do not belong to the preset coding range, the method further includes: Sending a reset instruction to the database so that the database is initialized according to the reset instruction; A new minimum value of an identifier for generating a short URL is received from the database.
10. The method according to claim 7, characterized in that After determining the target short URL as the short URL of the target long URL, the method further includes: Storing a mapping relationship between the target long URL and the target short URL in a storage record; Setting a storage period for the mapping relationship between the target long URL and the target short URL; When the storage time of the mapping relationship reaches the storage period, the mapping relationship between the target long URL and the target short URL is deleted.
11. A device for generating a short URL, characterized in that: include: A determination unit, configured to determine a current minimum value and a current maximum value of a pre-stored identifier when the short URL corresponding to the target long URL is not included in the storage record, wherein the pre-stored identifier is used to generate the short URL; A generating unit, configured to generate a target short URL according to an identifier of the current minimum value and update the current minimum value if the current minimum value is less than or equal to the current maximum value; The determining unit is further configured to determine the target short URL as a short URL of the target long URL; The determining unit is specifically configured to: Query whether the storage record includes the target short URL; If not, the target short URL is determined as the short URL of the target long URL; If so, the current minimum value is updated, and when the updated current minimum value is less than the current maximum value, the target short URL is regenerated according to the updated current minimum value, and the recalculated target short URL is generated as the short URL of the target long URL.
12. A device for generating a short URL, characterized in that: include: A determination unit, configured to determine a current minimum value and a current maximum value of a pre-stored identifier when the short URL corresponding to the target long URL is not included in the storage record, wherein the pre-stored identifier is used to generate the short URL; A generating unit, configured to generate a target short URL according to an identifier of the current minimum value and update the current minimum value if the current minimum value is less than or equal to the current maximum value; The determining unit is further configured to determine the target short URL as a short URL of the target long URL; If the current minimum value is not less than the maximum value, the method further includes: An acquiring unit, used for acquiring a new minimum value of an identifier used for generating a short URL from a database; The determining unit is further used to determine a new maximum value of an identifier for generating a short URL according to the new minimum value and a preset step size; A judging unit, used to judge whether the new minimum value and the new maximum value belong to a preset coding range, wherein the preset coding range is determined according to a preset base system and a preset length of the short URL; an updating unit, configured to update the new minimum value to the current minimum value and update the new maximum value to the current maximum value if yes; The generating unit is specifically configured to generate a target short URL according to the identifier of the current minimum value; The determining unit is specifically configured to determine the target short URL as the short URL of the target long URL.
13. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 10.
14. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 10 is implemented.
Citation Information
Patent Citations
Method and terminal for generating short web address
CN109313660A