ERR invalid expire time: Expire value is out of range
The command accepted a TTL option but the number with it is not valid: it must be a positive integer within the supported range.
- Error prefix
ERR invalid expire time
它的意義
Commands that can expire a key, such as SET with EX, PX, EXAT or PXAT, EXPIRE, PEXPIRE, EXPIREAT and GETEX, validate the TTL before doing anything. A zero, a negative number, or a timestamp far outside the representable range produces this error with the command named, and the key is left untouched.
Two shapes of mistake dominate. A computed TTL that resolved to 0 or negative, where the code meant 'no expiry' and passed it straight through. And confusing relative with absolute: EX takes seconds from now, EXAT takes a unix timestamp. Handing a timestamp to EX does not error; it sets a TTL decades out, so the symptom is a key that never expires.
常見原因
- A computed TTL resolved to 0 or negative, for example a deadline that was already in the past.
- An absolute unix timestamp was passed to the relative option, EX instead of EXAT or the reverse.
- The client sent the TTL in the wrong form, such as the string 60s or a nil value.
Find the cause
SET session:abc data EX 0 # (error) ERR invalid expire time in 'set' command - the check itself TTL session:abc # The key is unchanged: -1 (no TTL) or -2 (missing) TIME # The server clock, for computing EXAT values
如何修正
-
Check the value and unit at the call site; relative options need positive integers:
SET session:abc data EX 3600 # 3600 seconds from now SET session:abc data EXAT 1798761600 # absolute unix timestamp
-
Guard computed TTLs before the call, and skip the command entirely when the intent was 'no expiry'.
-
When the intent is 'keep the existing TTL', say so instead of recomputing it:
SET session:abc newdata KEEPTTL
Other ways to handle it
Centralize TTL computation in one client helper so every expiry goes through the same validation, and prefer EXPIRE after the write when the TTL comes from a later decision. KEEPTTL replaces most recomputation of existing expiries.
安全地重現問題
redis-cli SET session:abc data EX 0 # (error) ERR invalid expire time in 'set' command redis-cli SET session:abc data EX 60 # OK redis-cli TTL session:abc # (integer) 60 or a little less
問題
Does EX 0 delete the key immediately?
No, SET rejects it before anything happens. To expire on purpose, use EXPIRE, which treats a non-positive TTL as 'delete now', while SET's EX option validates instead.
Is the timestamp in seconds or milliseconds?
EX and EXAT are seconds; PX and PXAT are milliseconds. Mixing the two does not raise this error; it sets the wrong expiry silently, so read TTL or PTTL after the write to confirm.
相關錯誤
More data types and commands errors
WRONGTYPEThe key holds a different data typeERR unknown commandRedis does not know this commandNOSCRIPTScript SHA1 is not cachedEXECABORTTransaction discarded after an errorBUSYServer is blocked by a long script
程式碼和訊息文字來自 Redis documentation and source code。瀏覽完整的 Redis error list 或 搜尋每個資料庫錯誤.
Work on Redis in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for Redis and the other databases you use.
本頁文字與資料: CC BY 4.0,請註明出處 SQLly (sqlly.app)。 程式碼與 SQL 片段: MIT. 授權詳情 引用的錯誤訊息文字歸其資料庫廠商所有。