Skip to content

Commit

Permalink
Merge pull request #19 from ipinfo/uman/fix-cache-options-issue
Browse files Browse the repository at this point in the history
[#18] Properly choose defaults for cache maxsize and ttl
  • Loading branch information
UmanShahzad authored Oct 30, 2019
2 parents d20d060 + 71c08ec commit da47d76
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# IPInfo Changelog

## 3.0.0

#### Breaking Changes

- [PR #19](https://github.com/ipinfo/python/pull/19)
DefaultCache requires keyword arguments now instead of positional arguments,
in particular `maxsize` and `ttl`.

#### Bug Fix

- [PR #19](https://github.com/ipinfo/python/pull/19)
[Issue #18](https://github.com/ipinfo/python/issues/18)
An issue with the handler not being created if you provide your own custom
`maxsize`/`ttl` values has been fixed.

## 2.1.0

#### General

- Released a batch ops function on the handler called `getBatchDetails` which
accepts a list of IP addresses (or an IP address plus a path to more specific
details, e.g. `8.8.8.8/country`). See documentation on batch operations in the
Expand Down
4 changes: 2 additions & 2 deletions ipinfo/cache/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
class DefaultCache(CacheInterface):
"""Default, in-memory cache."""

def __init__(self, maxsize, ttl, **cache_options):
self.cache = cachetools.TTLCache(maxsize, ttl, **cache_options)
def __init__(self, **cache_options):
self.cache = cachetools.TTLCache(**cache_options)

def __contains__(self, key):
return self.cache.__contains__(key)
Expand Down
8 changes: 5 additions & 3 deletions ipinfo/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ def __init__(self, access_token=None, **kwargs):
self.cache = kwargs["cache"]
else:
cache_options = kwargs.get("cache_options", {})
maxsize = cache_options.get("maxsize", self.CACHE_MAXSIZE)
ttl = cache_options.get("ttl", self.CACHE_TTL)
self.cache = DefaultCache(maxsize, ttl, **cache_options)
if "maxsize" not in cache_options:
cache_options["maxsize"] = self.CACHE_MAXSIZE
if "ttl" not in cache_options:
cache_options["ttl"] = self.CACHE_TTL
self.cache = DefaultCache(**cache_options)

def getDetails(self, ip_address=None):
"""Get details for specified IP address as a Details object."""
Expand Down
19 changes: 11 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --no-index
# pip-compile --no-emit-trusted-host --no-index
#
appdirs==1.4.3 # via black
atomicwrites==1.3.0 # via pytest
attrs==19.1.0 # via black, pytest
attrs==19.3.0 # via black, pytest
black==19.3b0
cachetools==3.1.1
certifi==2019.3.9 # via requests
certifi==2019.9.11 # via requests
chardet==3.0.4 # via requests
click==7.0 # via black, pip-tools
idna==2.8 # via requests
importlib-metadata==0.18 # via pluggy
more-itertools==7.0.0 # via pytest
importlib-metadata==0.23 # via pluggy
more-itertools==7.2.0 # via pytest, zipp
pip-tools==3.7.0
pluggy==0.12.0 # via pytest
pluggy==0.13.0 # via pytest
py==1.8.0 # via pytest
pytest==4.5.0
requests==2.22.0
six==1.12.0 # via pip-tools, pytest
toml==0.10.0 # via black
urllib3==1.25.3 # via requests
urllib3==1.25.6 # via requests
wcwidth==0.1.7 # via pytest
zipp==0.5.1 # via importlib-metadata
zipp==0.6.0 # via importlib-metadata

# The following packages are considered to be unsafe in a requirements file:
# setuptools==41.6.0 # via pytest
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="ipinfo",
version="2.1.0",
version="3.0.0",
description="Official Python library for IPInfo",
long_description=long_description,
url="https://github.com/ipinfo/python",
Expand Down
2 changes: 1 addition & 1 deletion tests/default_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def _get_new_cache():
maxsize = 4
ttl = 8
return DefaultCache(maxsize, ttl)
return DefaultCache(maxsize=maxsize, ttl=ttl)


def test_contains():
Expand Down

0 comments on commit da47d76

Please sign in to comment.