Skip to main content

A 3-tier caching system for Flask with zero external dependencies

Project description

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_cache with 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.

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

flask_supercache-1.0.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

flask_supercache-1.0.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file flask_supercache-1.0.0.tar.gz.

File metadata

  • Download URL: flask_supercache-1.0.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for flask_supercache-1.0.0.tar.gz
Algorithm Hash digest
SHA256 28ed36b21b57dadce4e95d2616ce562998999a5919c3a4ac7bf749647feaefc6
MD5 ded6dad016adbd4116330f727f9df7f6
BLAKE2b-256 8d42c1650465ed5f6f7240fc2d8822a59fc0db11cf1a451f2acdd6a0867fa70c

See more details on using hashes here.

File details

Details for the file flask_supercache-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_supercache-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b71f5b993c4579e7909006374d026221d1efacc963314d38f1895380552ac0a2
MD5 d5bb3f0ad40d8194e0df5beca41d3542
BLAKE2b-256 65ee515d352eef7b5eb36932c9956fdafe34afb35cba979360e64407016c9cdd

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