1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
"""Least Recently Used cache mapping.
"""
from __future__ import absolute_import, unicode_literals
import typing
from collections import OrderedDict
_K = typing.TypeVar("_K")
_V = typing.TypeVar("_V")
class LRUCache(OrderedDict, typing.Generic[_K, _V]):
"""A dictionary-like container that stores a given maximum items.
If an additional item is added when the LRUCache is full, the least
recently used key is discarded to make room for the new item.
"""
def __init__(self, cache_size):
# type: (int) -> None
"""Create a new LRUCache with the given size."""
self.cache_size = cache_size
super(LRUCache, self).__init__()
def __setitem__(self, key, value):
# type: (_K, _V) -> None
"""Store a new views, potentially discarding an old value."""
if key not in self:
if len(self) >= self.cache_size:
self.popitem(last=False)
OrderedDict.__setitem__(self, key, value)
def __getitem__(self, key):
# type: (_K) -> _V
"""Get the item, but also makes it most recent."""
_super = typing.cast(OrderedDict, super(LRUCache, self))
value = _super.__getitem__(key)
_super.__delitem__(key)
_super.__setitem__(key, value)
return value
|