tgram_dnd.caching package

Submodules

tgram_dnd.caching.base module

class tgram_dnd.caching.base.BaseCache[source]

Bases: object

get(key: str) Any | None[source]

get the CachedItem by key if has not expierd

Parameters:

key (str) – the item key

Returns:

Optional[Any]

get_or_create(key: str, value: Callable, ttl: int = None) CachedItem[source]

get the CachedItem by key, and if not exists a new CachedItem will be created and returned

Parameters:
  • ket (str) – the item key

  • value (Callable) – the function that will retrive the item value (in case the item does not exist/expired)

  • ttl (int, optional) – item time_to_live, defaults to self.default_ttl

Returns:

tgram_dnd.caching.cached_item.CachedItem

set(key: str, value: Any, ttl: int = None) CachedItem | None[source]

create a CachedItem with the ttl specified or the default_tll

Parameters:
  • key (str) – the item key

  • value (Any) – the cached value

  • ttl (int, optional) – item time_to_live, defaults to self.default_ttl

Returns:

Optional[tgram_dnd.caching.cached_item.CachedItem]

tgram_dnd.caching.cached_item module

class tgram_dnd.caching.cached_item.CachedItem(value: Any, ttl: int = -1)[source]

Bases: object

a Cached Item

Parameters:
  • value (Any) – the stored Value

  • ttl (int) – the time_to_live for the item

get() Any | None[source]

used to return the item value if it has not expired yet.

property has_expired: bool

check if the item has passed its ttl or not

tgram_dnd.caching.memory module

class tgram_dnd.caching.memory.MemoryCache(default_ttl: int = 10)[source]

Bases: BaseCache

A Memory cache to store temporary items

Parameters:

default_ttl (int, optional) – the default time_to_live in seconds for each item, defaults to 10

get(key: str) Any | None[source]

get the CachedItem by key if has not expired

Parameters:

key (str) – the item key

Returns:

the cached value

Return type:

Optional[Any]

async get_or_create(key: str, value: Callable, ttl: int = None) CachedItem[source]

get the CachedItem by key, and if not exists a new CachedItem will be created and returned

Parameters:
  • ket (str) – the item key

  • value (Callable) – the function that will retrieve the item value (in case the item does not exist/expired)

  • ttl (int, optional) – item time_to_live, defaults to self.default_ttl

Returns:

The cached Value

Return type:

Any

set(key: str, value: Any, ttl: int = None) CachedItem | None[source]

create a CachedItem with the ttl specified or the default_tll

Parameters:
  • key (str) – the item key

  • value (Any) – the cached value

  • ttl (int, optional) – item time_to_live, defaults to self.default_ttl

Returns:

Optional[tgram_dnd.caching.cached_item.CachedItem]

Module contents