配置 SSOT(TOML + .env) 统一错误契约 Auth 与事务边界 Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client 可观测性(OpenTelemetry + LGTM)
2.0 KiB
2.0 KiB
title, impact, impactDescription, tags, description, alwaysApply
| title | impact | impactDescription | tags | description | alwaysApply |
|---|---|---|---|---|---|
| Use Hash Field Expiration for Per-Field TTL | MEDIUM | Fine-grained expiration without managing timers | hash, expiration, ttl, hexpire | Use Hash Field Expiration for Per-Field TTL | true |
Use Hash Field Expiration for Per-Field TTL
Use hash field expiration (Redis 7.4+) to delete individual fields automatically from a hash after a specific period of time. This is useful for caching scenarios where different fields have different lifetimes, and is easier than managing expiration from your own code.
Correct: Use HEXPIRE to set per-field TTL on hash fields.
Python (redis-py):
import redis
client = redis.Redis(host='localhost', port=6379)
# Set hash fields
client.hset("sensor:sensor1", mapping={
"air_quality": "256",
"battery_level": "89"
})
# Set 60-second TTL on specific fields (Redis 7.4+)
client.hexpire("sensor:sensor1", 60, "air_quality", "battery_level")
Java (Jedis):
import redis.clients.jedis.UnifiedJedis;
import java.util.Map;
import java.util.HashMap;
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
Map<String, String> hashFields = new HashMap<>();
hashFields.put("air_quality", "256");
hashFields.put("battery_level", "89");
jedis.hset("sensor:sensor1", hashFields);
// Set 60-second TTL on specific fields (Redis 7.4+)
jedis.hexpire("sensor:sensor1", 60, "air_quality", "battery_level");
}
When to use:
- Sensor data or metrics that become stale after a period
- Session attributes where different fields have different lifetimes
- Cached values within a hash that should auto-expire independently
- Temporary flags or tokens stored alongside persistent data
When NOT needed:
- Persistent user profiles or configuration
- Data where the entire hash should expire together (use
EXPIREon the key instead) - Fields managed by application logic with explicit deletion
Reference: HEXPIRE command