Skip to main content

No project description provided

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

time_to_live_dict-0.1.4.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

time_to_live_dict-0.1.4-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file time_to_live_dict-0.1.4.tar.gz.

File metadata

  • Download URL: time_to_live_dict-0.1.4.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.7

File hashes

Hashes for time_to_live_dict-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5dc6ed5ae6c1004037996aa77f296115cc9d8fdd345e04d65a0e57a6c348c6e8
MD5 85c62f72eecf82aedf14c8f2206ec83e
BLAKE2b-256 16a3c954b055ec5c4cc8368f97c53fcdf76c19150d0cb824bf49f11d62029172

See more details on using hashes here.

File details

Details for the file time_to_live_dict-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for time_to_live_dict-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6797ba94cefb79134d08423d87366c2d7d4e5ced1493848cee9a0fc7e2963140
MD5 7fb633423be69a8584f0bf514e244ea3
BLAKE2b-256 01cea596145034bc0b6211ff6d0a37a8375ed0424d584f17757082680e273a99

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page