Skip to main content

Custom Mapping with TTL and LRU support.

Project description

Python TTL Dict

Documentation Status license tests Pre-commit Mypy

Installation

Installation is available using pip install ttlru-map.

Core Features

  • TTL Dict: A thread-safe dictionary that automatically removes keys after a certain amount of time or if max size is reached.
  • It can be used as a simple in-memory cache.
  • Simple - The TTLMap derives MutableMapping and implements the same interface as the built-in dict class. It can be used as a drop-in replacement for dict.
  • Efficient - The TTLMap is designed to be efficient in terms of both time and space complexity.
  • Thread-safe - The TTLMap is thread-safe. It can be used in multithreaded applications without any additional synchronization.
  • Lazy - The TTLMap is lazy. It does not spawn any additional threads or processes. Computing the time-to-live is done only when the TTLMap is accessed.
  • Zero dependencies - The TTLMap has no dependencies other than the Python standard library.
  • LRU support - The TTLMap supports LRU (least recently used) eviction policy. It can be configured to evict either the last set item or the least recently accessed item.

Usage Examples

from datetime import timedelta
from time import sleep

from ttlru_map import TTLMap

cache: TTLMap[str, str] = TTLMap(ttl=timedelta(seconds=10))

cache['key'] = 'value'
print(cache['key'])  # 'value'

# Wait for 10 seconds
sleep(10)
print(cache['key'])  # KeyError

If you want to add LRU functionality to your cache, you can maxsize argument

from ttlru_map import TTLMap

cache: TTLMap[str, str] = TTLMap(ttl=None, max_size=2)

cache['key1'] = 'value1'
cache['key2'] = 'value2'
cache['key3'] = 'value3'

print(cache.get('key1'))  # None
print(cache.get('key2'))  # 'value2'
print(cache.get('key3'))  # 'value3'

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

ttlru_map-1.1.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

ttlru_map-1.1.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file ttlru_map-1.1.0.tar.gz.

File metadata

  • Download URL: ttlru_map-1.1.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for ttlru_map-1.1.0.tar.gz
Algorithm Hash digest
SHA256 97f8b42cbfdf6ab64e9a01356c1d0dd8718b49807314765f60fb9c71799f543c
MD5 d4b6799b4db72dc730269daeedd2a657
BLAKE2b-256 f50b56c2ea263e2ee16c756ea786393659a1b7719c885ecc28dd5eb9c50c37b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for ttlru_map-1.1.0.tar.gz:

Publisher: publish.yml on zoola969/python_ttlru_map

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ttlru_map-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ttlru_map-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for ttlru_map-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4b4c9214db94d640a46c82e9764236b0265fb0740d388fa115a826060c27f39
MD5 a78418cf605b3bee707315a4d3fea321
BLAKE2b-256 486d0c1f58c1ca827a860aba80afd7a6260e4742095a3177d1f8cf48e8a25447

See more details on using hashes here.

Provenance

The following attestation bundles were made for ttlru_map-1.1.0-py3-none-any.whl:

Publisher: publish.yml on zoola969/python_ttlru_map

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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