1553 Unlike get(), this doesn't mutate the internal state. But be aware |
1552 Unlike get(), this doesn't mutate the internal state. But be aware |
1554 that it doesn't mean peek() is thread safe. |
1553 that it doesn't mean peek() is thread safe. |
1555 """ |
1554 """ |
1556 try: |
1555 try: |
1557 node = self._cache[k] |
1556 node = self._cache[k] |
1558 assert node is not None # help pytype |
|
1559 return node.value |
1557 return node.value |
1560 except KeyError: |
1558 except KeyError: |
1561 if default is _notset: |
1559 if default is _notset: |
1562 raise |
1560 raise |
1563 return default |
1561 return default |
1612 |
1610 |
1613 # Walk the linked list backwards starting at tail node until we hit |
1611 # Walk the linked list backwards starting at tail node until we hit |
1614 # a non-empty node. |
1612 # a non-empty node. |
1615 n = self._head.prev |
1613 n = self._head.prev |
1616 |
1614 |
1617 assert n is not None # help pytype |
|
1618 |
|
1619 while n.key is _notset: |
1615 while n.key is _notset: |
1620 n = n.prev |
1616 n = n.prev |
1621 |
|
1622 assert n is not None # help pytype |
|
1623 |
1617 |
1624 key, value = n.key, n.value |
1618 key, value = n.key, n.value |
1625 |
1619 |
1626 # And remove it from the cache and mark it as empty. |
1620 # And remove it from the cache and mark it as empty. |
1627 del self._cache[n.key] |
1621 del self._cache[n.key] |
1628 self.totalcost -= n.cost |
1622 self.totalcost -= n.cost |
1629 n.markempty() |
1623 n.markempty() |
1630 |
1624 |
1631 return key, value |
1625 return key, value |
1632 |
1626 |
1633 def _movetohead(self, node): |
1627 def _movetohead(self, node: _lrucachenode): |
1634 """Mark a node as the newest, making it the new head. |
1628 """Mark a node as the newest, making it the new head. |
1635 |
1629 |
1636 When a node is accessed, it becomes the freshest entry in the LRU |
1630 When a node is accessed, it becomes the freshest entry in the LRU |
1637 list, which is denoted by self._head. |
1631 list, which is denoted by self._head. |
1638 |
1632 |