Cacheout Caching Library

0.16.0 · active · verified Mon Apr 13

Cacheout is a comprehensive caching library for Python, offering in-memory, dictionary-based caching. It supports various eviction policies including FIFO, LIFO, LRU, MRU, LFU, and RR. Key features include configurable maximum cache size, time-to-live (TTL) expiration per entry, thread-safety, bulk operations, and memoization decorators for functions. The library is actively maintained and is currently at version 0.16.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates initializing a Cache, setting and retrieving values, using TTL, and handling missing keys with a callable default.

from cacheout import Cache
import time

# Initialize a cache with a max size and default TTL (in seconds)
cache = Cache(maxsize=100, ttl=60)

# Set a value
cache.set('key1', 'value1')

# Get a value
print(f"Value for key1: {cache.get('key1')}")

# Set a value with a custom TTL (overriding the default)
cache.set('key2', 'value2', ttl=5)

# Check cache size and keys
print(f"Cache size: {cache.size()}")
print(f"Cache keys: {cache.keys()}")

# Wait for key2 to expire
time.sleep(6)

# Try to get expired key (will return None by default)
print(f"Value for key2 after 6 seconds: {cache.get('key2')}")

# Use a callable default for missing keys (and set it if missing)
print(f"Value for key3 (missing): {cache.get('key3', default=lambda k: f'computed_for_{k}')}")
print(f"Value for key3 (now cached): {cache.get('key3')}")

view raw JSON →