Add cached property to the response by mtzfederico · Pull Request #54 · ipinfo/python · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
5 changes: 4 additions & 1 deletion ipinfo/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def getDetails(self, ip_address=None, timeout=None):
# check cache first.
try:
cached_ipaddr = self.cache[cache_key(ip_address)]
cached_ipaddr["cached"] = True
return Details(cached_ipaddr)
except KeyError:
pass
Expand All @@ -101,7 +102,7 @@ def getDetails(self, ip_address=None, timeout=None):
# format & cache
handler_utils.format_details(details, self.countries)
self.cache[cache_key(ip_address)] = details

details["cached"] = False
return Details(details)

def getBatchDetails(
Expand Down Expand Up @@ -160,6 +161,7 @@ def getBatchDetails(

try:
cached_ipaddr = self.cache[cache_key(ip_address)]
cached_ipaddr["cached"] = True
result[ip_address] = cached_ipaddr
except KeyError:
lookup_addresses.append(ip_address)
Expand Down Expand Up @@ -208,6 +210,7 @@ def getBatchDetails(
json_response = response.json()
for ip_address, details in json_response.items():
self.cache[cache_key(ip_address)] = details
details["cached"] = False

# merge cached results with new lookup
result.update(json_response)
Expand Down
4 changes: 4 additions & 0 deletions ipinfo/handler_async.py