Browse Source

allow the tag cache count to be modified...

main
John-Mark Gurney 2 years ago
parent
commit
4761f7f43f
1 changed files with 30 additions and 2 deletions
  1. +30
    -2
      ui/medashare/tags.py

+ 30
- 2
ui/medashare/tags.py View File

@@ -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') ])

Loading…
Cancel
Save