cachetools
Provides cache mappings and decorators for function results.
- Downloads / mo
- 280.0M
- Stars
- 2.8K
- Last release
- 8.0.2
- License
- MIT
Strengths
- Bounds retained cache values. Set maxsize to control how much the cache retains.
- Supports custom value sizing. Use getsizeof when the cache budget should count value sizes.
Limitations
- Shared caches need synchronization. Protect a shared cache with a lock when multiple threads access it.
- Expired entries can retain memory. Call expire to remove expired entries during read-only use.
Quickstart
from cachetools import TTLCache cache = TTLCache(maxsize=128, ttl=60) cache["answer"] = 42 print(cache["answer"])
cachetools examples
def __init__(self, persist_storage: CacheStorage, context: CacheStorageContext): self.function_key = context.function_key self.function_display_name = context.function_display_name self._ttl_seconds = context.ttl_seconds self._max_entries = context.max_entries self._mem_cache: TTLCache[str, bytes] = TTLCache( maxsize=self.max_entries, ttl=self.ttl_seconds, timer=cache_utils.TTLCACHE_TIMER, ) self._mem_cache_lock = threading.Lock() self._persist_storage = persist_storage