Files
life-echo/api/.agents/skills/redis-development/rules/ram-ttl.md
Sully 53e0065e3e refactor(api): TOML 配置 SSOT、统一错误契约、Auth/事务加固与可观测性 (#33)
配置 SSOT(TOML + .env)
统一错误契约
Auth 与事务边界
Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client
可观测性(OpenTelemetry + LGTM)
2026-05-22 13:44:50 +08:00

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