Flask-SuperCache
3-tier caching for Flask applications. Adds TTL support to Python's LRU cache.
What it does
- LRU cache with TTL: Combines
functools.lru_cachewith time-based expiration - 3-tier fallback: Redis → Filesystem → In-memory
- No external dependencies required: Works with just Python standard library
Built while working on wallmarkets.
Installation
pip install flask-supercache
Usage
Basic LRU cache with TTL
from flask_supercache import cached_query
@cached_query(maxsize=1000, ttl_seconds=300)
def get_user_by_id(user_id):
return User.query.get(user_id)
# First call - hits database
user = get_user_by_id(123)
# Second call - from cache
user = get_user_by_id(123)
# After 5 minutes - cache expires, hits database again
Flask-Caching integration
from flask import Flask
from flask_supercache import setup_cache
app = Flask(__name__)
app.config['CACHE_REDIS_URL'] = 'redis://localhost:6379/0'
cache = setup_cache(app) # Auto-selects Redis or filesystem
How TTL on LRU works
Standard functools.lru_cache doesn't support TTL. We add it using time-bucketing:
# Cache key includes current time bucket
time_bucket = int(time.time() // ttl_seconds)
cache_key = (args, kwargs, time_bucket)
# When time_bucket changes, it's a cache miss
# Old entries get evicted by LRU automatically
No background threads, no timers. O(1) lookup.
Cache statistics
info = get_user_by_id.cache_info()
print(f"Hits: {info.hits}, Misses: {info.misses}")
print(f"Hit rate: {info.hits / (info.hits + info.misses) * 100:.1f}%")
Configuration
app.config['CACHE_REDIS_URL'] = 'redis://localhost:6379/0'
app.config['CACHE_DEFAULT_TIMEOUT'] = 300
app.config['CACHE_DIR'] = '/tmp/flask_cache'
License
MIT
Contributing
Pull requests welcome. Please add tests.
See CONTRIBUTING.md for details.
Metadata
Release files for flask-supercache 1.0.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 | Actions |
|---|---|---|---|---|
| flask_supercache-1.0.0.tar.gz | 8.5 kB | Without provenance | Details for flask_supercache-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flask_supercache-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for flask_supercache-1.0.0-py3-none-any.whl |
Total release size: 16.9 kB
Release files / flask_supercache-1.0.0.tar.gz
| Download URL | flask_supercache-1.0.0.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
28ed36b21b57dadce4e95d2616ce562998999a5919c3a4ac7bf749647feaefc6
|
|
BLAKE2b-256 checksum How to use checksums |
8d42c1650465ed5f6f7240fc2d8822a59fc0db11cf1a451f2acdd6a0867fa70c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / flask_supercache-1.0.0-py3-none-any.whl
| Download URL | flask_supercache-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b71f5b993c4579e7909006374d026221d1efacc963314d38f1895380552ac0a2
|
|
BLAKE2b-256 checksum How to use checksums |
65ee515d352eef7b5eb36932c9956fdafe34afb35cba979360e64407016c9cdd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Security for flask-supercache 1.0.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.