DocumentationCache data
API referenceGitHub

Cache data

Store JSON-compatible values with expiration in process memory, the configured database, or Redis.

Work through each step in order. The examples use the generated LenAPI project structure and are designed to be adapted to your app.

01

Choose a backend

Set CACHE_BACKEND to memory, database, or redis. This setting selects only app.cache. REDIS_URL is a connection string: redis://host:port/database-number. Memory is process-local and temporary; database and Redis are shared across app workers when configured to use the same backend.

Python
# settings.py
REDIS_URL = "redis://127.0.0.1:6379/0"
CACHE_BACKEND = "redis"  # memory, database, or redis
02

Set, read, and delete cache entries

The current Cache service API is asynchronous. Values must be JSON serializable. TTL is specified in seconds.

Python
await app.cache.set("profile:42", {"name": "Ada"}, ttl=300)
profile = await app.cache.get("profile:42")
await app.cache.delete("profile:42")
03

Initialize persistent storage

Database-backed cache uses the internal security-store table and is initialized by the migration command. Redis mode requires the Python redis extra and a separate reachable Redis server. Installing the extra does not install or start the Redis server.

Python
# For the database cache backend
python lenora.py migration

# Install the Redis client extra (quote brackets in zsh)
python -m pip install 'lenapi[redis]'

# Check a local Redis server
redis-cli ping  # should print PONG
TIP

CACHE_BACKEND='redis' affects app.cache. For Redis-backed sessions, throttles, IP cooldowns, opaque bearer tokens, or HMAC nonce checks, also set SECURITY_STORAGE='redis'.