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 python_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.0.1.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ttlru_map-1.0.1.tar.gz
Algorithm Hash digest
SHA256 cf1e68dbcdbd6e749a701a799156d0a541b06bfac1e569ca8abca50049deb3a4
MD5 73d2916c1ba09a29bb71d99654ca49e2
BLAKE2b-256 65e3fd1635f64a4b70f2006e3b5fb02704b399bbdc61c501268e0f7e22fc422f

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ttlru_map-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1923e03d1b27f477508569f5109aa7320f5aad6ef7d236ff7e646fa064aec43a
MD5 61c02c01f86b56ce91a578315455d1f8
BLAKE2b-256 c19abd87d9cc3d487cf8637e11f9553f54742032ee80aa349d5cc07c54fb66e4

See more details on using hashes here.

Supported by

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