CacheForge
CacheForge is a high-performance, zero-dependency caching, tiered storage, and cache stampede protection toolkit for Python. Built strictly with standard library primitives, it combines multi-strategy in-memory algorithms (LRU, LFU, FIFO, TTL, Sliding TTL), persistent disk caching backed by SQLite WAL mode, and request coalescing (SingleFlight) to prevent thundering herd failures.
Key Features
- Zero External Dependencies: Pure Python standard library. No native compilation hurdles, zero bloated dependency trees.
- Multiple Eviction Policies:
LRUCache: Least Recently Used eviction with capacity, max-cost, and TTL bounds.LFUCache: $O(1)$ Least Frequently Used eviction with deterministic LRU tie-breaking.FIFOCache: Strictly chronological First-In, First-Out queue eviction.TTLCache: Time-To-Live expiration with active/passive pruning.SlidingTTLCache: Sliding inactivity window (resets expiration on read).
- Persistent Disk Caching (
DiskCache): Durable SQLite storage in Write-Ahead Log (WAL) mode supporting multi-process concurrency, automatic pruning, and size management. - Tiered Multi-Layer Storage (
TieredCache): Combine ultra-fast L1 memory with persistent L2 disk; automatic L2 hit promotion into L1. - Cache Stampede Protection (
SingleFlight/AsyncSingleFlight): Coalesces concurrent calls on missing/expired keys, ensuring only a single loader runs while waiting threads/coroutines share the result. - Sync & Async Decorators:
@cachedand@acachedwith fine-grained key builders, cache invalidation hooks (func.invalidate(...)), andunlessbypass conditions. - Payload Serialization & Compression: Built-in support for
PickleSerializer,JSONSerializer, andCompressedSerializer(transparentzlibcompression for large payloads). - Built-in CLI: Benchmarking suite (
cacheforge benchmark), disk store inspection (cacheforge inspect), and pruning tools (cacheforge prune). - Comprehensive Observability: Thread-safe telemetry tracking hits, misses, hit rates, evictions, and expirations.
Installation
pip install cacheforge
Or install in editable mode for development:
git clone https://github.com/mrhamidrezam/cacheforge.git
cd cacheforge
pip install -e .
Quick Start
1. In-Memory LRU & TTL Caching
from cacheforge import LRUCache, TTLCache
# In-memory LRU with capacity limit
cache = LRUCache(maxsize=1000, default_ttl=300.0)
cache.set("user:101", {"name": "Alice", "role": "admin"})
# Dict-like access
print(cache.get("user:101"))
print("user:101" in cache)
# TTL expiration
ttl_cache = TTLCache(default_ttl=60.0, maxsize=500)
ttl_cache.set("session_token", "secret_token_123")
print("Remaining TTL (seconds):", ttl_cache.ttl_remaining("session_token"))
2. Cache Stampede Prevention with SingleFlight
Protect your database from thundering herd spikes when an expensive or popular key expires:
import asyncio
from cacheforge import acached
@acached(stampede_protect=True, ttl=60.0)
async def fetch_expensive_market_rates(symbol: str) -> dict:
print(f"Executing database query for {symbol}...")
await asyncio.sleep(0.5)
return {"symbol": symbol, "rate": 104.25}
# If 50 coroutines invoke fetch_expensive_market_rates('EUR/USD') simultaneously,
# only 1 query executes. All 50 awaiters receive the shared result!
3. Hierarchical Tiered Caching (Memory L1 + Disk L2)
from cacheforge import LRUCache, DiskCache, TieredCache
# Fast L1 in-memory RAM cache (1,000 items)
l1 = LRUCache(maxsize=1000)
# Durable L2 disk cache (100,000 items)
l2 = DiskCache(directory="./data/cache", maxsize=100000)
# Automatic fallback and promotion:
# Checks L1 -> Miss -> Checks L2 -> Hit -> Promotes to L1!
tiered = TieredCache(l1=l1, l2=l2, write_through=True)
tiered.set("report:2026-Q3", {"revenue": 5200000})
report = tiered.get("report:2026-Q3")
4. Function Decorators & Programmatic Invalidation
from cacheforge import cached
@cached(ttl=600)
def compute_user_stats(user_id: int):
# Heavy analytical computation
return {"user_id": user_id, "score": 98.4}
# First call: computes and stores
stats = compute_user_stats(42)
# Second call: instantaneous cache hit
stats = compute_user_stats(42)
# Explicitly invalidate specific key
compute_user_stats.invalidate(42)
# Inspect metrics
print(compute_user_stats.cache_info())
Command Line Interface (CLI)
CacheForge ships with an intuitive CLI tool:
Run Algorithm Benchmark
cacheforge benchmark --iterations 100000 --cache-size 1000
Output:
=================================================================
CacheForge Benchmark: 100,000 ops | Capacity: 1,000
=================================================================
Algorithm | Time (s) | Ops / Sec | Hit Rate | Hits | Evictions
---------------------------------------------------------------------------
LRUCache | 0.082 | 1,219,512 | 78.4% | 78,400 | 20,600
LFUCache | 0.091 | 1,098,901 | 81.2% | 81,200 | 17,800
FIFOCache | 0.075 | 1,333,333 | 72.1% | 72,100 | 26,900
TTLCache | 0.088 | 1,136,363 | 78.4% | 78,400 | 20,600
=================================================================
Inspect Disk Cache SQLite Database
cacheforge inspect ./data/cache
Prune Expired Keys & Vacuum Database
cacheforge prune ./data/cache
Architecture
For an in-depth exploration of CacheForge's internal design, lock synchronization models, and complexity analysis, see the Architecture Guide.
For development environment setup and instructions on building distribution packages, see the Setup Guide.
Running Tests
CacheForge is rigorously tested with over 50+ unit and concurrency tests:
pytest -v
License
This project is licensed under the terms of the MIT License.
Metadata
Release files for pycacheforge 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pycacheforge-0.1.0.tar.gz | 28.2 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pycacheforge-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 55.5 kB
Release files / pycacheforge-0.1.0.tar.gz
| Download URL | pycacheforge-0.1.0.tar.gz |
|---|---|
| Size | 28.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ff3d9a91a2cbbb826cc3445cdd673580478b692460cafe96295f43a77bb41c17
|
|
BLAKE2b-256 checksum How to use checksums |
a5191f38cc1c675d41ce70cba468901aa29626ad6839c99ebfea2228b1a0cada
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.
Transparency logRelease files / pycacheforge-0.1.0-py3-none-any.whl
| Download URL | pycacheforge-0.1.0-py3-none-any.whl |
|---|---|
| Size | 27.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cecf8b4aec35629776260f05bb35c54bc38489779c6b107f21409583ee757026
|
|
BLAKE2b-256 checksum How to use checksums |
84b9f2e45d1f8e1a678e2b59c11731f1efa627e0b76b0f95c4c40221cad79e40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.
Transparency logSecurity for pycacheforge 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for pycacheforge-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.