expiringdict is a Python library that provides a dictionary-like object whose values automatically expire after a specified time-to-live (TTL). It's commonly used for caching purposes where stale data needs to be automatically removed. The current version is 1.2.2. The project appears to be stable with infrequent updates, indicating a maintenance phase rather than active feature development.
pip install expiringdictVerified import paths — ran on the pinned version, not inferred.
Initialize an ExpiringDict with a maximum length and item age, then demonstrate adding an item, retrieving it, and verifying its expiration after the TTL.
Ensure external locking mechanisms (e.g., threading.Lock) are used when accessing ExpiringDict from multiple threads, or use it in single-threaded contexts only.
To have items expire based on time, set `max_age_seconds` to a positive integer. If immediate removal is desired, manually delete the key or set a very small positive `max_age_seconds`.
If iteration is required, convert the dictionary items to a list first (e.g., `list(exp_dict.items())`) or acquire a lock around the iteration if in a multithreaded context and ensure no other threads modify the dictionary.
Ensure the library is installed in your active Python environment by running: `pip install expiringdict`
Before accessing a key, check for its presence using `key in expiring_dict` or use the `.get()` method with a default value to avoid the error. Example: `value = cache.get('foo')` or `if 'foo' in cache: value = cache['foo']`To make an ExpiringDict picklable, convert it to a standard `OrderedDict` or `dict` before pickling, and then reconstruct the `ExpiringDict` upon unpickling. Example: `import dill; from expiringdict import ExpiringDict; cache = ExpiringDict(max_len=10, max_age_seconds=60); cache['test'] = 1; picklable_cache = dict(cache); pickled_object = dill.dumps(picklable_cache); original_dict = dill.loads(pickled_object); new_cache = ExpiringDict(max_len=10, max_age_seconds=60, items=original_dict)`
No dependency data recorded yet.