I’m using Python on Appengine and am looking up the geolocation of an IP address like this:
import pygeoip
gi = pygeoip.GeoIP('GeoIP.dat')
Location = gi.country_code_by_addr(self.request.remote_addr)
(pygeoip can be found here: http://code.google.com/p/pygeoip/)
I want to geolocate each page of my app for a user so currently I lookup the IP address once then store it in memcache.
My question – which is quicker? Looking up the IP address each time from the .dat file or fetching it from memcache? Are there any other pros/cons I need to be aware of?
For general queries like this, is there a good guide to teach me how to optimise my code and run speed tests myself? I’m new to python and coding in general so apologies if this is a basic concept.
Thanks!
Tom
EDIT: Thanks for the responses, memcache seems to be the right answer. I think that Nick and Lennart are suggesting that I add the whole gi variable to memcache. I think this is possible. FYI – the whole GeoIP.dat file is just over 1MB so not that large.
What takes time there is rather loading the database from the dat file. Once you have that in memory, the lookup time is not significant. So if you can keep the gi variable in memory that seems the best solution.
If you can’t you probably can’t use memcached either.