Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG+1] DNS cache expiration. #2333

wants to merge 7 commits into
base: master

Implement ExpiringCache dict methods.

  • Loading branch information...
ArturGaspar committed Oct 24, 2016
commit b461e126176dc7b86ce189acb129c3fecefcdc5c
@@ -312,7 +312,8 @@ class ExpiringCache(LocalCache):
Dictionary with finite number of keys and time-based expiration.
Membership tests (key in dict, dict.has_key) will not trigger expiration.
Membership tests (key in dict, dict.has_key) are not guaranteed to trigger
def __init__(self, limit=None, expiration=None):
@@ -334,3 +335,24 @@ def get(self, key, default=None):
return self[key]
except KeyError:
return default

def iteritems(self):
for k in self.iterkeys():
yield k, self[k]

def itervalues(self):
for k in self.iterkeys():
yield self[k]

if six.PY2:
def items(self):
return list(self.iteritems())

def values(self):
return list(self.itervalues())
def iterkeys(self):
# For iteritems() and itervalues() above.
return self.keys()
items = iteritems
values = itervalues

This comment has been minimized.

Copy link

kmike Nov 14, 2016


I think there is little value in implementing these PY2/PY3 differences. In Python3 items() is not really a generator anyways, it is a special view object which e.g. has a repr with values. I'd have the same methods for Python 2 and Python 3 here - why introduce porting issues ourselves?

This comment has been minimized.

Copy link

ArturGaspar Dec 6, 2016

Author Contributor

items() and values() are inherited from OrderedDict. Starting with Python 3.5, they will bypass the custom __getitem__ (because OrderedDict is implemented in C now) and expose the actual (time, value) tuple.

In Python up to 3.4:

>>> list(d.items())
[('a', 1)]

In Python 3.5:

>>> list(d.items())
[('a', (1481032887.784956, 1))]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.