Adaptive cache invalidation for Redis. Drop-in wrapper, zero config.
Project description
aci-cache
Adaptive cache invalidation for Redis. Drop-in wrapper, zero config.
aci-cache wraps your existing Redis client and adds an intelligent invalidation
layer that automatically adjusts its strategy based on real-time write patterns:
- Low write rate → TTL-based expiry (minimal overhead)
- Medium write rate → Batched invalidation (balanced efficiency)
- High write rate → Eager per-write invalidation (maximum consistency)
Quick Start
pip install aci-cache
import redis
from aci_cache import AdaptiveCache
# Wrap your existing Redis client — that's it
r = redis.Redis(host="localhost")
cache = AdaptiveCache(r)
# Use it just like Redis
cache.set("user:123", "alice")
user = cache.get("user:123")
# Check what strategy is active
print(cache.strategy) # "ttl", "batched", or "eager"
How It Works
The library monitors your write rate in real time using a sliding window. Based on configurable thresholds, it automatically switches between three invalidation strategies:
| Strategy | When Active | Behavior |
|---|---|---|
| TTL | < 10 writes/sec | Standard Redis TTL. Zero overhead. |
| Batched | 10–50 writes/sec | Buffers keys, flushes every 2.5s via Pub/Sub. |
| Eager | > 50 writes/sec | Publishes invalidation per write via Pub/Sub. |
Multi-Instance Support
When running multiple app instances behind a load balancer, aci-cache
coordinates cache invalidation across all instances via Redis Pub/Sub:
# Instance 1 (controller)
cache = AdaptiveCache(redis.Redis(), is_controller=True)
# Instance 2-N (followers)
cache = AdaptiveCache(redis.Redis(), is_controller=False)
Configuration
All settings have sensible defaults. Override via constructor:
cache = AdaptiveCache(
redis.Redis(),
high_threshold=100, # writes/sec → eager
low_threshold=20, # writes/sec → ttl
default_ttl=30, # seconds
batch_interval=5.0, # seconds between batch flushes
namespace="my-service", # key namespace for multi-service Redis
)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aci_cache-0.1.0.tar.gz.
File metadata
- Download URL: aci_cache-0.1.0.tar.gz
- Upload date:
- Size: 36.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1963295c9dc88ab473079353c9bd68079bf1e2cd17e4a0a907f4b3d50ea509a
|
|
| MD5 |
d7a7ab1f087ed4620b644d514b2b4d49
|
|
| BLAKE2b-256 |
101ec91b1ea4a36406f3c39e777044e18eb044d58c5addaaaea77b9f8941fcf8
|
File details
Details for the file aci_cache-0.1.0-py3-none-any.whl.
File metadata
- Download URL: aci_cache-0.1.0-py3-none-any.whl
- Upload date:
- Size: 20.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcc2db9d4d9448b09344fb82b0a19a273d3245a7095d4ec0403f57616ebd31e7
|
|
| MD5 |
ee3dcc884638ac2f1b1c8c19cab63e6d
|
|
| BLAKE2b-256 |
3a58bfec23e1769e90d9a2e3ca162f2e0756be08fd7113384bcd4068777d8ff6
|