Search code examples
javageolocationgeoipmaxmind

Java and GeoLite database: How to use GeoLiteCity.dat in a production way?


I downloaded GeoLite City database and I am able to access the database with Java to get the corresponding location information of an IP address in the following way:

File file = new File("C:\\GeoLiteCity.dat");
LookupService lookup = new LookupService(file, LookupService.GEOIP_MEMORY_CACHE);
Location location = lookup.getLocation("123.123.12.34");

My question: opening the file for each an IP address in the above way is obviously not desirable in a production environment. How can I use this database in a production way (assuming the use of GeoLiteCity.dat)?

Any input or pointer is really appreciated.

Thanks and regards!


Solution

  • Two suggestions:

    1. Make the LookupService static (lookup) and available in the memory (Singleton)
    2. Load this file (or CSV file version) into a database (New tables, columns etc...)
    

    I'm not sure if the database would improve performance greatly enough versus just keeping the service loaded into memory and searching on it. With these GeoLite weekly file releases, there would additional overhead of cleaning/loading/indexing the database as well.