Mercurial > public > mercurial-scm > hg-stable
diff mercurial/util.py @ 39582:bd9d3a89f07b
util: add a popoldest() method to lrucachedict
This allows consumers to prune the oldest item from the cache. This
could be useful for e.g. a consumer that wishes for the size of
items tracked by the cache to remain under a high water mark.
Differential Revision: https://phab.mercurial-scm.org/D4501
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 05 Sep 2018 23:15:20 -0700 |
parents | 2dcc68c7d25b |
children | ee087f0d7db5 |
line wrap: on
line diff
--- a/mercurial/util.py Thu Sep 06 11:40:20 2018 -0700 +++ b/mercurial/util.py Wed Sep 05 23:15:20 2018 -0700 @@ -1341,6 +1341,28 @@ return result + def popoldest(self): + """Remove the oldest item from the cache. + + Returns the (key, value) describing the removed cache entry. + """ + if not self._cache: + return + + # Walk the linked list backwards starting at tail node until we hit + # a non-empty node. + n = self._head.prev + while n.key is _notset: + n = n.prev + + key, value = n.key, n.value + + # And remove it from the cache and mark it as empty. + del self._cache[n.key] + n.markempty() + + return key, value + def _movetohead(self, node): """Mark a node as the newest, making it the new head.