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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for time_to_live_dict-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4f72a006730b367bc76063faeed18dd63bdbee2d50ef241cec2dadee34d85ee7
MD5 df4a0492dcd55896a1fb493333af634b
BLAKE2b-256 4e1c86322dec104cd5211689f9279f191ddb13f82b566abefffd2b3c0a8de300

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for time_to_live_dict-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4bcc28895e2cf8184b6fc769aaf906bda10b11bb5ef872e197ae1678812983c0
MD5 a1ead2e221ffe6efda132edc5ea946dd
BLAKE2b-256 b973add89ee391d2e34b92103d4ace33b6fc9047f8eed942b3d37da50258eb54

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