Skip to content

zoola969/python_ttlru_map

Repository files navigation

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'

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages