"""A bounded, thread-safe mutable mapping with least-recently-used eviction.""" from collections.abc import ( Hashable, ItemsView, Iterator, KeysView, MutableMapping, ValuesView, ) from operator import index from types import GenericAlias from typing import TYPE_CHECKING, SupportsIndex, TypeVar, final, overload from lrucache_rs._lib import Cache as _Cache from lrucache_rs._views import CacheItemsView, CacheKeysView, CacheValuesView __all__ = ('LRUCache',) _K = TypeVar('_K', bound=Hashable) _V = TypeVar('_V') _D = TypeVar('_D') @final class LRUCache(_Cache, MutableMapping[_K, _V]): """A mutable mapping with a fixed positive maximum number of entries. Item lookup, get, assignment and setdefault mark a key most recently used. Peek, containment, iteration, views and equality do not change recency. Inserting beyond maxsize evicts the least-recently-used entry. Iterators snapshot entries in least-to-most-recently-used order. Individual native operations are thread-safe, including pop and setdefault. Update processes its input incrementally; it is not an atomic transaction. Key equality and object finalizers may reenter the cache. Equality that continually changes membership may prevent a lookup from stabilizing. Parameters ---------- maxsize : SupportsIndex Positive maximum entry count within the native index range. Raises ------ ValueError If ``maxsize`` is nonpositive or exceeds the native capacity limit. TypeError If ``maxsize`` does not support Python's integer index protocol (``__index__``). """ __slots__ = () __hash__ = None # type: ignore[assignment] def __new__(cls, maxsize: SupportsIndex) -> 'LRUCache[_K, _V]': return super().__new__(cls, index(maxsize)) if not TYPE_CHECKING: @classmethod def __class_getitem__(cls, _item: object) -> GenericAlias: return GenericAlias(cls, _item) def __getitem__(self, key: _K, /) -> _V: """Return the value for ``key`` and mark it most recently used. Raises ------ KeyError If ``key`` is not in the cache. TypeError If ``key`` is unhashable. """ entry: tuple[_K, _V] | None = self._get(key) if entry is None: raise KeyError(key) return entry[1] def __setitem__(self, key: _K, value: _V, /) -> None: """Store ``value`` for ``key`` and mark it most recently used. Evicts the least-recently-used entry when inserting into a full cache. Raises ------ TypeError If ``key`` is unhashable. """ self._set(key, value) def __delitem__(self, key: _K, /) -> None: """Remove ``key`` from the cache. Raises ------ KeyError If ``key`` is not in the cache. TypeError If ``key`` is unhashable. """ entry = self._remove(key) if entry is None: raise KeyError(key) @overload def get(self, key: _K, /, default: None = None) -> _V | None: ... @overload def get(self, key: _K, /, default: _V) -> _V: ... @overload def get(self, key: _K, /, default: _D) -> _V | _D: ... def get(self, key: _K, /, default: _D | None = None) -> _V | _D | None: """Retrieve a value and mark a present key most recently used. Raises ------ TypeError If ``key`` is unhashable. """ entry: tuple[_K, _V] | None = self._get(key) return default if entry is None else entry[1] @overload def peek(self, key: _K, /, default: None = None) -> _V | None: ... @overload def peek(self, key: _K, /, default: _V) -> _V: ... @overload def peek(self, key: _K, /, default: _D) -> _V | _D: ... def peek(self, key: _K, /, default: _D | None = None) -> _V | _D | None: """Retrieve a value without changing recency. Raises ------ TypeError If ``key`` is unhashable. """ entry: tuple[_K, _V] | None = self._peek(key) return default if entry is None else entry[1] @overload def pop(self, key: _K, /) -> _V: ... @overload def pop(self, key: _K, default: _V, /) -> _V: ... @overload def pop(self, key: _K, default: _D, /) -> _V | _D: ... def pop(self, key: _K, /, *default: _D) -> _V | _D: """Remove a key atomically, returning its value or the supplied default. Raises ------ KeyError If ``key`` is not in the cache and no default was provided. TypeError If ``key`` is unhashable or more than one default is supplied. """ if len(default) > 1: raise TypeError('pop accepts at most one default') entry: tuple[_K, _V] | None = self._remove(key) if entry is not None: return entry[1] if default: return default[0] raise KeyError(key) @overload def setdefault(self, key: _K, /, default: None = None) -> _V | None: ... @overload def setdefault(self, key: _K, /, default: _V) -> _V: ... def setdefault(self, key: _K, /, default: _V | None = None) -> _V | None: """Return and touch an existing value, or atomically insert the default. Raises ------ TypeError If ``key`` is unhashable. """ entry: tuple[_K, _V | None] = self._setdefault(key, default) return entry[1] def items(self) -> ItemsView[_K, _V]: """Return a live items view with non-touching snapshot iteration.""" return CacheItemsView(self) def __iter__(self) -> Iterator[_K]: """Snapshot keys from least to most recently used.""" return iter(self._snapshot_elements(values=False)) def keys(self) -> KeysView[_K]: """Return a live keys view with non-touching snapshot iteration.""" return CacheKeysView(self) def popitem(self) -> tuple[_K, _V]: """Remove and return the least-recently-used ``(key, value)`` pair. Raises ------ KeyError If the cache is empty. """ entry = self._popitem() if entry is None: raise KeyError('popitem(): cache is empty') return entry def values(self) -> ValuesView[_V]: """Return a live values view with non-touching snapshot iteration.""" return CacheValuesView(self)