Skip to content

Commit

Permalink
only check cache and add for lookup if not bogon
Browse files Browse the repository at this point in the history
  • Loading branch information
UmanShahzad committed Jan 31, 2024
1 parent e595a4b commit 2de1c2c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
24 changes: 10 additions & 14 deletions ipinfo/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,11 @@ def getBatchDetails(
details["bogon"] = True
result[ip_address] = Details(details)
else:
lookup_addresses.append(ip_address)

try:
cached_ipaddr = self.cache[cache_key(ip_address)]
result[ip_address] = cached_ipaddr
except KeyError:
lookup_addresses.append(ip_address)
try:
cached_ipaddr = self.cache[cache_key(ip_address)]
result[ip_address] = cached_ipaddr
except KeyError:
lookup_addresses.append(ip_address)

# all in cache - return early.
if len(lookup_addresses) == 0:
Expand Down Expand Up @@ -322,13 +320,11 @@ def getBatchDetailsIter(
details["bogon"] = True
yield Details(details)
else:
lookup_addresses.append(ip_address)

try:
cached_ipaddr = self.cache[cache_key(ip_address)]
result[ip_address] = cached_ipaddr
except KeyError:
lookup_addresses.append(ip_address)
try:
cached_ipaddr = self.cache[cache_key(ip_address)]
result[ip_address] = cached_ipaddr
except KeyError:
lookup_addresses.append(ip_address)

# all in cache - exit early.
if len(lookup_addresses) == 0:
Expand Down
12 changes: 5 additions & 7 deletions ipinfo/handler_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,11 @@ async def getBatchDetailsIter(
details = {"ip": ip_address, "bogon": True}
yield Details(details)
else:
lookup_addresses.append(ip_address)

try:
cached_ipaddr = self.cache[cache_key(ip_address)]
results[ip_address] = cached_ipaddr
except KeyError:
lookup_addresses.append(ip_address)
try:
cached_ipaddr = self.cache[cache_key(ip_address)]
results[ip_address] = cached_ipaddr
except KeyError:
lookup_addresses.append(ip_address)

if not lookup_addresses:
yield results.items()
Expand Down

0 comments on commit 2de1c2c

Please sign in to comment.