Switch names of TimeoutDict and ExpiringDict.

For consistency, as TimeoutQueue has guaranteed deletion after the timeout,
like the new TimeoutDict (ex-ExpiringDict).
This commit is contained in:
Valentin Lorentz 2020-05-10 08:42:25 +02:00
parent d205a9b5f8
commit 759fca5eba
4 changed files with 21 additions and 19 deletions

View File

@ -122,7 +122,7 @@ class Fediverse(callbacks.PluginRegexp):
def __init__(self, irc):
super().__init__(irc)
self._startHttp()
self._actor_cache = utils.structures.ExpiringDict(timeout=600)
self._actor_cache = utils.structures.TimeoutDict(timeout=600)
def _startHttp(self):
callback = FediverseHttp()

View File

@ -58,7 +58,7 @@ from . import conf, ircdb, ircmsgs, ircutils, log, utils, world
from .drivers import Server
from .utils.str import rsplit
from .utils.iter import chain
from .utils.structures import smallqueue, RingBuffer, TimeoutDict
from .utils.structures import smallqueue, RingBuffer, ExpiringDict
MAX_LINE_SIZE = 512 # Including \r\n, but excluding server_tags
@ -557,7 +557,7 @@ class IrcState(IrcCommandDispatcher, log.Firewalled):
# Batches should always finish and be way shorter than 3600s, but
# let's just make sure to avoid leaking memory.
self.batches = TimeoutDict(timeout=3600)
self.batches = ExpiringDict(timeout=3600)
def reset(self):
"""Resets the state to normal, unconnected state."""

View File

@ -307,6 +307,8 @@ class smallqueue(list):
class TimeoutQueue(object):
"""A queue whose elements are dropped after a certain time."""
__slots__ = ('queue', 'timeout')
def __init__(self, timeout, queue=None):
if queue is None:
@ -458,9 +460,9 @@ class CacheDict(collections.abc.MutableMapping):
return len(self.d)
class TimeoutDict(collections.abc.MutableMapping):
class ExpiringDict(collections.abc.MutableMapping):
"""An efficient dictionary that MAY drop its items when they are too old.
For guaranteed expiry, use ExpiringDict.
For guaranteed expiry, use TimeoutDict.
Currently, this is implemented by internally alternating two "generation"
dicts, which are dropped after a certain time."""
@ -478,7 +480,7 @@ class TimeoutDict(collections.abc.MutableMapping):
return (self.__class__, (self.timeout, dict(self)))
def __repr__(self):
return 'TimeoutDict(%s, %r)' % (self.timeout, dict(self))
return 'ExpiringDict(%s, %r)' % (self.timeout, dict(self))
def __getitem__(self, key):
try:
@ -534,11 +536,11 @@ class TimeoutDict(collections.abc.MutableMapping):
return len(set(self.new_gen.keys()) | set(self.old_gen.keys()))
class ExpiringDict: # Don't inherit from MutableMapping: not thread-safe
class TimeoutDict: # Don't inherit from MutableMapping: not thread-safe
"""A dictionary that drops its items after they have been in the dict
for a certain time.
Use TimeoutDict for a more efficient implementation that doesn't require
Use ExpiringDict for a more efficient implementation that doesn't require
guaranteed timeout.
"""
__slots__ = ('_lock', 'd', 'timeout')
@ -554,7 +556,7 @@ class ExpiringDict: # Don't inherit from MutableMapping: not thread-safe
return (self.__class__, (self.timeout, dict(self)))
def __repr__(self):
return 'ExpiringDict(%s, %r)' % (self.timeout, dict(self))
return 'TimeoutDict(%s, %r)' % (self.timeout, dict(self))
def __getitem__(self, key):
with self._lock:

View File

@ -1166,16 +1166,16 @@ class TestCacheDict(SupyTestCase):
self.assertTrue(i in d)
self.assertTrue(d[i] == i)
class TestTimeoutDict(SupyTestCase):
class TestExpiringDict(SupyTestCase):
def testInit(self):
d = TimeoutDict(10)
d = ExpiringDict(10)
self.assertEqual(dict(d), {})
d['foo'] = 'bar'
d['baz'] = 'qux'
self.assertEqual(dict(d), {'foo': 'bar', 'baz': 'qux'})
def testExpire(self):
d = TimeoutDict(10)
d = ExpiringDict(10)
self.assertEqual(dict(d), {})
d['foo'] = 'bar'
timeFastForward(11)
@ -1189,8 +1189,8 @@ class TestTimeoutDict(SupyTestCase):
self.assertEqual(dict(d), {'baz': 'qux', 'quux': 42})
def testEquality(self):
d1 = TimeoutDict(10)
d2 = TimeoutDict(10)
d1 = ExpiringDict(10)
d2 = ExpiringDict(10)
self.assertEqual(d1, d2)
d1['foo'] = 'bar'
@ -1208,16 +1208,16 @@ class TestTimeoutDict(SupyTestCase):
self.assertEqual(d1, d2)
class TestExpiringDict(SupyTestCase):
class TestTimeoutDict(SupyTestCase):
def testInit(self):
d = ExpiringDict(10)
d = TimeoutDict(10)
self.assertEqual(dict(d), {})
d['foo'] = 'bar'
d['baz'] = 'qux'
self.assertEqual(dict(d), {'foo': 'bar', 'baz': 'qux'})
def testExpire(self):
d = ExpiringDict(10)
d = TimeoutDict(10)
self.assertEqual(dict(d), {})
d['foo'] = 'bar'
timeFastForward(11)
@ -1231,8 +1231,8 @@ class TestExpiringDict(SupyTestCase):
self.assertEqual(dict(d), {'quux': 42})
def testEquality(self):
d1 = ExpiringDict(10)
d2 = ExpiringDict(10)
d1 = TimeoutDict(10)
d2 = TimeoutDict(10)
self.assertEqual(d1, d2)
d1['foo'] = 'bar'