Method and system for converting high-performance IP to geographic position based on IP offline library
By adopting an IP offline library-based method in IP geolocation query, combining Redis and MongoDB caches to clean and use hybrid data structures, the problems of low query efficiency and insufficient data accuracy in the existing technology are solved, and efficient and accurate IP geolocation query is achieved.
Patent Information
- Application Number
- CN202510135076.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-07
- Publication Date
- 2025-03-07
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The prior art has problems such as data redundancy, untimely updates and inefficient query in IP-based geolocation queries, and there is room for improvement in cache hit rate and data accuracy.
The high-performance IP-to-geographic location method based on IP offline library is adopted, and the query process is accelerated through Redis cache and MongoDB cache mechanism, combining data cleaning and mixed data structures to improve the accuracy and query efficiency of geolocation information.
It significantly improves the accuracy and query efficiency of geolocation information, reduces query delay, and can quickly obtain reliable geolocation information when cache misses, ensuring the stability and practicality of the system.
Smart Images

Figure CN119577054A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of IP geographic location query and positioning, and in particular relates to a high-performance IP-to-geolocation method and system based on an IP offline library. Background Art
[0002] With the rapid development of the Internet, IP-based geolocation query technology has been widely used in network security, advertising, content distribution, user analysis and other fields. Traditional IP geolocation resolution methods mainly rely on online databases, but this method has certain real-time and reliability issues. The offline IP geolocation database (IP Offline Database) provides a solution that can quickly resolve geolocation without relying on network connection by pre-downloading and storing geolocation information. However, relying solely on offline databases for querying may face problems such as data redundancy, untimely updates and low query efficiency.
[0003] In order to solve the above problems, some IP geolocation query methods based on cache technology have been developed in recent years, which accelerate the query process by using high-speed cache. However, the existing technology still has room for improvement in cache hit rate and data accuracy. For example, how to effectively manage and update cache data, and how to quickly and accurately obtain geolocation information from offline libraries when the cache misses are all problems that need to be solved. Summary of the invention
[0004] The purpose of the present invention is to provide a high-performance IP-to-geolocation method and system based on an IP offline library to solve the problem of low efficiency of the prior art.
[0005] To achieve one of the above-mentioned purposes of the invention, an embodiment of the present invention provides a high-performance IP geolocation method based on an IP offline library, comprising:
[0006] In response to the IP query request, query the geographic location information in the Redis cache. If the corresponding geographic location information is found in the Redis cache, output the query result; if the corresponding geographic location information is not found in the Redis cache, query the MongoDB cache. If the corresponding geographic location information is found in the MongoDB cache, output the query result;
[0007] If the corresponding geographic location information is not found in the MongoDB cache, the IP string is converted into an integer;
[0008] Based on the integer, a search is performed in the index area of the IP offline library to obtain the offset corresponding to the IP in the index area, and based on the offset, the geographical location information corresponding to the IP is extracted from the record area of the IP offline library.
[0009] As a further improvement of an embodiment of the present invention, the method further includes acquiring geographic location information of all administrative divisions to construct an IP offline library, and performing data cleaning on the IP offline library, including removing duplicate data according to a unique identifier of the geographic location information, supplementing missing data or identifiers, and completing the missing data by manual intervention, thereby generating an IP offline library file with a hybrid structure of an array and a tree;
[0010] The IP offline library file includes a national list and a provincial list; the national list includes a national field; the provincial list includes a provincial field, a municipal field, and a district and county field;
[0011] The file header, index area and record area information are obtained by parsing the IP offline library file.
[0012] As a further improvement of an embodiment of the present invention, the method further includes that the "obtaining the file header, index area and record area information by parsing the IP offline library file" includes:
[0013] By reading the first several fixed-length bytes of the IP offline library file, the file header information is obtained, including the version number, the total number of records, the starting offset of the index area, and the starting offset of the record area;
[0014] According to the starting offset of the index area, the reading position of the IP offline library file is adjusted to obtain the index area information, including multiple index records, each index record points to a specific position in the record area; each index record includes an integer of the starting IP and the ending IP and a record area offset, and each index record is sorted from small to large according to the integer corresponding to the starting IP;
[0015] According to the record area offset, the IP offline library file reading position is adjusted to obtain the record area information, including the geographical location information at the corresponding offset.
[0016] As a further improvement of an embodiment of the present invention, the method further includes: the index area information further includes:
[0017] Based on the file header information, the starting offset of the index area, the total number of records and the offset address length are obtained to calculate the end record offset, and the end position of the index area is marked; the calculation formula of the end record offset is:
[0018] The offset of the end record = the starting offset + the total number of records * the offset address length.
[0019] As a further improvement of an embodiment of the present invention, the method further includes that the “searching in the index area of the IP offline library based on the integer, obtaining the offset corresponding to the IP in the index area, and extracting the geographical location information corresponding to the IP from the record area of the IP offline library based on the offset” includes,
[0020] Use binary search to find the starting record corresponding to the integer number of the IP in the index area;
[0021] The offset corresponding to the start record in the record area is obtained, and the geographical location information corresponding to the IP is read from the record area.
[0022] As a further improvement of an embodiment of the present invention, the method further includes using the IP offline library after data cleaning to parse and complete the geographic location information, including:
[0023] Match the country list and determine the country field of the first-level region information;
[0024] Match the provincial list to determine the province and city fields of the secondary region information;
[0025] Match the sublist of the province and city fields to determine the city-level fields for the third-level regions;
[0026] Match the sublist of the city-level field to determine the district and county field for the fourth-level region.
[0027] As a further improvement of an embodiment of the present invention, the method further includes: adopting active caching and passive caching strategies for Redis cache and MongoDB cache;
[0028] The active caching includes responding to the IP query request, if the Redis cache and the MongoDB cache do not query the corresponding address information, actively updating the Redis cache and the MongoDB cache;
[0029] The passive cache includes updating the Redis cache and the MongoDB cache when data is updated.
[0030] To achieve one of the above-mentioned purposes of the invention, an embodiment of the present invention provides a high-performance IP-to-geolocation system based on an IP offline library, including a query module, a conversion module and an extraction module;
[0031] The query module is used to query the geographic location information in the Redis cache in response to the IP query request, and if the corresponding geographic location information is found in the Redis cache, the query result is output; if the corresponding geographic location information is not found in the Redis cache, the MongoDB cache is queried, and if the corresponding geographic location information is found in the MongoDB cache, the query result is output;
[0032] The conversion module is used to convert the IP string into an integer number if the corresponding geographic location information is not found in the MongoDB cache;
[0033] The extraction module is used to search in the index area of the IP offline library based on the integer, obtain the offset corresponding to the IP in the index area, and extract the geographical location information corresponding to the IP from the record area of the IP offline library based on the offset.
[0034] To achieve one of the above-mentioned purposes of the invention, an embodiment of the present invention further provides an electronic device, comprising a memory and a processor, characterized in that the memory stores a computer program that can be run on the processor, and when the program is executed on the processor, the steps in the method of high-performance IP to geographic location conversion based on IP offline library as described above are implemented.
[0035] To achieve one of the above-mentioned purposes of the invention, an embodiment of the present invention further provides a storage medium, wherein the storage medium stores a computer program, and wherein when the computer program is executed by a processor, the steps in the method for high-performance IP-to-geolocation conversion based on an IP offline library as described above are implemented.
[0036] Compared with the prior art, the present invention provides a high-performance IP-to-geolocation method and system based on an IP offline library, which uses an offline library to parse IP, and significantly improves the accuracy and query efficiency of geographic location information through data cleaning and hybrid data structure. By introducing Redis cache and MongoDB cache mechanism, query delay is reduced, and when the cache misses, reliable geographic location information can still be quickly obtained from the offline library, ensuring the stability and practicality of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 It is an overall flow chart of the high-performance IP-to-geolocation method based on the IP offline library described in the present invention.
[0038] Figure 2 It is a data structure diagram of the high-performance IP-to-geolocation method based on the IP offline library of the present invention.
[0039] Figure 3 It is a schematic diagram of the architecture of the high-performance IP-to-geolocation system based on the IP offline library of the present invention. DETAILED DESCRIPTION
[0040] The present invention will be described in detail below in conjunction with the specific embodiments shown in the accompanying drawings. However, these embodiments do not limit the present invention, and any structural, methodological, or functional changes made by a person skilled in the art based on these embodiments are all within the scope of protection of the present invention.
[0041] The embodiments of the present invention are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and cannot be understood as limiting the present invention.
[0042] In the first embodiment of the present invention, the present invention provides a high-performance IP geolocation method based on an IP offline library, the method comprising:
[0043] S1: In response to the IP query request, query the geographic location information in the Redis cache. If the corresponding geographic location information is found in the Redis cache, the query result is output; if the corresponding geographic location information is not found in the Redis cache, query the MongoDB cache. If the corresponding geographic location information is found in the MongoDB cache, the query result is output;
[0044] S2: If the corresponding geographic location information is not found in the MongoDB cache, the IP string is converted into an integer;
[0045] S3: Search the index area of the IP offline library based on the integer number, obtain the offset corresponding to the IP in the index area, and extract the geographical location information corresponding to the IP from the record area of the IP offline library based on the offset.
[0046] Furthermore, the geographical location information of all administrative divisions is obtained to construct an IP offline library, and data cleaning is performed on the IP offline library, including removing duplicate data according to the unique identifier of the geographical location information, supplementing missing data or identifiers, and completing the missing data by manual intervention measures, to generate an IP offline library file with a hybrid structure of array and tree;
[0047] The IP offline library file includes a national list and a provincial list; the national list includes a national field; the provincial list includes a provincial field, a municipal field, and a district and county field;
[0048] The file header, index area and record area information are obtained by parsing the IP offline library file.
[0049] Specifically, first, duplicate data is removed based on the unique identifier of the geographic location information to ensure the uniqueness and accuracy of each geographic location record. Next, the data is verified and sorted, and missing geographic location information is supplemented or missing information is marked in the record. For missing data that cannot be resolved by automated processing, manual intervention measures are taken to complete it to ensure the integrity and high quality of geographic location information. In addition, by standardizing the format of geographic location information and unifying coding specifications, the consistency and availability of data are further improved. After these steps, the IP offline library has high accuracy and completeness, providing a reliable data foundation for subsequent IP geographic location resolution.
[0050] It should be noted that processing duplicate data means removing duplicate records based on unique identifiers (such as country codes, district and county codes, etc.), and ensuring that data redundancy does not occur due to duplicate data when merging different data sources; processing missing data means that for data entries that lack important fields, the missing data is first supplemented from other sources. If all data sources are missing, they are marked for manual intervention.
[0051] Specifically, the IP offline library data is first parsed into three parts: file header, index area and record area. The file header contains basic information and structure definition of the data, such as the total number of records, the starting position of the index area and the starting position of the record area. According to the information in the file header, the index area is further parsed to obtain the record position corresponding to each IP segment. The record area stores the actual geographic location information. In the data storage process, a hybrid data structure of array and tree is adopted to make data query and management more efficient. The national list and provincial list are important components of the hybrid data structure. The national list contains the field information of each country, which is used to quickly locate the national geographic location. The provincial list is further subdivided into provincial and municipal fields, municipal fields and district and county fields, which correspond to the geographic location information of each province, city and district and county respectively. Through this hierarchical structure, a large amount of geographic location information can be effectively organized and managed to ensure that the corresponding geographic location information can be quickly located and obtained when performing IP queries.
[0052] It should be noted that the hybrid data structure of array and tree used in the present invention is as follows: countryList [ { "country": "Andorra" }, { "country": "Benin" }, { "country": "Bahrain" }, { "country": "Cuba" } ] provinceList [ { "id": 19, "name": "Guangdong Province", "parentId": 0, "province": "Guangdong Province", "shortName": "Guangdong", "subList": [ { "city": "Guangzhou", "id": 230, "name": "Guangzhou", "parentId": 19, "province": "Guangdong Province", "shortName": "Guangzhou", "subList": [ { "city": "Guangzhou", "district": "Liwan District", "id": 2386, "name": "Liwan District", "parentId": 230, "province": "Guangdong Province", "shortName": "Liwan" } ] } ] } ]
[0053] In a specific implementation of the present invention, the file header, index area and record area information are obtained by parsing the IP offline library file, specifically,
[0054] By reading the first several fixed-length bytes of the IP offline library file, the file header information is obtained, including the version number, the total number of records, the starting offset of the index area, and the starting offset of the record area;
[0055] According to the starting offset of the index area, the reading position of the IP offline library file is adjusted to obtain the index area information, including multiple index records, each index record points to a specific position in the record area; each index record includes an integer of the starting IP and the ending IP and a record area offset, and each index record is sorted from small to large according to the integer corresponding to the starting IP;
[0056] According to the record area offset, the IP offline library file reading position is adjusted to obtain the record area information, including the geographical location information at the corresponding offset.
[0057] Based on the file header information, the starting offset of the index area, the total number of records and the offset address length are obtained to calculate the end record offset, and the end position of the index area is marked; the calculation formula of the end record offset is:
[0058] The offset of the end record = the starting offset + the total number of records * the offset address length.
[0059] It should be noted that the IP offline library data file is a binary structure, such as Figure 2 As shown, it is divided into file header, record area and index area, which need to be parsed and read. Load the IP database file into the local cache of the server and store it in the data structure of byte array (byte[] data).
[0060] In a specific implementation scenario, the file header information can be obtained by directly reading the first 32 bytes from data. The file header information includes the starting record offset of the index area (bytes 17-24), the total number of records (bytes 7-16), and the offset address length (byte 7);
[0061] Through the index area starting record offset (725813), total number of records (176515) and offset address length (3) in the file header information, the offset of the last record (1255358) can be calculated. The offset from the starting record to the last record in data is the index area data. Each record of the index area data contains an integer corresponding to the starting IP of the IP segment and the offset of the IP segment geographical location information in the record area. For example, 183.63.1.1-183.63.33.255 all belong to Huizhou, Guangdong, China. The starting IP of the IP segment (183.63.1.1) is converted into the integer 3074359553 ((183L<<24L)+(63L<<16L)+(1L<<8L)+1L). The offset of "Huizhou, Guangdong, China" in the record area is 10000. Then the record information of this index area is 3074359553 and 10000. All records in the index area are sorted from small to large by the integer corresponding to the starting IP.
[0062] The record area records the location information data of each record in the index area. For example, if the location information corresponding to the 183.63.1.1-183.63.33.255 IP segment is offset 10000 in the record area, then the data is read from the 10000th byte of data until "\0" is read. This part of the byte data is the geographical location information of the IP segment.
[0063] It should be noted that, taking the IP (183.63.1.1) of Huizhou, Guangdong as an example, the conversion of IP into an integer number includes the following steps: ①Convert each part of the IP address into binary form: 183 converted to binary: 10110111 63 converted to binary: 00111111 1 converted to binary: 00000001 1 converted to binary: 00000001; ② Shift each part left in sequence and convert it to decimal: The first segment (183) is shifted left by 24 bits: 183L << 24 10110111 << 24 = 10110111 00000000 00000000 00000000 Convert to decimal: 183 × (2^24) = 183 × 16777216 = 3070230528 The second segment (63) is shifted left by 16 bits: 63L << 16 00111111 << 16 = 00111111 00000000 00000000 Convert to decimal: 63 × (2^16) = 63 × 65536 = 4128768 The third segment (1) shifts left by 8 bits: 1L << 8 00000001 << 8 = 00000001 00000000 Convert to decimal: 1 × (2^8) = 1 × 256 = 256 The fourth segment (1) does not shift left: 1L Remain unchanged: 00000001 Convert to decimal: 1 ③Add up all the parts: 3070230528 + 4128768 + 256 + 1 = 3074359553.
[0064] In a specific embodiment of the present invention, a search is performed in the index area of the IP offline library based on an integer, an offset corresponding to the IP in the index area is obtained, and the geographical location information corresponding to the IP is extracted from the record area of the IP offline library based on the offset, specifically,
[0065] Use binary search to find the starting record corresponding to the integer number of the IP in the index area;
[0066] The offset corresponding to the start record in the record area is obtained, and the geographical location information corresponding to the IP is read from the record area.
[0067] It should be noted that the binary search algorithm is an efficient search algorithm, especially suitable for data sets that have been arranged in order. By continuously reducing the search range by half, the target record can be quickly located. Assuming that there are 100 records in the index area, first obtain the IP of the 50th record in the index area for comparison (compare the corresponding numbers). If the queried IP is smaller, then obtain the 25th record in the index area (between the 1st and 50th) for comparison. Otherwise, obtain the 75th record in the index area (between the 50th and 100th) for comparison, and so on, until the queried IP is between the previous and next two records. For example: the 32nd record is 3074359553, the 33rd record is 3074368001, and the number corresponding to the queried IP is greater than or equal to 3074359553 and less than 3074368001, then it belongs to the 32nd record.
[0068] In a specific embodiment of the present invention, the geographical location information is parsed and completed using the IP offline library after data cleaning, specifically,
[0069] Match the country list and determine the country field of the first-level region information;
[0070] Match the provincial list to determine the province and city fields of the secondary region information;
[0071] Match the sublist of the province and city fields to determine the city-level fields for the third-level regions;
[0072] Match the sublist of the city-level field to determine the district and county field for the fourth-level region.
[0073] It should be noted that after IP resolution and index search, a geographic location address corresponding to the IP will be obtained, but this geographic location address may not be a standard address. The address needs to be resolved, corrected, and completed. The specific steps are as follows:
[0074] ① Use the address to match the countryList data structure to determine whether the location address starts with these regions (address.startsWith(countryArea.getCountry())). If it matches, the region name is directly used as the first-level region information of the IP (country field);
[0075] ② Use the address to match the provinceList data structure to determine whether the location address contains the name (or abbreviation) of these regions, StringUtils.indexOf(address, area.getName())>-1 ||StringUtils.indexOf(address, area.getShortName())>-1. If it matches, the region name is directly used as the secondary region information (province field) of the IP.
[0076] ③ Use the location address to match the sublist of the subordinate regions of the second-level region matched in ② to determine whether the location address contains the names (or abbreviations) of these regions. If a match is found, the region name is directly used as the third-level region information (city field) of the IP;
[0077] ④ Use the location address to match the sublist of the third-level region matched in ③ to determine whether the location address contains the name (or abbreviation) of these regions. If it matches, the region name is directly used as the fourth-level region information (district field) of the IP;
[0078] ⑤ Concatenate the above country, province, city, and district to obtain the standardized geographic information corresponding to the IP. For example, if a location address is Sanjiang Tong Autonomous County, Liuzhou, Guangxi Tibetan Autonomous Region (actually: Sanjiang Dong Autonomous County, Liuzhou City, Guangxi Zhuang Autonomous Region), it will be standardized as Sanjiang, Liuzhou City, Guangxi through the above analysis.
[0079] In a specific embodiment of the present invention, active caching and passive caching strategies are adopted for Redis cache and MongoDB cache;
[0080] The active caching includes responding to the IP query request, if the Redis cache and the MongoDB cache do not query the corresponding address information, actively updating the Redis cache and the MongoDB cache;
[0081] The passive cache includes updating the Redis cache and the MongoDB cache when data is updated.
[0082] It should be noted that querying the Redis cache: the device first queries the Redis cache to check whether the geographic location information corresponding to the IP has been stored; if the information exists in the Redis cache, the geographic location information is directly obtained and returned from the Redis cache. Querying the MongoDB cache: If the geographic location information of the IP does not exist in the Redis cache, the device will query the MongoDB cache; search for the IP in MongoDB, and if the corresponding geographic location information is found, it will be returned to the device and the Redis cache will be updated for quick access next time.
[0083] It should be noted that, in order to ensure data consistency, the device adopts an active cache + passive cache strategy. Active cache - actively updates the Redis cache and MongoDB cache when data is queried. Passive cache - when data is updated, the device is responsible for updating the cached data in the Redis cache and MongoDB to keep it up to date.
[0084] Furthermore, in one embodiment of the present invention, by operating the Redis cache in memory as much as possible, the hard disk I / O overhead is reduced; the MongoDB index is used to accelerate the query and improve the retrieval efficiency; and expired cache data is regularly cleared to prevent storage space from being occupied by invalid data.
[0085] In the second embodiment of the present invention, the present invention provides a high-performance IP-to-geolocation system based on an IP offline library, the system comprising a query module 1, a parsing module 2 and a completion module 3;
[0086] The query module 1 is used to query the geographic location information in the Redis cache in response to the IP query request, and if the corresponding geographic location information is found in the Redis cache, the query result is output; if the corresponding geographic location information is not found in the Redis cache, the MongoDB cache is queried, and if the corresponding geographic location information is found in the MongoDB cache, the query result is output;
[0087] The conversion module 2 is used to convert the IP string into an integer number if the corresponding geographic location information is not found in the MongoDB cache;
[0088] The extraction module 3 is used to search in the index area of the IP offline library based on the integer, obtain the offset corresponding to the IP in the index area, and extract the geographical location information corresponding to the IP from the record area of the IP offline library based on the offset.
[0089] In the third embodiment of the present invention, the present invention provides an electronic device, including a memory and a processor, characterized in that the memory stores a computer program that can be run on the processor, and when the program is executed on the processor, the steps in the method of high-performance IP to geographic location conversion based on IP offline library as described above are implemented.
[0090] In a fourth embodiment of the present invention, the present invention provides a storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps in the method for high-performance IP-to-geolocation conversion based on an IP offline library as described above.
[0091] In summary, the present invention provides a high-performance IP-to-geolocation method and system based on an IP offline library, which uses an offline library to parse IP, and significantly improves the accuracy and query efficiency of geographic location information through data cleaning and hybrid data structure. By introducing Redis cache and MongoDB cache mechanism, query delay is reduced, and when the cache misses, reliable geographic location information can still be quickly obtained from the offline library, ensuring the stability and practicality of the system.
[0092] It should be understood that although this specification is described according to implementation modes, not every implementation mode contains only one independent technical solution. This description of the specification is only for the sake of clarity. Those skilled in the art should regard the specification as a whole. The technical solutions in each implementation mode may also be appropriately combined to form other implementation modes that can be understood by those skilled in the art.
[0093] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the modules described above can refer to the corresponding process in the aforementioned method implementation, and will not be repeated here.
[0094] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical modules, that is, they may be located in one place or distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present implementation scheme.
[0095] In addition, each functional module in each embodiment of the present application can be integrated into one processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The above integrated modules can be implemented in the form of hardware or in the form of hardware plus software functional modules.
[0096] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for a computer system (which can be a personal computer, a server, or a network system, etc.) or a processor to perform some steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program codes.
[0097] Finally, it should be noted that the above implementation modes are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned implementation modes, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned implementation modes, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various implementation modes of the present application.
Claims
1. A high-performance IP-to-geolocation method based on an IP offline library, characterized in that: include, In response to the IP query request, query the geographic location information in the Redis cache. If the corresponding geographic location information is found in the Redis cache, output the query result; If the corresponding geographic location information is not found in the Redis cache, the MongoDB cache is queried. If the corresponding geographic location information is found in the MongoDB cache, the query result is output; If the corresponding geographic location information is not found in the MongoDB cache, the IP string is converted into an integer; Based on the integer, a search is performed in the index area of the IP offline library to obtain the offset corresponding to the IP in the index area, and based on the offset, the geographical location information corresponding to the IP is extracted from the record area of the IP offline library.
2. The high-performance IP geolocation method based on IP offline library according to claim 1 is characterized in that: Also includes, Obtaining the geographical location information of all administrative divisions to build an IP offline library, and performing data cleaning on the IP offline library, including removing duplicate data according to the unique identifier of the geographical location information, supplementing missing data or identifiers, and completing the missing data by manual intervention measures, and generating an IP offline library file with a hybrid structure of array and tree; The IP offline library file includes a national list and a provincial list; the national list includes a national field; the provincial list includes a provincial field, a municipal field, and a district and county field; The file header, index area and record area information are obtained by parsing the IP offline library file.
3. The high-performance IP geolocation method based on IP offline library according to claim 2 is characterized in that: The "obtaining file header, index area and record area information by parsing the IP offline library file" includes: By reading the first several fixed-length bytes of the IP offline library file, the file header information is obtained, including the version number, the total number of records, the starting offset of the index area, and the starting offset of the record area; According to the starting offset of the index area, the reading position of the IP offline library file is adjusted to obtain the index area information, including multiple index records, each index record points to a specific position in the record area; each index record includes an integer of the starting IP and the ending IP and a record area offset, and each index record is sorted from small to large according to the integer corresponding to the starting IP; According to the record area offset, the IP offline library file reading position is adjusted to obtain the record area information, including the geographical location information at the corresponding offset.
4. The high-performance IP geolocation method based on IP offline library according to claim 3 is characterized in that: The index area information also includes: Based on the file header information, the starting offset of the index area, the total number of records and the offset address length are obtained to calculate the end record offset, and the end position of the index area is marked; the calculation formula of the end record offset is: The offset of the end record = the starting offset + the total number of records * the offset address length.
5. The high-performance IP geolocation method based on IP offline library according to claim 4 is characterized in that: The "searching in the index area of the IP offline library based on the integer, obtaining the offset corresponding to the IP in the index area, and extracting the geographical location information corresponding to the IP from the record area of the IP offline library based on the offset" includes: Use binary search to find the starting record corresponding to the integer number of the IP in the index area; The offset corresponding to the start record in the record area is obtained, and the geographical location information corresponding to the IP is read from the record area.
6. The high-performance IP geolocation method based on IP offline library according to claim 5 is characterized in that: It also includes using the IP offline library after data cleaning to parse and complete the geographic location information, including: Match the country list and determine the country field of the first-level region information; Match the provincial list to determine the province and city fields of the secondary region information; Match the sublist of the province and city fields to determine the city-level fields for the third-level regions; Match the sublist of the city-level field to determine the district and county field for the fourth-level region.
7. The high-performance IP geolocation method based on IP offline library according to claim 1 is characterized in that: Also includes, Use active caching and passive caching strategies for Redis cache and MongoDB cache; The active caching includes responding to the IP query request, if the Redis cache and the MongoDB cache do not query the corresponding address information, actively updating the Redis cache and the MongoDB cache; The passive cache includes updating the Redis cache and the MongoDB cache when data is updated.
8. A high-performance IP-to-geolocation system based on an IP offline database, characterized in that: It includes a query module, a conversion module and an extraction module; The query module is used to query the geographic location information in the Redis cache in response to the IP query request, and output the query result if the corresponding geographic location information is found in the Redis cache; If the corresponding geographic location information is not found in the Redis cache, the MongoDB cache is queried. If the corresponding geographic location information is found in the MongoDB cache, the query result is output; The conversion module is used to convert the IP string into an integer number if the corresponding geographic location information is not found in the MongoDB cache; The extraction module is used to search in the index area of the IP offline library based on the integer, obtain the offset corresponding to the IP in the index area, and extract the geographical location information corresponding to the IP from the record area of the IP offline library based on the offset.
9. An electronic device, comprising a memory and a processor, characterized in that: The memory stores a computer program that can be run on the processor, and when the program is executed on the processor, the steps in the method for high-performance IP geolocation conversion based on an IP offline library as described in any one of claims 1-7 are implemented.
10. A storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps in the method for high-performance IP geolocation conversion based on an IP offline library as described in any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Index querying method and apparatus
CN105224609A
Data query method and device
CN111159244A
Data query method and device
CN111291079A
IP address data processing method and device and electronic equipment
CN111629081A
IP geographical location information query method, terminal device and storage medium
CN112948376A
