Skip to main content

Introduction

This library provides the same experience of builtin dict, plus automatical time-to-live (TTL) management.

How it Works

Different from some other libraries, this one does not require background thread and guarantees that the expired items do not affect results.

Note that technically our TTLDict class is a subclass of MutableMapping instead of dict.

Internally, it utilizes a dict to manage expiries. Because every item has the same TTL, the insert order of the dict matches the expiry order.

How to Install

Simply run pip install time-to-live-dict.

How to Use

It works just like builtin dict:

In [1]: from ttl_dict import TTLDict

In [2]: from datetime import timedelta

In [3]: from time import sleep

In [4]: ttl = timedelta(seconds=2)
   ...:
   ...: d = TTLDict(ttl, {"a": 1})
   ...: print(d)
   ...:
   ...: sleep(1)
   ...: print(d)
   ...:
   ...: sleep(1)
   ...: print(d)
{'a': 1}
{'a': 1}
{}

In [5]: ttl1 = timedelta(seconds=2)
   ...: ttl2 = timedelta(seconds=4)
   ...:
   ...: d1 = TTLDict(ttl1, {"a": 1})
   ...: d2 = TTLDict(ttl2, {"b": 2})
   ...:
   ...: d = d1 | d2
   ...: print(d)
   ...:
   ...: sleep(2)
   ...: print(d)
   ...:
   ...: sleep(2)
   ...: print(d)
{'a': 1, 'b': 2}
{'b': 2}
{}

In additional, it offers a few new methods:

  • cleanup() cleans up all expired items. Note that this method is called automatically.
  • cleanup_by_key(key) cleans up item with specified key if it is expired. Note that this method is called automatically.
  • get_expiry(key) gets the expiry of specified key if it is not expired yet
  • renew_expiry(key) renews the expiry of specified key (as if it was just inserted into)

Metadata

Release files for time-to-live-dict 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for time-to-live-dict 0.1.4
File Size Uploaded
time_to_live_dict-0.1.4.tar.gz 43.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for time-to-live-dict 0.1.4
File Interpreter ABI Platform
time_to_live_dict-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 47.2 kB

Release files / time_to_live_dict-0.1.4.tar.gz

Download URL time_to_live_dict-0.1.4.tar.gz
Size 43.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5dc6ed5ae6c1004037996aa77f296115cc9d8fdd345e04d65a0e57a6c348c6e8
BLAKE2b-256 checksum
How to use checksums
16a3c954b055ec5c4cc8368f97c53fcdf76c19150d0cb824bf49f11d62029172
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.7

Release files / time_to_live_dict-0.1.4-py3-none-any.whl

Download URL time_to_live_dict-0.1.4-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6797ba94cefb79134d08423d87366c2d7d4e5ced1493848cee9a0fc7e2963140
BLAKE2b-256 checksum
How to use checksums
01cea596145034bc0b6211ff6d0a37a8375ed0424d584f17757082680e273a99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.7

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page