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.2.0.post2.tar.gz (76.0 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.2.0.post2-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file ttlru_map-1.2.0.post2.tar.gz.

File metadata

  • Download URL: ttlru_map-1.2.0.post2.tar.gz
  • Upload date:
  • Size: 76.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ttlru_map-1.2.0.post2.tar.gz
Algorithm Hash digest
SHA256 30e58ed07019a15669f51d9069d970df32ff920f8b568469e0ca544dcadf23e8
MD5 5ffb8917ae1927aaa8b48a6a24c6a803
BLAKE2b-256 0f3c3ef72318835f8a551fa2eced1918c43ee468441976642050b1b16e61f61a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ttlru_map-1.2.0.post2.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.2.0.post2-py3-none-any.whl.

File metadata

File hashes

Hashes for ttlru_map-1.2.0.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 14b94680556fc93c5951044bc4b3d0ae00f4045d24799676b024d9b78843016a
MD5 110e08f976433d7cabe5c5e27d61c66c
BLAKE2b-256 0eec989843c28e68e08707a189a4124e8c78f99139f7424bb0ef08c4df6814d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for ttlru_map-1.2.0.post2-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