Skip to content

cache

Generic in-memory object caches.

This module provides two classes for in-memory object caching: ThreadSafeCache and AsyncCache. ThreadSafeCache is a simple thread-safe cache implementation, while AsyncCache is designed to work with asyncio.

Example
cache = get_cache_instance()
cache.add('key1', 'value1')
cache.add('key2', 'value2')
print(cache.get('key1'))  # Output: value1
print(cache.list())  # Output: ['key1', 'key2']
cache.remove('key2')
print(cache.list())  # Output: ['key1']
cache.clear()
print(cache.list())  # Output: []

Classes

AsyncCache

AsyncCache()

A simple asynchronous cache.

Functions

add async
add(key: str, obj: Any) -> None
clear async
clear() -> None
get async
get(key: str) -> Optional[Any]
list async
list() -> List[str]
remove async
remove(key: str) -> None

ThreadSafeCache

ThreadSafeCache()

A simple thread-safe cache.

Functions

add
add(key: str, obj: Any) -> None
clear
clear() -> None
get
get(key: str) -> Optional[Any]
list
list() -> List[str]
remove
remove(key: str) -> None

Functions

get_cache_instance

get_cache_instance() -> Union[ThreadSafeCache, AsyncCache]

Determines whether to use the synchronous or asynchronous version of the cache based on whether an asyncio event loop is running.

Returns: