sagemaker.core.utilities.cache

sagemaker.core.utilities.cache#

This module defines a LRU cache class.

Classes

LRUCache(max_cache_items, ...)

Class that implements LRU cache with expiring items.

class sagemaker.core.utilities.cache.LRUCache(max_cache_items: int, expiration_horizon: timedelta, retrieval_function: Callable[[KeyType, ValType], ValType])[source]#

Bases: Generic[KeyType, ValType]

Class that implements LRU cache with expiring items.

LRU caches remove items in a FIFO manner, such that the oldest items to be used are the first to be removed. If you attempt to retrieve a cache item that is older than the expiration time, the item will be invalidated.

class Element(value: ValType, creation_time: datetime)[source]#

Bases: object

Class describes the values in the cache.

This object stores the value itself as well as a timestamp so that this element can be invalidated if it becomes too old.

clear() None[source]#

Deletes all elements from the cache.

get(key: KeyType, data_source_fallback: bool | None = True) Tuple[ValType, bool][source]#

Returns value corresponding to key in cache and boolean indicating cache hit.

Parameters:
  • key (KeyType) – Key in cache to retrieve.

  • data_source_fallback (Optional[bool]) – True if data should be retrieved if it’s stale or not in cache. Default: True.

Raises:
  • KeyError – If key is not found in cache or is outdated and

  • data_source_fallback` is False

put(key: KeyType, value: ValType | None = None) None[source]#

Adds key to cache using retrieval_function.

If value is provided, this is used instead. If the key is already in cache, the old element is removed. If the cache size exceeds the size limit, old elements are removed in order to meet the limit.

Parameters:
  • key (KeyType) – Key in cache to retrieve.

  • value (Optional[ValType]) – Value to store for key. Default: None.