配置 SSOT(TOML + .env) 统一错误契约 Auth 与事务边界 Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client 可观测性(OpenTelemetry + LGTM)
1.3 KiB
1.3 KiB
title, impact, impactDescription, tags, description, alwaysApply
| title | impact | impactDescription | tags | description | alwaysApply |
|---|---|---|---|---|---|
| Set TTL on Cache Keys | HIGH | Prevents unbounded memory growth | ttl, expiration, cache, memory | Set TTL on Cache Keys | true |
Set TTL on Cache Keys
Always set expiration times on cache keys to prevent unbounded memory growth.
Correct: Set TTL at write time.
Python (redis-py):
# Good: TTL set atomically with the value
redis.setex("cache:user:1001", 3600, user_json)
# Good: For hashes, set TTL after
redis.hset("session:abc", mapping=session_data)
redis.expire("session:abc", 1800)
Java (Jedis):
import redis.clients.jedis.params.SetParams;
// Good: TTL set atomically with SetParams
jedis.set("cachedItem:1", "fe8c357903ac9", new SetParams().ex(120));
Incorrect: Forgetting TTL on cache keys.
Python (redis-py):
# Risk: This key may live forever
redis.set("cache:user:1001", user_json)
Java (Jedis):
// Risk: This key may live forever
jedis.set("cachedItem:1", "fe8c357903ac9");
TTL strategies:
- Cache data: 1-24 hours depending on freshness requirements
- Sessions: 30 minutes to 24 hours
- Rate limiting: Seconds to minutes
- Temporary locks: Seconds with automatic release
Reference: Redis EXPIRE