{"id":1476,"library":"expiringdict","title":"expiringdict","description":"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.","status":"maintenance","version":"1.2.2","language":"en","source_language":"en","source_url":"https://github.com/akhmerov/expiringdict","tags":["cache","dictionary","expiration","ttl","key-value"],"install":[{"cmd":"pip install expiringdict","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"ExpiringDict","correct":"from expiringdict import ExpiringDict"}],"quickstart":{"code":"from expiringdict import ExpiringDict\nimport time\n\n# Create an expiring dictionary with max 100 items, and items expire after 2 seconds\ncache = ExpiringDict(max_len=100, max_age_seconds=2)\n\nprint(\"Adding 'data' to cache...\")\ncache['my_key'] = 'data_value'\nprint(f\"Retrieved immediately: {cache.get('my_key', 'Not found')}\")\n\nprint(\"Waiting 3 seconds for expiration...\")\ntime.sleep(3)\n\nprint(f\"Retrieved after expiration: {cache.get('my_key', 'Not found')}\")\n# Direct access would raise KeyError: print(cache['my_key'])","lang":"python","description":"Initialize an ExpiringDict with a maximum length and item age, then demonstrate adding an item, retrieving it, and verifying its expiration after the TTL."},"warnings":[{"fix":"Ensure external locking mechanisms (e.g., threading.Lock) are used when accessing ExpiringDict from multiple threads, or use it in single-threaded contexts only.","message":"expiringdict is explicitly not thread-safe. Concurrent access from multiple threads without external locking will lead to unpredictable behavior and potential data corruption.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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`.","message":"Setting `max_age_seconds` to 0 does not mean items expire immediately; it means items will *never* expire based on age. Only `max_len` will trigger eviction.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Iterating over an ExpiringDict while items are expiring (or being added/removed) can lead to a `RuntimeError` ('dictionary changed size during iteration') or inconsistent results, as the underlying dictionary is being modified.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}