my_utilities.types package¶
Submodules¶
my_utilities.types.ttl_dict module¶
- class my_utilities.types.ttl_dict.TTLDict(default_ttl=300, function_on_expired=None)[source]¶
Bases:
object- A dictionary-like container with time-to-live (TTL)
functionality for its keys. The keys in this dictionary automatically expire and get removed after a specified duration.
- Parameters:
default_ttl (int) – The default time-to-live for keys in seconds.
_dict (OrderedDict) – The main dictionary to store key-value pairs.
_timers (dict) – A dictionary to store Timer objects for each key.
function_on_expired (Callable[[Any], None] | None)
- __init__(default_ttl=300, function_on_expired=None)[source]¶
init ttl dict
- Parameters:
default_ttl (int) – The default time-to-live for keys in seconds.
function_on_expired (Callable[[Any], None] | None) – the function that will be used when the key expires
- Return type:
None
- _cleanup(key, is_expire_cleanup=False)[source]¶
method for cleaning the key
- Parameters:
key (Any) – The key used to identify the item to be cleaned up.
is_expire_cleanup (bool)
- Return type:
None
- _expire(key)[source]¶
a method for the timer that will be called when the timer expires
- Parameters:
key (Any) – The key of the item to expire from the dictionary.
- Return type:
None
- _set_ttl(key, ttl)[source]¶
Set ttl for key
- Parameters:
key (Any) – The key for which the TTL (Time To Live) is being set.
ttl (int) – The time in seconds after which the key should expire.
- Return type:
None
- extend_ttl(key)[source]¶
Attempts to extend the time-to-live (TTL) value for the specified key.
- If the key exists in the internal dictionary, it resets its TTL to the default
TTL value. If the key does not exist, a KeyError is raised.
- Parameters:
key (Any) – The key for which the TTL is to be extended.
- Raises:
KeyError – If the key is not found in the internal dictionary.
- Return type:
None
- items()[source]¶
Returns all items in the dictionary.
- Returns:
A view object that displays a list of a dictionary’s
- Return type:
tuple[list[Any], list[Any]]
- keys()[source]¶
Returns the keys of the internal dictionary.
- Returns:
A view object that displays a list of all the keys.
- Return type:
list[Any]