From 4761f7f43f11a39d57a8308867d267071279aa9a Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Wed, 14 Sep 2022 10:20:35 -0700 Subject: [PATCH] allow the tag cache count to be modified... --- ui/medashare/tags.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/ui/medashare/tags.py b/ui/medashare/tags.py index 6c2592a..d815d0e 100644 --- a/ui/medashare/tags.py +++ b/ui/medashare/tags.py @@ -13,6 +13,22 @@ class TagCache: self._count = count self._modified = False + def _limit_count(self): + while len(self._cache) > self._count: + del self._cache[next(iter(self._cache.keys()))] + + @property + def count(self): + '''The number of items allowed in the cache.''' + + return self._count + + @count.setter + def count(self, v): + self._count = v + + self._limit_count() + @property def modified(self): '''Return if the cache has been modified since the last @@ -33,8 +49,7 @@ class TagCache: self._cache[tag] = None - while len(self._cache) > self._count: - del self._cache[next(iter(self._cache.keys()))] + self._limit_count() def tags(self): '''Returns the sorted list of tags in the cache.''' @@ -119,3 +134,16 @@ class _TestTagCache(unittest.TestCase): self.assertEqual(ntc.tags(), [ ('foo', 'foo'), ('whee', 'whee') ]) + ntc.count = 3 + + ntc.add(('a', 'a')) + + ntc.add(('b', 'b')) + + self.assertEqual(ntc.tags(), [ ('a', 'a'), ('b', 'b'), ('whee', 'whee') ]) + + ntc.add(('whee', 'whee')) + + ntc.count = 2 + + self.assertEqual(ntc.tags(), [ ('b', 'b'), ('whee', 'whee') ])