Source code for boapi.cache

"""In-memory cache for raw CSV data, to avoid repeated downloads."""

from typing import Optional


[docs] class CacheManager: """In-memory cache for CSV data. The cache stores raw data to avoid querying the API several times for the same document. """ def __init__(self): """Initialize an empty cache.""" self._data_cache = {}
[docs] def get(self, key: str) -> Optional[str]: """Return a value from the cache. :param key: Data identifier (usually doc_id) :type key: str :return: Stored value, or None if absent :rtype: Optional[str] """ return self._data_cache.get(key)
[docs] def set(self, key: str, value: str): """Store a value in the cache. :param key: Data identifier :type key: str :param value: Content to store (usually CSV) :type value: str """ self._data_cache[key] = value
[docs] def has(self, key: str) -> bool: """Return whether a key is present in the cache. :param key: Identifier to check :type key: str :return: True if the key exists :rtype: bool """ return key in self._data_cache
[docs] def delete(self, key: str): """Remove an entry from the cache. :param key: Identifier to remove :type key: str """ if key in self._data_cache: del self._data_cache[key]
[docs] def clear(self): """Clear the whole cache.""" self._data_cache.clear()
[docs] def size(self) -> int: """Return the number of entries in the cache. :return: Number of cached items :rtype: int """ return len(self._data_cache)