Skip to content

Commit ff6e06b

Browse files
committed
Cache database type in object
We retrieve this on every lookup. This provides a noticeable performance improvement.
1 parent 6e47815 commit ff6e06b

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

geoip2/database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def __init__(self, fileish, locales=None, mode=MODE_AUTO):
8484
if locales is None:
8585
locales = ['en']
8686
self._db_reader = maxminddb.open_database(fileish, mode)
87+
self._db_type = self._db_reader.metadata().database_type
8788
self._locales = locales
8889

8990
def __enter__(self):
@@ -180,11 +181,10 @@ def isp(self, ip_address):
180181
ip_address)
181182

182183
def _get(self, database_type, ip_address):
183-
if database_type not in self.metadata().database_type:
184+
if database_type not in self._db_type:
184185
caller = inspect.stack()[2][3]
185186
raise TypeError("The %s method cannot be used with the "
186-
"%s database" %
187-
(caller, self.metadata().database_type))
187+
"%s database" % (caller, self._db_type))
188188
(record, prefix_len) = self._db_reader.get_with_prefix_len(ip_address)
189189
if record is None:
190190
raise geoip2.errors.AddressNotFoundError(

0 commit comments

Comments
 (0)