Skip to main content

Thread-safe in-process cache for Python with eviction policies and TTL

Project description

cachelocal

A thread-safe in-process cache for Python with multiple eviction policies and optional TTL support.

Features

  • Multiple eviciton policies: LRU, FIFO
  • Optional per-entry TTL (lazy expiration)
  • Thread-safe via a single lock
  • O(1) get/set/delete operations
  • Optional stats tracking (hits, misses, evictions)

Installation

pip install cachelocal

Quickstart

LRU Cache

from cachelocal import LRUCache

cache = LRUCache(capacity=2)

cache.set("a", 1)
cache.set("b", 2)

cache.get("a")          # -> 1 (refreshes recency)
cache.set("c", 3)       # evicts "b"

cache.get("b")          # -> None

FIFO Cache

from cachelocal import FIFOCache

cache = FIFOCache(capacity=2)

cache.set("a", 1)
cache.set("b", 2)

cache.get("a")          # -> 1 (does NOT refresh position)
cache.set("c", 3)       # evicts "a" (oldest insertion)

cache.get("a")          # -> None

TTL

cache.set("x", 42, ttl_seconds=1.0)

Expired entries are removed on access and may also be removed during eviction when capacity is exceeded.

Stats Tracking

cache = LRUCache(capacity=100, track_stats=True)

cache.set("a", 1)
cache.get("a")  # hit
cache.get("b")  # miss

stats = cache.get_stats()
print(f"Hits: {stats.hits}, Misses: {stats.misses}, Evictions: {stats.evictions}")

Performance

Benchmarks on Apple M-series (100k operations):

Read-Only Workload

Implementation ops/sec vs functools
FIFOCache 3.7M 35%
LRUCache 2.5M 24%
functools.lru_cache 10.6M baseline

Mixed Workload (stats disabled)

Implementation Writes Reads Mixed
FIFOCache 3.7M ops/s 3.7M ops/s 3.3M ops/s
LRUCache 2.3M ops/s 2.7M ops/s 2.4M ops/s

Key takeaways:

  • FIFO is ~48% faster than LRU (no recency updates on access)
  • Stats tracking overhead: ~5-10%
  • Both implementations handle millions of operations per second

Run benchmarks yourself:

python benchmarks/benchmark.py

Semantics

LRU Cache

  • Eviction policy: Least Recently Used
  • Access behavior: get() and set() refresh recency
  • Best for: General-purpose caching with access-based eviction

FIFO Cache

  • Eviction policy: First In, First Out
  • Access behavior: get() does NOT refresh position
  • Best for: Time-windowed data, session caches

Both

  • TTL: Optional, checked on access (lazy eviction)
  • Thread safety: Single lock guards hashmap and linked list
  • Missing/expired keys: get() returns None

Complexity

  • get(): O(1)
  • set(): O(1)
  • delete(): O(1)

Development

pip install -e ".[dev]"
pytest

Roadmap

  • Additional eviction policies (LFU, ARC)
  • Optional sharding for reduced lock contention
  • Async support

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

cachelocal-0.2.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cachelocal-0.2.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file cachelocal-0.2.0.tar.gz.

File metadata

  • Download URL: cachelocal-0.2.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for cachelocal-0.2.0.tar.gz
Algorithm Hash digest
SHA256 24cc906bffa5668977e7dc87202e74178a6506323e9a1717b4b8f0da715dfd7e
MD5 086a91cd85cbd96faa031908189fbb14
BLAKE2b-256 b0aecd101fecfa499a70097f58255af238f7e9ef5ca868e61103f21bac602380

See more details on using hashes here.

File details

Details for the file cachelocal-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cachelocal-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for cachelocal-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f2a7331647a4bdb5ecfd2b3c21d4ac454cc5fdc6152d51c331dbaa2313c6d05
MD5 91a7e0aaa05a695bf129f6338e07b1a4
BLAKE2b-256 d6676e22d99b0df50f1d3dba2f7bf979975f1379faf85c357c917b339a70dff6

See more details on using hashes here.

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