Fix scrapy.utils.datatypes.LocalCache limit issue

This commit is contained in:
Eugenio Lacuesta 2019-11-04 10:35:58 -03:00
parent f02c3d1dcf
commit 439a3e59b8
No known key found for this signature in database
GPG Key ID: DA3EF2D0913E9810
2 changed files with 30 additions and 4 deletions

View File

@ -315,8 +315,9 @@ class LocalCache(collections.OrderedDict):
self.limit = limit
def __setitem__(self, key, value):
while len(self) >= self.limit:
self.popitem(last=False)
if self.limit:
while len(self) >= self.limit:
self.popitem(last=False)
super(LocalCache, self).__setitem__(key, value)

View File

@ -7,7 +7,7 @@ if six.PY2:
else:
from collections.abc import Mapping, MutableMapping
from scrapy.utils.datatypes import CaselessDict, SequenceExclude
from scrapy.utils.datatypes import CaselessDict, SequenceExclude, LocalCache
__doctests__ = ['scrapy.utils.datatypes']
@ -242,6 +242,31 @@ class SequenceExcludeTest(unittest.TestCase):
for v in [-3, "test", 1.1]:
self.assertNotIn(v, d)
class LocalCacheTest(unittest.TestCase):
def test_cache_with_limit(self):
cache = LocalCache(limit=2)
cache['a'] = 1
cache['b'] = 2
cache['c'] = 3
self.assertEqual(len(cache), 2)
self.assertNotIn('a', cache)
self.assertIn('b', cache)
self.assertIn('c', cache)
self.assertEqual(cache['b'], 2)
self.assertEqual(cache['c'], 3)
def test_cache_without_limit(self):
max = 10**4
cache = LocalCache()
for x in range(max):
cache[str(x)] = x
self.assertEqual(len(cache), max)
for x in range(max):
self.assertIn(str(x), cache)
self.assertEqual(cache[str(x)], x)
if __name__ == "__main__":
unittest.main()