comparison 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
comparison
equal deleted inserted replaced
39581:2dcc68c7d25b 39582:bd9d3a89f07b
1338 for i in range(len(self._cache)): 1338 for i in range(len(self._cache)):
1339 result[n.key] = n.value 1339 result[n.key] = n.value
1340 n = n.prev 1340 n = n.prev
1341 1341
1342 return result 1342 return result
1343
1344 def popoldest(self):
1345 """Remove the oldest item from the cache.
1346
1347 Returns the (key, value) describing the removed cache entry.
1348 """
1349 if not self._cache:
1350 return
1351
1352 # Walk the linked list backwards starting at tail node until we hit
1353 # a non-empty node.
1354 n = self._head.prev
1355 while n.key is _notset:
1356 n = n.prev
1357
1358 key, value = n.key, n.value
1359
1360 # And remove it from the cache and mark it as empty.
1361 del self._cache[n.key]
1362 n.markempty()
1363
1364 return key, value
1343 1365
1344 def _movetohead(self, node): 1366 def _movetohead(self, node):
1345 """Mark a node as the newest, making it the new head. 1367 """Mark a node as the newest, making it the new head.
1346 1368
1347 When a node is accessed, it becomes the freshest entry in the LRU 1369 When a node is accessed, it becomes the freshest entry in the LRU