Skip to content

Commit

Permalink
Make the order of the parameters determanistic
Browse files Browse the repository at this point in the history
The URL parameters should be deterministic as the full URL is used as the key for caching. If the non-ordered dictionary is used the caching will miss even when a value has been stored.
  • Loading branch information
matt-oakes committed Dec 15, 2015
1 parent ef4f2d0 commit 8db2246
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions prismic/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def get_json(url, params=None, access_token=None, cache=None, ttl=None, request_
request_handler = get_using_requests
if access_token is not None:
full_params["access_token"] = access_token
full_url = url if len(full_params) == 0 else (url + "?" + urlparse.urlencode(full_params, doseq=1))
sorted_params = OrderedDict(sorted(full_params.items(), key=lambda t: t[0]))
full_url = url if len(full_params) == 0 else (url + "?" + urlparse.urlencode(sorted_params, doseq=1))
cached = cache.get(full_url)
if cached is not None:
return cached
Expand Down Expand Up @@ -70,5 +71,9 @@ def get_max_age(headers):
if expire_header is not None:
m = re.match("max-age=(\d+)", expire_header)
if m:
return int(m.group(1))
# The Prismic API is sending very high expeiery times (10 years) for articles, which
# makes it seem like it's using milliseconds rather than seconds. Some caching libraries
# (including AppEngine Memcache) sees this 10 year value as an error and refuses to
# store or retreive the cached value
return int(m.group(1)) / 1000
return None

0 comments on commit 8db2246

Please sign in to comment.