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.3.tar.gz (43.4 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.3-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for time_to_live_dict-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e2b8a84d23e41d97656d7373ac85a9970dc92784d15bc1514df2d89e9e09a151
MD5 e61609e744e4fc9b174be4fbbf79a784
BLAKE2b-256 e4a638c1365e074e234925df664fe6b7ec1e6520d1086b10753fe17c5f734900

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for time_to_live_dict-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f01967d1ec7f84df53681e9b0e84465a7a05992212f3599d456ea4223dc0ad50
MD5 c211a7f5bfcd5b620881b3cd947c16ef
BLAKE2b-256 d0d7e3a9d6e373b0bba2e67243dc53f9725bf7765aa63805e2a74169da3a44e3

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