### Install with pip Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Install the package using pip. ```bash pip install expiringdictx ``` -------------------------------- ### ExpiringDict usage example Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Demonstrates creating an ExpiringDict with capacity and default_age, adding items with default or custom expiration times (seconds or timedelta), retrieving items, getting TTL and DDL, and creating new dictionaries via fromkeys, frommapping, and fromexpiringdict. ```python from datetime import timedelta from expiringdictx import ExpiringDict # 创建一个 ExpiringDict 实例 | Create an ExpiringDict instance cache = ExpiringDict[str, str](capacity=100, default_age=60) # 添加一个元素,过期时间为默认值 | Add an item with the default expiration time cache["key0"] = "value0" # 添加一个元素,过期时间为 30 秒 | Add an item with an expiration time of 30 seconds cache["key1", 30] = "value1" # 添加一个元素,过期时间为 2 分钟 | Add an item with an expiration time of 2 minutes cache["key2", timedelta(minutes=2)] = "value2" # 或者使用 set 方法 | Or use the set method cache.set("key3", "value3", 60) # 获取一个元素 | Get an item value0 = cache["key0"] value1 = cache.get('key1') # 获取时附带过期时间 | Get with expiration time value2, deadtime2 = cache.get_with_deadtime('key2') # 获取并删除一个元素 | pop an item value3 = cache.pop('key3') value4 = cache.popitem(least_recently=True) # 获取元素ttl(剩余存活时间) | Get the ttl(time to live) of an item ttl = cache.ttl('key0') # 获取元素ddl(过期时间) | Get the ddl(deadline) of an item ddl = cache.ddl('key0') # 按照给定keys创建一个新的ExpiringDict | Create a new ExpiringDict with the given keys new_cache = cache.fromkeys(['key0', 'key1', 'key2'], "default_value", 60) # 按照给定可映射对象创建一个新的ExpiringDict | Create a new ExpiringDict with the given mapping new_cache = cache.frommapping({'key0': 'value0', 'key1': ('value1', 1), 'key2': ('value2', timedelta(hours=2))}, 60) # 从另一个ExpiringDict创建一个新的ExpiringDict | Create a new ExpiringDict from another ExpiringDict new_cache = cache.fromexpiringdict(cache, 60) ``` -------------------------------- ### Install with pdm Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Install the package using pdm. ```bash pdm add expiringdictx ``` -------------------------------- ### Install with poetry Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Install the package using poetry. ```bash poetry add expiringdictx ``` -------------------------------- ### Run unit tests Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Run the unit tests. ```bash pytest tests/ ``` -------------------------------- ### ExpiringDict.fromkeys Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Creates a new ExpiringDict with the given keys and a default value and expiration time. ```APIDOC ## [ExpiringDict] [fromkeys] ### Description Creates a new ExpiringDict with the given keys and a default value and expiration time. ### Method fromkeys ### Endpoint cache.fromkeys(keys, value, age) ### Parameters #### Parameters - **keys** (list) - Required - List of keys for the new dictionary. - **value** (str) - Required - Default value for each key. - **age** (int) - Required - Expiration time in seconds for each item. ### Request Example ```python new_cache = cache.fromkeys(['key0', 'key1', 'key2'], "default_value", 60) ``` ### Response #### Success Response - **new_cache** (ExpiringDict) - A new ExpiringDict with the specified keys and values. #### Response Example ```python new_cache = cache.fromkeys(['key0', 'key1', 'key2'], "default_value", 60) ``` ``` -------------------------------- ### ExpiringDict constructor Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Constructor for creating an ExpiringDict instance with capacity and default expiration time. ```APIDOC ## [ExpiringDict] [constructor] ### Description Creates a new ExpiringDict instance with a specified capacity and default expiration time. ### Method Constructor ### Endpoint ExpiringDict(capacity: int, default_age: int) ### Parameters #### Constructor Parameters - **capacity** (int) - Required - Maximum number of items the dictionary can hold. - **default_age** (int) - Required - Default expiration time in seconds for items added without an explicit expiration. ### Request Example ```python cache = ExpiringDict[str, str](capacity=100, default_age=60) ``` ### Response #### Success Response - **ExpiringDict** - A new ExpiringDict instance. #### Response Example ```python cache = ExpiringDict[str, str](capacity=100, default_age=60) ``` ``` -------------------------------- ### ExpiringDict.frommapping Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Creates a new ExpiringDict from a mapping object, where values can be either a value or a tuple of (value, age). ```APIDOC ## [ExpiringDict] [frommapping] ### Description Creates a new ExpiringDict from a mapping object, where values can be either a value or a tuple of (value, age). ### Method frommapping ### Endpoint cache.frommapping(mapping, default_age) ### Parameters #### Parameters - **mapping** (dict) - Required - A dictionary where keys are the new keys and values are either a value or a tuple (value, age). - **default_age** (int) - Required - Default expiration time in seconds for items without an explicit age. ### Request Example ```python new_cache = cache.frommapping({'key0': 'value0', 'key1': ('value1', 1), 'key2': ('value2', timedelta(hours=2))}, 60) ``` ### Response #### Success Response - **new_cache** (ExpiringDict) - A new ExpiringDict with the specified items. #### Response Example ```python new_cache = cache.frommapping({'key0': 'value0', 'key1': ('value1', 1), 'key2': ('value2', timedelta(hours=2))}, 60) ``` ``` -------------------------------- ### ExpiringDict.set Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Adds an item to the ExpiringDict with a specified expiration time. ```APIDOC ## [ExpiringDict] [set] ### Description Adds an item to the dictionary with a specified expiration time. ### Method set ### Endpoint cache.set(key, value, age) ### Parameters #### Parameters - **key** (str) - Required - The key under which the value is stored. - **value** (str) - Required - The value to store. - **age** (int) - Required - Expiration time in seconds. ### Request Example ```python cache.set("key3", "value3", 60) ``` ### Response #### Success Response No return value. #### Response Example ```python cache.set("key3", "value3", 60) ``` ``` -------------------------------- ### ExpiringDict.ddl Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Returns the deadline (expiration time) of the item with the given key. ```APIDOC ## [ExpiringDict] [ddl] ### Description Returns the deadline (expiration time) of the item with the given key. ### Method ddl ### Endpoint cache.ddl(key) ### Parameters #### Parameters - **key** (str) - Required - The key whose deadline is to be retrieved. ### Request Example ```python ddl = cache.ddl('key0') ``` ### Response #### Success Response - **ddl** (datetime) - The expiration time of the item. #### Response Example ```python ddl = cache.ddl('key0') ``` ``` -------------------------------- ### ExpiringDict.__getitem__ Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Retrieves the value associated with the given key. ```APIDOC ## [ExpiringDict] [getitem] ### Description Retrieves the value associated with the given key. ### Method __getitem__ ### Endpoint cache[key] ### Parameters #### Parameters - **key** (str) - Required - The key whose value is to be retrieved. ### Request Example ```python value0 = cache["key0"] ``` ### Response #### Success Response - **value** (str) - The value associated with the key. #### Response Example ```python value0 = cache["key0"] ``` ``` -------------------------------- ### ExpiringDict.ttl Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Returns the time-to-live (remaining lifetime) of the item with the given key. ```APIDOC ## [ExpiringDict] [ttl] ### Description Returns the time-to-live (remaining lifetime) of the item with the given key. ### Method ttl ### Endpoint cache.ttl(key) ### Parameters #### Parameters - **key** (str) - Required - The key whose TTL is to be retrieved. ### Request Example ```python ttl = cache.ttl('key0') ``` ### Response #### Success Response - **ttl** (int) - The remaining time to live in seconds. #### Response Example ```python ttl = cache.ttl('key0') ``` ``` -------------------------------- ### ExpiringDict.fromexpiringdict Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Creates a new ExpiringDict from another ExpiringDict, with a specified default expiration time. ```APIDOC ## [ExpiringDict] [fromexpiringdict] ### Description Creates a new ExpiringDict from another ExpiringDict, with a specified default expiration time. ### Method fromexpiringdict ### Endpoint cache.fromexpiringdict(other, default_age) ### Parameters #### Parameters - **other** (ExpiringDict) - Required - The source ExpiringDict to copy items from. - **default_age** (int) - Required - Default expiration time in seconds for the new dictionary. ### Request Example ```python new_cache = cache.fromexpiringdict(cache, 60) ``` ### Response #### Success Response - **new_cache** (ExpiringDict) - A new ExpiringDict with the same items as the source. #### Response Example ```python new_cache = cache.fromexpiringdict(cache, 60) ``` ``` -------------------------------- ### ExpiringDict.get Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Retrieves the value associated with the given key, returning None if not found. ```APIDOC ## [ExpiringDict] [get] ### Description Retrieves the value associated with the given key. Returns None if the key is not found. ### Method get ### Endpoint cache.get(key) ### Parameters #### Parameters - **key** (str) - Required - The key whose value is to be retrieved. ### Request Example ```python value1 = cache.get('key1') ``` ### Response #### Success Response - **value** (str) - The value associated with the key, or None if not found. #### Response Example ```python value1 = cache.get('key1') ``` ``` -------------------------------- ### ExpiringDict.__setitem__ Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Adds an item to the ExpiringDict with an optional expiration time. ```APIDOC ## [ExpiringDict] [setitem] ### Description Adds an item to the dictionary with an optional expiration time. If no expiration is provided, the default expiration time is used. ### Method __setitem__ ### Endpoint cache[key] = value cache[key, age] = value ### Parameters #### Parameters - **key** (str) - Required - The key under which the value is stored. - **value** (str) - Required - The value to store. - **age** (int or timedelta) - Optional - Expiration time in seconds or as a timedelta. If omitted, the default expiration time is used. ### Request Example ```python cache["key0"] = "value0" cache["key1", 30] = "value1" cache["key2", timedelta(minutes=2)] = "value2" ``` ### Response #### Success Response No return value. #### Response Example ```python cache["key0"] = "value0" ``` ``` -------------------------------- ### ExpiringDict.get_with_deadtime Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Retrieves the value and its expiration time for the given key. ```APIDOC ## [ExpiringDict] [get_with_deadtime] ### Description Retrieves the value and its expiration time (deadline) for the given key. ### Method get_with_deadtime ### Endpoint cache.get_with_deadtime(key) ### Parameters #### Parameters - **key** (str) - Required - The key whose value and deadline are to be retrieved. ### Request Example ```python value2, deadtime2 = cache.get_with_deadtime('key2') ``` ### Response #### Success Response - **value** (str) - The value associated with the key. - **deadtime** (datetime) - The expiration time of the item. #### Response Example ```python value2, deadtime2 = cache.get_with_deadtime('key2') ``` ``` -------------------------------- ### ExpiringDict.popitem Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Removes and returns an item from the dictionary, either least recently used or most recently used. ```APIDOC ## [ExpiringDict] [popitem] ### Description Removes and returns an item from the dictionary. The item removed can be the least recently used or the most recently used depending on the parameter. ### Method popitem ### Endpoint cache.popitem(least_recently=True) ### Parameters #### Parameters - **least_recently** (bool) - Optional - If True, removes the least recently used item; if False, removes the most recently used item. Default is True. ### Request Example ```python value4 = cache.popitem(least_recently=True) ``` ### Response #### Success Response - **value** (str) - The value of the removed item. #### Response Example ```python value4 = cache.popitem(least_recently=True) ``` ``` -------------------------------- ### ExpiringDict.pop Source: https://github.com/azidecupric/expiringdictx/blob/main/README.md Removes and returns the value associated with the given key. ```APIDOC ## [ExpiringDict] [pop] ### Description Removes and returns the value associated with the given key. ### Method pop ### Endpoint cache.pop(key) ### Parameters #### Parameters - **key** (str) - Required - The key whose value is to be removed and returned. ### Request Example ```python value3 = cache.pop('key3') ``` ### Response #### Success Response - **value** (str) - The value associated with the key. #### Response Example ```python value3 = cache.pop('key3') ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.